FoundationInteractsWithDatabaseTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace Illuminate\Tests\Foundation;
  3. use Illuminate\Database\Connection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Database\Query\Builder;
  7. use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
  8. use Mockery as m;
  9. use PHPUnit\Framework\ExpectationFailedException;
  10. use PHPUnit\Framework\TestCase;
  11. class FoundationInteractsWithDatabaseTest extends TestCase
  12. {
  13. use InteractsWithDatabase;
  14. protected $table = 'products';
  15. protected $data = [
  16. 'title' => 'Spark',
  17. 'name' => 'Laravel',
  18. ];
  19. protected $connection;
  20. protected function setUp(): void
  21. {
  22. $this->connection = m::mock(Connection::class);
  23. }
  24. protected function tearDown(): void
  25. {
  26. m::close();
  27. }
  28. public function testSeeInDatabaseFindsResults()
  29. {
  30. $this->mockCountBuilder(1);
  31. $this->assertDatabaseHas($this->table, $this->data);
  32. }
  33. public function testAssertDatabaseHasSupportModels()
  34. {
  35. $this->mockCountBuilder(1);
  36. $this->assertDatabaseHas(ProductStub::class, $this->data);
  37. $this->assertDatabaseHas(new ProductStub, $this->data);
  38. }
  39. public function testSeeInDatabaseDoesNotFindResults()
  40. {
  41. $this->expectException(ExpectationFailedException::class);
  42. $this->expectExceptionMessage('The table is empty.');
  43. $builder = $this->mockCountBuilder(0);
  44. $builder->shouldReceive('get')->andReturn(collect());
  45. $this->assertDatabaseHas($this->table, $this->data);
  46. }
  47. public function testSeeInDatabaseFindsNotMatchingResults()
  48. {
  49. $this->expectException(ExpectationFailedException::class);
  50. $this->expectExceptionMessage('Found similar results: '.json_encode([['title' => 'Forge']], JSON_PRETTY_PRINT));
  51. $builder = $this->mockCountBuilder(0);
  52. $builder->shouldReceive('take')->andReturnSelf();
  53. $builder->shouldReceive('get')->andReturn(collect([['title' => 'Forge']]));
  54. $this->assertDatabaseHas($this->table, $this->data);
  55. }
  56. public function testSeeInDatabaseFindsManyNotMatchingResults()
  57. {
  58. $this->expectException(ExpectationFailedException::class);
  59. $this->expectExceptionMessage('Found similar results: '.json_encode(['data', 'data', 'data'], JSON_PRETTY_PRINT).' and 2 others.');
  60. $builder = $this->mockCountBuilder(0);
  61. $builder->shouldReceive('count')->andReturn(0, 5);
  62. $builder->shouldReceive('take')->andReturnSelf();
  63. $builder->shouldReceive('get')->andReturn(
  64. collect(array_fill(0, 3, 'data'))
  65. );
  66. $this->assertDatabaseHas($this->table, $this->data);
  67. }
  68. public function testDontSeeInDatabaseDoesNotFindResults()
  69. {
  70. $this->mockCountBuilder(0);
  71. $this->assertDatabaseMissing($this->table, $this->data);
  72. }
  73. public function testAssertDatabaseMissingSupportModels()
  74. {
  75. $this->mockCountBuilder(0);
  76. $this->assertDatabaseMissing(ProductStub::class, $this->data);
  77. $this->assertDatabaseMissing(new ProductStub, $this->data);
  78. }
  79. public function testDontSeeInDatabaseFindsResults()
  80. {
  81. $this->expectException(ExpectationFailedException::class);
  82. $builder = $this->mockCountBuilder(1);
  83. $builder->shouldReceive('take')->andReturnSelf();
  84. $builder->shouldReceive('get')->andReturn(collect([$this->data]));
  85. $this->assertDatabaseMissing($this->table, $this->data);
  86. }
  87. public function testAssertTableEntriesCount()
  88. {
  89. $this->mockCountBuilder(1);
  90. $this->assertDatabaseCount($this->table, 1);
  91. }
  92. public function testAssertDatabaseCountSupportModels()
  93. {
  94. $this->mockCountBuilder(1);
  95. $this->assertDatabaseCount(ProductStub::class, 1);
  96. $this->assertDatabaseCount(new ProductStub, 1);
  97. }
  98. public function testAssertTableEntriesCountWrong()
  99. {
  100. $this->expectException(ExpectationFailedException::class);
  101. $this->expectExceptionMessage('Failed asserting that table [products] matches expected entries count of 3. Entries found: 1.');
  102. $this->mockCountBuilder(1);
  103. $this->assertDatabaseCount($this->table, 3);
  104. }
  105. public function testAssertDeletedPassesWhenDoesNotFindResults()
  106. {
  107. $this->mockCountBuilder(0);
  108. $this->assertDatabaseMissing($this->table, $this->data);
  109. }
  110. public function testAssertDeletedFailsWhenFindsResults()
  111. {
  112. $this->expectException(ExpectationFailedException::class);
  113. $builder = $this->mockCountBuilder(1);
  114. $builder->shouldReceive('get')->andReturn(collect([$this->data]));
  115. $this->assertDatabaseMissing($this->table, $this->data);
  116. }
  117. public function testAssertDeletedPassesWhenDoesNotFindModelResults()
  118. {
  119. $this->data = ['id' => 1];
  120. $builder = $this->mockCountBuilder(0);
  121. $builder->shouldReceive('get')->andReturn(collect());
  122. $this->assertDeleted(new ProductStub($this->data));
  123. }
  124. public function testAssertModelMissingPassesWhenDoesNotFindModelResults()
  125. {
  126. $this->data = ['id' => 1];
  127. $builder = $this->mockCountBuilder(0);
  128. $builder->shouldReceive('get')->andReturn(collect());
  129. $this->assertModelMissing(new ProductStub($this->data));
  130. }
  131. public function testAssertDeletedFailsWhenFindsModelResults()
  132. {
  133. $this->expectException(ExpectationFailedException::class);
  134. $this->data = ['id' => 1];
  135. $builder = $this->mockCountBuilder(1);
  136. $builder->shouldReceive('get')->andReturn(collect([$this->data]));
  137. $this->assertDeleted(new ProductStub($this->data));
  138. }
  139. public function testAssertSoftDeletedInDatabaseFindsResults()
  140. {
  141. $this->mockCountBuilder(1);
  142. $this->assertSoftDeleted($this->table, $this->data);
  143. }
  144. public function testAssertSoftDeletedSupportModelStrings()
  145. {
  146. $this->mockCountBuilder(1);
  147. $this->assertSoftDeleted(ProductStub::class, $this->data);
  148. }
  149. public function testAssertSoftDeletedInDatabaseDoesNotFindResults()
  150. {
  151. $this->expectException(ExpectationFailedException::class);
  152. $this->expectExceptionMessage('The table is empty.');
  153. $builder = $this->mockCountBuilder(0);
  154. $builder->shouldReceive('get')->andReturn(collect());
  155. $this->assertSoftDeleted($this->table, $this->data);
  156. }
  157. public function testAssertSoftDeletedInDatabaseDoesNotFindModelResults()
  158. {
  159. $this->expectException(ExpectationFailedException::class);
  160. $this->expectExceptionMessage('The table is empty.');
  161. $this->data = ['id' => 1];
  162. $builder = $this->mockCountBuilder(0);
  163. $builder->shouldReceive('get')->andReturn(collect());
  164. $this->assertSoftDeleted(new ProductStub($this->data));
  165. }
  166. public function testAssertSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnResults()
  167. {
  168. $this->expectException(ExpectationFailedException::class);
  169. $this->expectExceptionMessage('The table is empty.');
  170. $model = new CustomProductStub(['id' => 1, 'name' => 'Laravel']);
  171. $this->data = ['id' => 1, 'name' => 'Tailwind'];
  172. $builder = $this->mockCountBuilder(0, 'trashed_at');
  173. $builder->shouldReceive('get')->andReturn(collect());
  174. $this->assertSoftDeleted($model, ['name' => 'Tailwind']);
  175. }
  176. public function testAssertNotSoftDeletedInDatabaseFindsResults()
  177. {
  178. $this->mockCountBuilder(1);
  179. $this->assertNotSoftDeleted($this->table, $this->data);
  180. }
  181. public function testAssertNotSoftDeletedSupportModelStrings()
  182. {
  183. $this->mockCountBuilder(1);
  184. $this->assertNotSoftDeleted(ProductStub::class, $this->data);
  185. }
  186. public function testAssertNotSoftDeletedOnlyFindsMatchingModels()
  187. {
  188. $this->expectException(ExpectationFailedException::class);
  189. $this->expectExceptionMessage('Failed asserting that any existing row');
  190. $builder = $this->mockCountBuilder(0);
  191. $builder->shouldReceive('get')->andReturn(collect(), collect(1));
  192. $this->assertNotSoftDeleted(ProductStub::class, $this->data);
  193. }
  194. public function testAssertNotSoftDeletedInDatabaseDoesNotFindResults()
  195. {
  196. $this->expectException(ExpectationFailedException::class);
  197. $this->expectExceptionMessage('The table is empty.');
  198. $builder = $this->mockCountBuilder(0);
  199. $builder->shouldReceive('get')->andReturn(collect());
  200. $this->assertNotSoftDeleted($this->table, $this->data);
  201. }
  202. public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelResults()
  203. {
  204. $this->expectException(ExpectationFailedException::class);
  205. $this->expectExceptionMessage('The table is empty.');
  206. $this->data = ['id' => 1];
  207. $builder = $this->mockCountBuilder(0);
  208. $builder->shouldReceive('get')->andReturn(collect());
  209. $this->assertNotSoftDeleted(new ProductStub($this->data));
  210. }
  211. public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnResults()
  212. {
  213. $this->expectException(ExpectationFailedException::class);
  214. $this->expectExceptionMessage('The table is empty.');
  215. $model = new CustomProductStub(['id' => 1, 'name' => 'Laravel']);
  216. $this->data = ['id' => 1, 'name' => 'Tailwind'];
  217. $builder = $this->mockCountBuilder(0, 'trashed_at');
  218. $builder->shouldReceive('get')->andReturn(collect());
  219. $this->assertNotSoftDeleted($model, ['name' => 'Tailwind']);
  220. }
  221. public function testAssertExistsPassesWhenFindsResults()
  222. {
  223. $this->data = ['id' => 1];
  224. $builder = $this->mockCountBuilder(1);
  225. $builder->shouldReceive('get')->andReturn(collect($this->data));
  226. $this->assertModelExists(new ProductStub($this->data));
  227. }
  228. public function testGetTableNameFromModel()
  229. {
  230. $this->assertEquals($this->table, $this->getTable(ProductStub::class));
  231. $this->assertEquals($this->table, $this->getTable(new ProductStub));
  232. $this->assertEquals($this->table, $this->getTable($this->table));
  233. }
  234. protected function mockCountBuilder($countResult, $deletedAtColumn = 'deleted_at')
  235. {
  236. $builder = m::mock(Builder::class);
  237. $key = array_key_first($this->data);
  238. $value = $this->data[$key];
  239. $builder->shouldReceive('where')->with($key, $value)->andReturnSelf();
  240. $builder->shouldReceive('limit')->andReturnSelf();
  241. $builder->shouldReceive('where')->with($this->data)->andReturnSelf();
  242. $builder->shouldReceive('whereNotNull')->with($deletedAtColumn)->andReturnSelf();
  243. $builder->shouldReceive('whereNull')->with($deletedAtColumn)->andReturnSelf();
  244. $builder->shouldReceive('count')->andReturn($countResult)->byDefault();
  245. $this->connection->shouldReceive('table')
  246. ->with($this->table)
  247. ->andReturn($builder);
  248. return $builder;
  249. }
  250. protected function getConnection()
  251. {
  252. return $this->connection;
  253. }
  254. }
  255. class ProductStub extends Model
  256. {
  257. use SoftDeletes;
  258. protected $table = 'products';
  259. protected $guarded = [];
  260. }
  261. class CustomProductStub extends ProductStub
  262. {
  263. const DELETED_AT = 'trashed_at';
  264. }