Skip to content

dmitry-ivanov/laravel-testing-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel-specific Testing Helpers and Assertions

Laravel Testing Tools

Buy me a coffee

StyleCI Build Status Coverage Status

Packagist Version Packagist Stars Packagist Downloads Packagist License

Laravel-specific Testing Helpers and Assertions.

Laravel Testing Tools
9.x 9.x
8.x 8.x
7.x 7.x
6.x 6.x
5.8.* 5.8.*
5.7.* 5.7.*
5.6.* 5.6.*
5.5.* 5.5.*
5.4.* 5.4.*
5.3.* 5.3.*
5.2.* 5.2.*
5.1.* 5.1.*

Usage

  1. Install the package via Composer:

    composer require --dev "illuminated/testing-tools:^9.0"
  2. Use Illuminated\Testing\TestingTools trait:

    use Illuminated\Testing\TestingTools;
    
    abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
    {
        use TestingTools;
    
        // ...
    }
  3. Use any of the provided helpers and assertions in your tests:

    class ExampleCommandTest extends TestCase
    {
        /** @test */
        public function it_logs_hello_world()
        {
            $this->artisan('example');
    
            $this->seeInLogFile('example.log', 'Hello World!');
        }
    }

Available helpers

Feel free to contribute.

Available assertions

Feel free to contribute.

Helpers

ApplicationHelpers

emulateLocal()

Emulate that application is running on the local environment:

$this->emulateLocal();

emulateProduction()

Emulate that application is running on the production environment:

$this->emulateProduction();

emulateEnvironment()

Emulate that application is running on the given environment:

$this->emulateEnvironment('demo');

isTravis()

Check whether the application is running on Travis or not:

$this->isTravis();

// true

Assertions

CollectionAsserts

assertCollectionsEqual()

Assert that the given collections are equal based on the specified key:

$this->assertCollectionsEqual($collection1, $collection2, 'id');

assertCollectionsNotEqual()

Assert that the given collections are not equal based on the specified key:

$this->assertCollectionsNotEqual($collection1, $collection2, 'id');

DatabaseAsserts

assertDatabaseHasTable()

Assert that the database has the given table:

$this->assertDatabaseHasTable('users');

assertDatabaseMissingTable()

Assert that the database doesn't have the given table:

$this->assertDatabaseMissingTable('unicorns');

assertDatabaseHasMany()

Assert that the database has all the given rows:

$this->assertDatabaseHasMany('posts', [
    ['title' => 'First Post'],
    ['title' => 'Second Post'],
    ['title' => 'Third Post'],
]);

assertDatabaseMissingMany()

Assert that the database doesn't have all the given rows:

$this->assertDatabaseMissingMany('posts', [
    ['title' => 'Fourth Post'],
    ['title' => 'Fifth Post'],
]);

EloquentAsserts

assertEloquentTableEquals()

Assert that the model's table name equals to the given value:

$this->assertEloquentTableEquals(User::class, 'users');

assertEloquentTableNotEquals()

Assert that the model's table name doesn't equal to the given value:

$this->assertEloquentTableNotEquals(User::class, 'posts');

assertEloquentIsIncrementing()

Assert that the model's primary key is incrementing:

$this->assertEloquentIsIncrementing(Post::class);

assertEloquentIsNotIncrementing()

Assert that the model's primary key is not incrementing:

$this->assertEloquentIsNotIncrementing(Category::class);

assertEloquentFillableEquals()

Assert that the model's fillable field equals to the given value:

$this->assertEloquentFillableEquals(Post::class, ['title', 'publish_at']);

assertEloquentFillableNotEquals()

Assert that the model's fillable field doesn't equal to the given value:

$this->assertEloquentFillableNotEquals(Post::class, ['title', 'body', 'publish_at']);

assertEloquentDatesEquals()

Assert that the model's dates field equals to the given value:

$this->assertEloquentDatesEquals(Post::class, ['publish_at', 'created_at', 'updated_at']);

assertEloquentDatesNotEquals()

Assert that the model's dates field doesn't equal to the given value:

$this->assertEloquentDatesNotEquals(Post::class, ['publish_at']);

assertEloquentTouchesEquals()

Assert that the model's touches field equals to the given value:

$this->assertEloquentTouchesEquals(Comment::class, ['post']);

assertEloquentTouchesNotEquals()

Assert that the model's touches field doesn't equal to the given value:

$this->assertEloquentTouchesNotEquals(Comment::class, ['user']);

assertEloquentHasMany()

NOTE: To use this assertion, you have to create model factories for both classes.

Assert that the model has the given HasMany relation:

$this->assertEloquentHasMany(Post::class, 'comments');

Assuming that Post class has comments relation:

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

assertEloquentHasCreateFor()

NOTE: To use this assertion, you have to create model factories for both classes.

Assert that the model has create method for the given HasMany relation:

$this->assertEloquentHasCreateFor(Post::class, 'comments');

Assuming that Post class has createComment() method:

class Post extends Model
{
    public function createComment(array $attributes)
    {
        return $this->comments()->create($attributes);
    }
}

assertEloquentHasCreateManyFor()

NOTE: To use this assertion, you have to create model factories for both classes.

Assert that the model has createMany method for the given HasMany relation:

$this->assertEloquentHasCreateManyFor(Post::class, 'comments');

Assuming that Post class has createManyComments() method:

class Post extends Model
{
    public function createManyComments(array $comments)
    {
        return $this->comments()->createMany($comments);
    }
}

assertEloquentBelongsTo()

NOTE: To use this assertion, you have to create model factories for both classes.

Assert that the model has the given BelongsTo relation:

$this->assertEloquentBelongsTo(Comment::class, 'post');

Assuming that Comment class has post relation:

class Comment extends Model
{
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

ExceptionAsserts

willSeeException()

Add expectation that the given exception would be thrown:

$this->willSeeException(RuntimeException::class, 'Oops! Houston, we have a problem!');

FilesystemAsserts

assertDirectoryEmpty()

Assert that the given directory is empty:

$this->assertDirectoryEmpty('./my/dir/');

assertDirectoryNotEmpty()

Assert that the given directory is not empty:

$this->assertDirectoryNotEmpty('./my/dir/');

assertFilesCount()

Assert that directory has the given number of files:

$this->assertFilesCount('./my/dir/', 3);

assertNotFilesCount()

Assert that directory doesn't have the given number of files:

$this->assertNotFilesCount('./my/dir/', 5);

LogFileAsserts

seeLogFile()

Assert that the given log file exists.

The path is relative to the storage/logs folder:

$this->seeLogFile('example.log');

dontSeeLogFile()

Assert that the given log file doesn't exist.

The path is relative to the storage/logs folder:

$this->dontSeeLogFile('foobarbaz.log');

seeInLogFile()

Assert that the log file contains the given message.

The path is relative to the storage/logs folder:

$this->seeInLogFile('example.log', 'Sample log message!');

Also, you can specify an array of messages:

$this->seeInLogFile('example.log', [
    'Sample log message 1!',
    'Sample log message 2!',
    'Sample log message 3!',
]);

You can use these placeholders in messages:

  • %datetime% - any datetime string.
$this->seeInLogFile('example.log', '[%datetime%]: Sample log message!');

dontSeeInLogFile()

Assert that the log file doesn't contain the given message.

The path is relative to the storage/logs folder:

$this->dontSeeInLogFile('example.log', 'Non-existing log message!');

Also, you can specify an array of messages:

$this->dontSeeInLogFile('example.log', [
    'Non-existing log message 1!',
    'Non-existing log message 2!',
    'Non-existing log message 3!',
]);

ReflectionAsserts

assertSubclassOf()

Assert that class is a subclass of the given parent:

$this->assertSubclassOf(Post::class, Model::class);

assertNotSubclassOf()

Assert that class is not a subclass of the given parent:

$this->assertNotSubclassOf(Post::class, Command::class);

assertTraitUsed()

Assert that class uses the given trait:

$this->assertTraitUsed(User::class, Notifiable::class);

assertTraitNotUsed()

Assert that class doesn't use the given trait:

$this->assertTraitNotUsed(Post::class, Notifiable::class);

assertMethodExists()

Assert that object has the given method:

$this->assertMethodExists(Post::class, 'save');

assertMethodNotExists()

Assert that object doesn't have the given method:

$this->assertMethodNotExists(Post::class, 'fly');

ScheduleAsserts

seeScheduleCount()

Assert that schedule count equals to the given value:

$this->seeScheduleCount(3);

dontSeeScheduleCount()

Assert that schedule count doesn't equal to the given value:

$this->dontSeeScheduleCount(5);

seeInSchedule()

Assert that the given command is scheduled:

$this->seeInSchedule('foo', 'everyFiveMinutes');
$this->seeInSchedule('bar', 'hourly');
$this->seeInSchedule('baz', 'twiceDaily');

Also, you can use cron expressions:

$this->seeInSchedule('foo', '*/5 * * * * *');
$this->seeInSchedule('bar', '0 * * * * *');
$this->seeInSchedule('baz', '0 1,13 * * * *');

dontSeeInSchedule()

Assert that the given command is not scheduled:

$this->dontSeeInSchedule('foobarbaz');

ServiceProviderAsserts

seeRegisteredAlias()

Assert that the given alias is registered:

$this->seeRegisteredAlias('Twitter');

dontSeeRegisteredAlias()

Assert that the given alias is not registered:

$this->dontSeeRegisteredAlias('FooBarBaz');

seeRegisteredCommand()

Assert that the given command is registered:

$this->seeRegisteredCommand('my-command');

dontSeeRegisteredCommand()

Assert that the given command is not registered:

$this->dontSeeRegisteredCommand('foobarbaz');

Sponsors

Laravel Idea
Material Theme UI Plugin

License

Laravel Testing Tools is open-sourced software licensed under the MIT license.

Support on Patreon

Packages

No packages published

Contributors 2

  •  
  •  

Languages