123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- namespace Illuminate\Tests\Foundation;
- use Illuminate\Database\Connection;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
- use Mockery as m;
- use PHPUnit\Framework\ExpectationFailedException;
- use PHPUnit\Framework\TestCase;
- class FoundationInteractsWithDatabaseTest extends TestCase
- {
- use InteractsWithDatabase;
- protected $table = 'products';
- protected $data = [
- 'title' => 'Spark',
- 'name' => 'Laravel',
- ];
- protected $connection;
- protected function setUp(): void
- {
- $this->connection = m::mock(Connection::class);
- }
- protected function tearDown(): void
- {
- m::close();
- }
- public function testSeeInDatabaseFindsResults()
- {
- $this->mockCountBuilder(1);
- $this->assertDatabaseHas($this->table, $this->data);
- }
- public function testAssertDatabaseHasSupportModels()
- {
- $this->mockCountBuilder(1);
- $this->assertDatabaseHas(ProductStub::class, $this->data);
- $this->assertDatabaseHas(new ProductStub, $this->data);
- }
- public function testSeeInDatabaseDoesNotFindResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('The table is empty.');
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('get')->andReturn(collect());
- $this->assertDatabaseHas($this->table, $this->data);
- }
- public function testSeeInDatabaseFindsNotMatchingResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('Found similar results: '.json_encode([['title' => 'Forge']], JSON_PRETTY_PRINT));
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('take')->andReturnSelf();
- $builder->shouldReceive('get')->andReturn(collect([['title' => 'Forge']]));
- $this->assertDatabaseHas($this->table, $this->data);
- }
- public function testSeeInDatabaseFindsManyNotMatchingResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('Found similar results: '.json_encode(['data', 'data', 'data'], JSON_PRETTY_PRINT).' and 2 others.');
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('count')->andReturn(0, 5);
- $builder->shouldReceive('take')->andReturnSelf();
- $builder->shouldReceive('get')->andReturn(
- collect(array_fill(0, 3, 'data'))
- );
- $this->assertDatabaseHas($this->table, $this->data);
- }
- public function testDontSeeInDatabaseDoesNotFindResults()
- {
- $this->mockCountBuilder(0);
- $this->assertDatabaseMissing($this->table, $this->data);
- }
- public function testAssertDatabaseMissingSupportModels()
- {
- $this->mockCountBuilder(0);
- $this->assertDatabaseMissing(ProductStub::class, $this->data);
- $this->assertDatabaseMissing(new ProductStub, $this->data);
- }
- public function testDontSeeInDatabaseFindsResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $builder = $this->mockCountBuilder(1);
- $builder->shouldReceive('take')->andReturnSelf();
- $builder->shouldReceive('get')->andReturn(collect([$this->data]));
- $this->assertDatabaseMissing($this->table, $this->data);
- }
- public function testAssertTableEntriesCount()
- {
- $this->mockCountBuilder(1);
- $this->assertDatabaseCount($this->table, 1);
- }
- public function testAssertDatabaseCountSupportModels()
- {
- $this->mockCountBuilder(1);
- $this->assertDatabaseCount(ProductStub::class, 1);
- $this->assertDatabaseCount(new ProductStub, 1);
- }
- public function testAssertTableEntriesCountWrong()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('Failed asserting that table [products] matches expected entries count of 3. Entries found: 1.');
- $this->mockCountBuilder(1);
- $this->assertDatabaseCount($this->table, 3);
- }
- public function testAssertDeletedPassesWhenDoesNotFindResults()
- {
- $this->mockCountBuilder(0);
- $this->assertDatabaseMissing($this->table, $this->data);
- }
- public function testAssertDeletedFailsWhenFindsResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $builder = $this->mockCountBuilder(1);
- $builder->shouldReceive('get')->andReturn(collect([$this->data]));
- $this->assertDatabaseMissing($this->table, $this->data);
- }
- public function testAssertDeletedPassesWhenDoesNotFindModelResults()
- {
- $this->data = ['id' => 1];
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('get')->andReturn(collect());
- $this->assertDeleted(new ProductStub($this->data));
- }
- public function testAssertModelMissingPassesWhenDoesNotFindModelResults()
- {
- $this->data = ['id' => 1];
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('get')->andReturn(collect());
- $this->assertModelMissing(new ProductStub($this->data));
- }
- public function testAssertDeletedFailsWhenFindsModelResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->data = ['id' => 1];
- $builder = $this->mockCountBuilder(1);
- $builder->shouldReceive('get')->andReturn(collect([$this->data]));
- $this->assertDeleted(new ProductStub($this->data));
- }
- public function testAssertSoftDeletedInDatabaseFindsResults()
- {
- $this->mockCountBuilder(1);
- $this->assertSoftDeleted($this->table, $this->data);
- }
- public function testAssertSoftDeletedSupportModelStrings()
- {
- $this->mockCountBuilder(1);
- $this->assertSoftDeleted(ProductStub::class, $this->data);
- }
- public function testAssertSoftDeletedInDatabaseDoesNotFindResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('The table is empty.');
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('get')->andReturn(collect());
- $this->assertSoftDeleted($this->table, $this->data);
- }
- public function testAssertSoftDeletedInDatabaseDoesNotFindModelResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('The table is empty.');
- $this->data = ['id' => 1];
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('get')->andReturn(collect());
- $this->assertSoftDeleted(new ProductStub($this->data));
- }
- public function testAssertSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('The table is empty.');
- $model = new CustomProductStub(['id' => 1, 'name' => 'Laravel']);
- $this->data = ['id' => 1, 'name' => 'Tailwind'];
- $builder = $this->mockCountBuilder(0, 'trashed_at');
- $builder->shouldReceive('get')->andReturn(collect());
- $this->assertSoftDeleted($model, ['name' => 'Tailwind']);
- }
- public function testAssertNotSoftDeletedInDatabaseFindsResults()
- {
- $this->mockCountBuilder(1);
- $this->assertNotSoftDeleted($this->table, $this->data);
- }
- public function testAssertNotSoftDeletedSupportModelStrings()
- {
- $this->mockCountBuilder(1);
- $this->assertNotSoftDeleted(ProductStub::class, $this->data);
- }
- public function testAssertNotSoftDeletedOnlyFindsMatchingModels()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('Failed asserting that any existing row');
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('get')->andReturn(collect(), collect(1));
- $this->assertNotSoftDeleted(ProductStub::class, $this->data);
- }
- public function testAssertNotSoftDeletedInDatabaseDoesNotFindResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('The table is empty.');
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('get')->andReturn(collect());
- $this->assertNotSoftDeleted($this->table, $this->data);
- }
- public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('The table is empty.');
- $this->data = ['id' => 1];
- $builder = $this->mockCountBuilder(0);
- $builder->shouldReceive('get')->andReturn(collect());
- $this->assertNotSoftDeleted(new ProductStub($this->data));
- }
- public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnResults()
- {
- $this->expectException(ExpectationFailedException::class);
- $this->expectExceptionMessage('The table is empty.');
- $model = new CustomProductStub(['id' => 1, 'name' => 'Laravel']);
- $this->data = ['id' => 1, 'name' => 'Tailwind'];
- $builder = $this->mockCountBuilder(0, 'trashed_at');
- $builder->shouldReceive('get')->andReturn(collect());
- $this->assertNotSoftDeleted($model, ['name' => 'Tailwind']);
- }
- public function testAssertExistsPassesWhenFindsResults()
- {
- $this->data = ['id' => 1];
- $builder = $this->mockCountBuilder(1);
- $builder->shouldReceive('get')->andReturn(collect($this->data));
- $this->assertModelExists(new ProductStub($this->data));
- }
- public function testGetTableNameFromModel()
- {
- $this->assertEquals($this->table, $this->getTable(ProductStub::class));
- $this->assertEquals($this->table, $this->getTable(new ProductStub));
- $this->assertEquals($this->table, $this->getTable($this->table));
- }
- protected function mockCountBuilder($countResult, $deletedAtColumn = 'deleted_at')
- {
- $builder = m::mock(Builder::class);
- $key = array_key_first($this->data);
- $value = $this->data[$key];
- $builder->shouldReceive('where')->with($key, $value)->andReturnSelf();
- $builder->shouldReceive('limit')->andReturnSelf();
- $builder->shouldReceive('where')->with($this->data)->andReturnSelf();
- $builder->shouldReceive('whereNotNull')->with($deletedAtColumn)->andReturnSelf();
- $builder->shouldReceive('whereNull')->with($deletedAtColumn)->andReturnSelf();
- $builder->shouldReceive('count')->andReturn($countResult)->byDefault();
- $this->connection->shouldReceive('table')
- ->with($this->table)
- ->andReturn($builder);
- return $builder;
- }
- protected function getConnection()
- {
- return $this->connection;
- }
- }
- class ProductStub extends Model
- {
- use SoftDeletes;
- protected $table = 'products';
- protected $guarded = [];
- }
- class CustomProductStub extends ProductStub
- {
- const DELETED_AT = 'trashed_at';
- }
|