Laravel-specific testing helpers and asserts.
Laravel | Testing Tools |
---|---|
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.* |
-
Install the package via Composer:
composer require --dev "illuminated/testing-tools:^6.0"
-
Use
Illuminated\Testing\TestingTools
and disable$mockConsoleOutput
:use Illuminated\Testing\TestingTools; abstract class TestCase extends Illuminate\Foundation\Testing\TestCase { use TestingTools; public $mockConsoleOutput = false; // ... }
-
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!'); } }
New helpers are always adding. Feel free to contribute.
New asserts are always adding. Feel free to contribute.
- ArtisanAsserts
- CollectionAsserts
- DatabaseAsserts
- EloquentAsserts
- assertEloquentTableEquals
- assertEloquentTableNotEquals
- assertEloquentIsIncrementing
- assertEloquentIsNotIncrementing
- assertEloquentFillableEquals
- assertEloquentFillableNotEquals
- assertEloquentDatesEquals
- assertEloquentDatesNotEquals
- assertEloquentTouchesEquals
- assertEloquentTouchesNotEquals
- assertEloquentHasMany
- assertEloquentHasCreateFor
- assertEloquentHasCreateManyFor
- assertEloquentBelongsTo
- ExceptionAsserts
- FilesystemAsserts
- LogFileAsserts
- ReflectionAsserts
- ScheduleAsserts
- ServiceProviderAsserts
Emulate that application is running on the local
environment:
$this->emulateLocal();
Emulate that application is running on the production
environment:
$this->emulateProduction();
Emulate that application is running on the given environment:
$this->emulateEnvironment('demo');
Check whether application is running on Travis or not:
if ($this->isTravis()) {
// Yep, it's Travis.
}
Run artisan command by the class name, and return it:
$command = $this->runArtisan(MyCommand::class, ['--name' => 'John']);
Also, you can pass the command instance as a first argument:
$command = $this->runArtisan(new MyCommand, ['--name' => 'Jane']);
Add expectation that the given confirmation question would be shown:
$this->willSeeConfirmation('Are you sure?', MyCommand::class);
Add expectation that the given confirmation question would not be shown:
$this->willNotSeeConfirmation('Are you sure?', MyCommand::class);
Add expectation that the given confirmation question would be shown, and accept it:
$this->willGiveConfirmation('Are you sure?', MyCommand::class);
$this->seeArtisanOutput('Done!');
Add expectation that the given confirmation question would be shown, and do not accept it:
$this->willNotGiveConfirmation('Are you sure?', MyCommand::class);
$this->dontSeeArtisanOutput('Done!');
Assert that the given artisan output is seen:
$this->seeArtisanOutput('Hello, World!');
Also, you can pass the file path:
$this->seeArtisanOutput('correct.output.txt');
Assert that the given artisan output is not seen:
$this->dontSeeArtisanOutput('Hello, Universe!');
Also, you can pass the file path:
$this->dontSeeArtisanOutput('incorrect.output.txt');
Assert that the given string is seen in the artisan output:
$this->seeInArtisanOutput('Hello');
Also, you can pass the file path:
$this->seeInArtisanOutput('needle.txt');
Assert that the given string is not seen in the artisan output:
$this->dontSeeInArtisanOutput('Universe');
Also, you can pass the file path:
$this->dontSeeInArtisanOutput('wrong-needle.txt');
Assert that the given data is seen in the artisan output table:
$this->seeArtisanTableOutput([
['System' => 'Node-1', 'Status' => 'Enabled'],
['System' => 'Node-2', 'Status' => 'Enabled'],
['System' => 'Node-3', 'Status' => 'Enabled'],
]);
Assert that the given data is not seen in the artisan output table:
$this->dontSeeArtisanTableOutput([
['System' => 'Node-1', 'Status' => 'Disabled'],
['System' => 'Node-2', 'Status' => 'Disabled'],
['System' => 'Node-3', 'Status' => 'Disabled'],
]);
Assert that the artisan output table has the given number of data rows:
$this->seeArtisanTableRowsCount(3);
Assert that the artisan output table doesn't have the given number of data rows:
$this->dontSeeArtisanTableRowsCount(5);
Assert that the given collections are equal based on the specified key:
$this->assertCollectionsEqual($collection1, $collection2, 'id');
Assert that the given collections are not equal based on the specified key:
$this->assertCollectionsNotEqual($collection1, $collection2, 'id');
Assert that database has the given table:
$this->assertDatabaseHasTable('users');
Assert that database doesn't have the given table:
$this->assertDatabaseMissingTable('unicorns');
Assert that database has all of the given rows:
$this->assertDatabaseHasMany('posts', [
['title' => 'First Post'],
['title' => 'Second Post'],
['title' => 'Third Post'],
]);
Assert that database doesn't have all of the given rows:
$this->assertDatabaseMissingMany('posts', [
['title' => 'Fourth Post'],
['title' => 'Fifth Post'],
]);
Assert that model's table name equals to the given value:
$this->assertEloquentTableEquals(User::class, 'users');
Assert that model's table name doesn't equal to the given value:
$this->assertEloquentTableNotEquals(User::class, 'posts');
Assert that model's primary key is incrementing:
$this->assertEloquentIsIncrementing(Post::class);
Assert that model's primary key is not incrementing:
$this->assertEloquentIsNotIncrementing(Category::class);
Assert that model's fillable
field equals to the given value:
$this->assertEloquentFillableEquals(Post::class, ['title', 'publish_at']);
Assert that model's fillable
field doesn't equal to the given value:
$this->assertEloquentFillableNotEquals(Post::class, ['title', 'body', 'publish_at']);
Assert that model's dates
field equals to the given value:
$this->assertEloquentDatesEquals(Post::class, ['publish_at', 'created_at', 'updated_at']);
Assert that model's dates
field doesn't equal to the given value:
$this->assertEloquentDatesNotEquals(Post::class, ['publish_at']);
Assert that model's touches
field equals to the given value:
$this->assertEloquentTouchesEquals(Comment::class, ['post']);
Assert that model's touches
field doesn't equal to the given value:
$this->assertEloquentTouchesNotEquals(Comment::class, ['user']);
NOTE: To use this assertion, you have to create model factories for both classes.
Assert that 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);
}
}
NOTE: To use this assertion, you have to create model factories for both classes.
Assert that 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);
}
}
NOTE: To use this assertion, you have to create model factories for both classes.
Assert that 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);
}
}
NOTE: To use this assertion, you have to create model factories for both classes.
Assert that 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);
}
}
Add expectation that the given exception would be thrown:
$this->willSeeException(RuntimeException::class, 'Oops! Houston, we have a problem!');
Assert that the given directory is empty:
$this->assertDirectoryEmpty('./some/folder');
Assert that the given directory is not empty:
$this->assertDirectoryNotEmpty('./some/folder');
Assert that directory has the given number of files:
$this->assertFilesCount('./some/folder', 3);
Assert that directory doesn't have the given number of files:
$this->assertNotFilesCount('./some/folder', 5);
Assert that the given log file exists. The path is relative to storage/logs
folder:
$this->seeLogFile('example.log');
Assert that the given log file doesn't exist. The path is relative to storage/logs
folder:
$this->dontSeeLogFile('foobarbaz.log');
Assert that log file contains the given message. The path is relative to storage/logs
folder:
$this->seeInLogFile('example.log', 'Sample log message!');
Also, you can pass 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!');
Assert that log file doesn't contain the given message. The path is relative to storage/logs
folder:
$this->dontSeeInLogFile('example.log', 'Unexisting log message!');
Also, you can pass an array of messages:
$this->dontSeeInLogFile('example.log', [
'Unexisting log message 1!',
'Unexisting log message 2!',
'Unexisting log message 3!',
]);
Assert that class is a subclass of the given parent:
$this->assertSubclassOf(Post::class, Model::class);
Assert that class is not a subclass of the given parent:
$this->assertNotSubclassOf(Post::class, Command::class);
Assert that class uses the given trait:
$this->assertTraitUsed(User::class, Notifiable::class);
Assert that class doesn't use the given trait:
$this->assertTraitNotUsed(Post::class, Notifiable::class);
Assert that object has the given method:
$this->assertMethodExists(Post::class, 'save');
Assert that object doesn't have the given method:
$this->assertMethodNotExists(Post::class, 'fly');
Assert that schedule count equals to the given value:
$this->seeScheduleCount(3);
Assert that schedule count doesn't equal to the given value:
$this->dontSeeScheduleCount(5);
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 * * * *');
Assert that the given command is not scheduled:
$this->dontSeeInSchedule('foobarbaz');
Assert that the given alias is registered:
$this->seeRegisteredAlias('Twitter');
Assert that the given alias is not registered:
$this->dontSeeRegisteredAlias('FooBarBaz');
Assert that the given command is registered:
$this->seeRegisteredCommand('my-command');
Assert that the given command is not registered:
$this->dontSeeRegisteredCommand('foobarbaz');
The MIT License. Please see License File for more information.