1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
<?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 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 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 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 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 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.');
$this->data = ['id' => 1];
$builder = $this->mockCountBuilder(0, 'trashed_at');
$builder->shouldReceive('get')->andReturn(collect());
$this->assertSoftDeleted(new CustomProductStub($this->data));
}
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('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';
}
|