Skip to content

dmitry-ivanov/laravel-testing-tools

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Testing Tools

Become a Patron

StyleCI Build Status Coverage Status

Latest Stable Version Latest Unstable Version Total Downloads License

Provides Laravel-specific testing helpers and asserts.

Laravel Testing Tools
5.1.* 5.1.*
5.2.* 5.2.*
5.3.* 5.3.*
5.4.* 5.4.*
5.5.* 5.5.*

Usage

  1. Install package through composer:

    composer require --dev "illuminated/testing-tools:5.2.*"
  2. Use Illuminated\Testing\TestingTools in your TestCase class:

    use Illuminated\Testing\TestingTools;
    
    abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
    {
        use TestingTools;
    
        // ...
    }
  3. That's it! Now you can use any of provided helpers and asserts in your tests:

    class HelloCommandTest extends TestCase
    {
        /** @test */
        public function it_outputs_hello_world()
        {
            $this->artisan('hello');
    
            $this->seeArtisanOutput('Hello, World!');
        }
    }

Available helpers

New helpers are always adding. Feel free to contribute.

Available asserts

New asserts are always adding. Feel free to contribute.

Helpers

ApplicationHelpers

emulateLocal()

Emulates that application is working at local environment:

$this->emulateLocal();

emulateProduction()

Emulates that application is working at production environment:

$this->emulateProduction();

emulateEnvironment()

Emulates that application is working at specified environment:

$this->emulateEnvironment('demo');

isTravis()

Checks if tests are running on Travis CI or not:

if ($this->isTravis()) {
    // Yep, it's Travis.
}

ArtisanHelpers

runArtisan()

Runs artisan command directly by the class name, and return it:

$command = $this->runArtisan(MyCommand::class, ['--name' => 'John']);

Also, you can run artisan command via command object directly:

$command = $this->runArtisan(new MyCommand, ['--name' => 'Jane']);

Asserts

ArtisanAsserts

willSeeConfirmation()

Checks if confirmation is seen during artisan command execution:

$this->willSeeConfirmation('Are you sure?', MyCommand::class);

willNotSeeConfirmation()

Checks if confirmation is not seen during artisan command execution:

$this->willNotSeeConfirmation('Are you sure?', MyCommand::class);

willGiveConfirmation()

Checks if confirmation is seen during artisan command execution and accepts it:

$this->willGiveConfirmation('Are you sure?', MyCommand::class);

$this->seeArtisanOutput('Done!');

willNotGiveConfirmation()

Checks if confirmation is seen during artisan command execution and refuses it:

$this->willNotGiveConfirmation('Are you sure?', MyCommand::class);

$this->dontSeeArtisanOutput('Done!');

seeArtisanOutput()

Checks if specified string is seen as artisan output:

$this->seeArtisanOutput('Hello, World!');

Also, path to text file containing output can be provided:

$this->seeArtisanOutput('correct.output.txt');

dontSeeArtisanOutput()

Checks if specified string is not seen as artisan output:

$this->dontSeeArtisanOutput('Hello, Universe!');

Also, path to text file containing output can be provided:

$this->dontSeeArtisanOutput('incorrect.output.txt');

seeInArtisanOutput()

Checks if artisan output contains specified string:

$this->seeInArtisanOutput('Hello');

Also, path to text file can be provided:

$this->seeInArtisanOutput('needle.txt');

dontSeeInArtisanOutput()

Checks if artisan output not contains specified string:

$this->dontSeeInArtisanOutput('Universe');

Also, path to text file can be provided:

$this->dontSeeInArtisanOutput('wrong-needle.txt');

seeArtisanTableOutput()

Checks if specified data is seen as artisan table output:

$this->seeArtisanTableOutput([
    ['System' => 'Node-1', 'Status' => 'Enabled'],
    ['System' => 'Node-2', 'Status' => 'Enabled'],
    ['System' => 'Node-3', 'Status' => 'Enabled'],
]);

dontSeeArtisanTableOutput()

Checks if specified data is not seen as artisan table output:

$this->dontSeeArtisanTableOutput([
    ['System' => 'Node-1', 'Status' => 'Disabled'],
    ['System' => 'Node-2', 'Status' => 'Disabled'],
    ['System' => 'Node-3', 'Status' => 'Disabled'],
]);

seeArtisanTableRowsCount()

Checks if artisan output table rows count equals to specified value:

$this->seeArtisanTableRowsCount(3);

dontSeeArtisanTableRowsCount()

Checks if artisan output table rows count not equals to specified value:

$this->dontSeeArtisanTableRowsCount(5);

CollectionAsserts

assertCollectionsEqual()

Checks if passed collections are equal according to the specified key:

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

assertCollectionsNotEqual()

Checks if passed collections are not equal according to the specified key:

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

DatabaseAsserts

seeDatabaseTable()

Checks if specified table exists in database:

$this->seeDatabaseTable('users');

dontSeeDatabaseTable()

Checks if specified table not exists in database:

$this->dontSeeDatabaseTable('unicorns');

seeInDatabaseMany()

Checks if each of the specified rows exists in database:

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

dontSeeInDatabaseMany()

Checks if each of the specified rows is not exist in database:

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

EloquentAsserts

assertEloquentTableEquals()

Checks if Eloquent model table equals to specified value:

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

assertEloquentTableNotEquals()

Checks if Eloquent model table not equals to specified value:

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

assertEloquentIsIncrementing()

Checks if Eloquent model has incrementing primary key:

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

assertEloquentIsNotIncrementing()

Checks if Eloquent model has not incrementing primary key:

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

assertEloquentFillableEquals()

Checks if Eloquent model fillable fields are equal to specified value:

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

assertEloquentFillableNotEquals()

Checks if Eloquent model fillable fields are not equal to specified value:

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

assertEloquentDatesEquals()

Checks if Eloquent model date fields are equal to specified value:

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

assertEloquentDatesNotEquals()

Checks if Eloquent model date fields are not equal to specified value:

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

assertEloquentTouchesEquals()

Checks if Eloquent model touched relations are equal to specified value:

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

assertEloquentTouchesNotEquals()

Checks if Eloquent model touched relations are not equal to specified value:

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

assertEloquentHasMany()

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

Checks if Eloquent model has specified 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: In order to use this assertion, you have to create model factories for both classes.

Checks if Eloquent model has create method for specified 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: In order to use this assertion, you have to create model factories for both classes.

Checks if Eloquent model has createMany method for specified 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: In order to use this assertion, you have to create model factories for both classes.

Checks if Eloquent model has specified 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()

Adds expectation that exception of the specified class, with specified message and specified code will be thrown:

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

FilesystemAsserts

assertDirectoryEmpty()

Checks if specified directory is empty:

$this->assertDirectoryEmpty('./some/folder');

assertDirectoryNotEmpty()

Checks if specified directory is not empty:

$this->assertDirectoryNotEmpty('./some/folder');

assertFilesCount()

Checks if specified directory has specified number of files:

$this->assertFilesCount('./some/folder', 3);

assertNotFilesCount()

Checks if specified directory not has specified number of files:

$this->assertNotFilesCount('./some/folder', 5);

LogFileAsserts

seeLogFile()

Checks if log file exists by specified path. Path is relative to storage/logs folder:

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

dontSeeLogFile()

Checks if log file not exists by specified path. Path is relative to storage/logs folder:

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

seeInLogFile()

Checks if log file contains specified content. Path is relative to storage/logs folder.

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

Or you can pass an array of expected content items:

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

These placeholders are also available for content:

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

dontSeeInLogFile()

Checks if log file not contains specified content. Path is relative to storage/logs folder.

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

Or you can pass an array of unexpected content items:

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

ReflectionAsserts

assertSubclassOf()

Checks that class is subclass of specified parent class:

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

assertNotSubclassOf()

Checks that class is not subclass of specified parent class:

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

assertTraitUsed()

Checks that class is using specified trait:

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

assertTraitNotUsed()

Checks that class is not using specified trait:

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

assertMethodExists()

Checks that method exists on specified object or class name:

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

assertMethodNotExists()

Checks that method not exists on specified object or class name:

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

ScheduleAsserts

seeScheduleCount()

Checks that schedule events count is exactly as specified:

$this->seeScheduleCount(3);

dontSeeScheduleCount()

Checks that schedule events count is not exactly as specified:

$this->dontSeeScheduleCount(5);

seeInSchedule()

Checks that command is in schedule. Expressions can be the same as schedule event methods:

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

Also you can pass pure cron expressions if you wish:

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

dontSeeInSchedule()

Checks that command is not in schedule:

$this->dontSeeInSchedule('foobarbaz');

ServiceProviderAsserts

seeRegisteredAlias()

Checks that specified alias was successfully registered by alias loader:

$this->seeRegisteredAlias('Twitter');

dontSeeRegisteredAlias()

Checks that specified alias was not registered by alias loader:

$this->dontSeeRegisteredAlias('FooBarBaz');

seeRegisteredCommand()

Checks that specified command was successfully registered by service provider:

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

dontSeeRegisteredCommand()

Checks that specified command was not registered by service provider:

$this->dontSeeRegisteredCommand('foobarbaz');

License

The MIT License. Please see License File for more information.

Support on Patreon