Laravel-specific testing helpers and asserts.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
tests/TestingTools
.coveralls.yml
.gitattributes
.gitignore
.styleci.yml
.travis.yml
LICENSE
README.md
composer.json
composer.lock
phpunit.xml

README.md

Laravel Testing Tools

Become a Patron

StyleCI Build Status Coverage Status

Latest Stable Version Latest Unstable Version Total Downloads License

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.*
5.6.* 5.6.*
5.7.* 5.7.*

Usage

  1. Install the package via Composer:

    composer require --dev illuminated/testing-tools
  2. Use Illuminated\Testing\TestingTools and disable $mockConsoleOutput:

    use Illuminated\Testing\TestingTools;
    
    abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
    {
        use TestingTools;
    
        public $mockConsoleOutput = false;
    
        // ...
    }
  3. Use any of the 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()

Check if tests are running on Travis CI or not:

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

ArtisanHelpers

runArtisan()

Run artisan command by the class name, and return it:

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

Also, you can run artisan command by the command object:

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

Asserts

ArtisanAsserts

willSeeConfirmation()

Check if the confirmation is seen during artisan command execution:

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

willNotSeeConfirmation()

Check if the confirmation is not seen during artisan command execution:

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

willGiveConfirmation()

Check if the confirmation is seen during artisan command execution, and accept it:

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

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

willNotGiveConfirmation()

Check if the confirmation is seen during artisan command execution, and refuse it:

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

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

seeArtisanOutput()

Check if the specified string is seen as artisan output:

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

You can pass the path to the text file, containing output:

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

dontSeeArtisanOutput()

Check if the specified string is not seen as artisan output:

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

You can pass the path to the text file, containing output:

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

seeInArtisanOutput()

Check if artisan output contains specified string:

$this->seeInArtisanOutput('Hello');

You can pass the path to the text file:

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

dontSeeInArtisanOutput()

Check if artisan output doesn't contain specified string:

$this->dontSeeInArtisanOutput('Universe');

You can pass the path to the text file:

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

seeArtisanTableOutput()

Check if the 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()

Check if the 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()

Check if the artisan output table rows count equals to the specified value:

$this->seeArtisanTableRowsCount(3);

dontSeeArtisanTableRowsCount()

Check if the artisan output table rows count doesn't equal to the specified value:

$this->dontSeeArtisanTableRowsCount(5);

CollectionAsserts

assertCollectionsEqual()

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

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

assertCollectionsNotEqual()

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

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

DatabaseAsserts

assertDatabaseHasTable()

Check if the specified table exists in the database:

$this->assertDatabaseHasTable('users');

assertDatabaseMissingTable()

Check if the specified table doesn't exist in the database:

$this->assertDatabaseMissingTable('unicorns');

assertDatabaseHasMany()

Check if each of the specified rows exists in the database:

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

assertDatabaseMissingMany()

Check if each of the specified rows doesn't exist in the database:

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

EloquentAsserts

assertEloquentTableEquals()

Check if an Eloquent model table equals to the specified value:

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

assertEloquentTableNotEquals()

Check if an Eloquent model table doesn't equal to the specified value:

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

assertEloquentIsIncrementing()

Check if an Eloquent model has incrementing primary key:

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

assertEloquentIsNotIncrementing()

Check if an Eloquent model has not incrementing primary key:

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

assertEloquentFillableEquals()

Check if an Eloquent model fillable fields are equal to the specified value:

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

assertEloquentFillableNotEquals()

Check if an Eloquent model fillable fields are not equal to the specified value:

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

assertEloquentDatesEquals()

Check if an Eloquent model date fields are equal to the specified value:

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

assertEloquentDatesNotEquals()

Check if an Eloquent model date fields are not equal to the specified value:

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

assertEloquentTouchesEquals()

Check if an Eloquent model touched relations are equal to the specified value:

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

assertEloquentTouchesNotEquals()

Check if an Eloquent model touched relations are not equal to the specified value:

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

assertEloquentHasMany()

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

Check if an 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: To use this assertion, you have to create model factories for both classes.

Check if an Eloquent model has create method for the 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: To use this assertion, you have to create model factories for both classes.

Check if an Eloquent model has createMany method for the 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: To use this assertion, you have to create model factories for both classes.

Check if an 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()

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

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

FilesystemAsserts

assertDirectoryEmpty()

Check if the specified directory is empty:

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

assertDirectoryNotEmpty()

Check if the specified directory is not empty:

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

assertFilesCount()

Check if the specified directory has the specified files count:

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

assertNotFilesCount()

Check if the specified directory doesn't have the specified files count:

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

LogFileAsserts

seeLogFile()

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

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

dontSeeLogFile()

Check if the log file doesn't exist by the specified path. Path is relative to the storage/logs folder:

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

seeInLogFile()

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

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

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()

Check if the log file doesn't contain the specified content. Path is relative to the storage/logs folder.

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

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()

Check that class is a subclass of the specified parent class:

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

assertNotSubclassOf()

Check that class is not a subclass of the specified parent class:

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

assertTraitUsed()

Check that class is using the specified trait:

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

assertTraitNotUsed()

Check that class is not using the specified trait:

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

assertMethodExists()

Check that method exists on the specified object or class name:

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

assertMethodNotExists()

Check that method doesn't exist on the specified object or class name:

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

ScheduleAsserts

seeScheduleCount()

Check that schedule events count equals to the specified:

$this->seeScheduleCount(3);

dontSeeScheduleCount()

Check that schedule events count doesn't equal to the specified:

$this->dontSeeScheduleCount(5);

seeInSchedule()

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

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

You can pass pure cron expressions:

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

dontSeeInSchedule()

Check that the command is not in the schedule:

$this->dontSeeInSchedule('foobarbaz');

ServiceProviderAsserts

seeRegisteredAlias()

Check that the specified alias was successfully registered by the alias loader:

$this->seeRegisteredAlias('Twitter');

dontSeeRegisteredAlias()

Check that the specified alias was not registered by the alias loader:

$this->dontSeeRegisteredAlias('FooBarBaz');

seeRegisteredCommand()

Check that the specified command was successfully registered by the service provider:

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

dontSeeRegisteredCommand()

Check that the specified command was not registered by the service provider:

$this->dontSeeRegisteredCommand('foobarbaz');

License

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

Support on Patreon