ValidationDatabasePresenceVerifierTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Illuminate\Tests\Validation;
  3. use Closure;
  4. use Illuminate\Database\ConnectionResolverInterface;
  5. use Illuminate\Validation\DatabasePresenceVerifier;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. use stdClass;
  9. class ValidationDatabasePresenceVerifierTest extends TestCase
  10. {
  11. protected function tearDown(): void
  12. {
  13. m::close();
  14. }
  15. public function testBasicCount()
  16. {
  17. $verifier = new DatabasePresenceVerifier($db = m::mock(ConnectionResolverInterface::class));
  18. $verifier->setConnection('connection');
  19. $db->shouldReceive('connection')->once()->with('connection')->andReturn($conn = m::mock(stdClass::class));
  20. $conn->shouldReceive('table')->once()->with('table')->andReturn($builder = m::mock(stdClass::class));
  21. $builder->shouldReceive('useWritePdo')->once()->andReturn($builder);
  22. $builder->shouldReceive('where')->with('column', '=', 'value')->andReturn($builder);
  23. $extra = ['foo' => 'NULL', 'bar' => 'NOT_NULL', 'baz' => 'taylor', 'faz' => true, 'not' => '!admin'];
  24. $builder->shouldReceive('whereNull')->with('foo');
  25. $builder->shouldReceive('whereNotNull')->with('bar');
  26. $builder->shouldReceive('where')->with('baz', 'taylor');
  27. $builder->shouldReceive('where')->with('faz', true);
  28. $builder->shouldReceive('where')->with('not', '!=', 'admin');
  29. $builder->shouldReceive('count')->once()->andReturn(100);
  30. $this->assertEquals(100, $verifier->getCount('table', 'column', 'value', null, null, $extra));
  31. }
  32. public function testBasicCountWithClosures()
  33. {
  34. $verifier = new DatabasePresenceVerifier($db = m::mock(ConnectionResolverInterface::class));
  35. $verifier->setConnection('connection');
  36. $db->shouldReceive('connection')->once()->with('connection')->andReturn($conn = m::mock(stdClass::class));
  37. $conn->shouldReceive('table')->once()->with('table')->andReturn($builder = m::mock(stdClass::class));
  38. $builder->shouldReceive('useWritePdo')->once()->andReturn($builder);
  39. $builder->shouldReceive('where')->with('column', '=', 'value')->andReturn($builder);
  40. $closure = function ($query) {
  41. $query->where('closure', 1);
  42. };
  43. $extra = ['foo' => 'NULL', 'bar' => 'NOT_NULL', 'baz' => 'taylor', 'faz' => true, 'not' => '!admin', 0 => $closure];
  44. $builder->shouldReceive('whereNull')->with('foo');
  45. $builder->shouldReceive('whereNotNull')->with('bar');
  46. $builder->shouldReceive('where')->with('baz', 'taylor');
  47. $builder->shouldReceive('where')->with('faz', true);
  48. $builder->shouldReceive('where')->with('not', '!=', 'admin');
  49. $builder->shouldReceive('where')->with(m::type(Closure::class))->andReturnUsing(function () use ($builder, $closure) {
  50. $closure($builder);
  51. });
  52. $builder->shouldReceive('where')->with('closure', 1);
  53. $builder->shouldReceive('count')->once()->andReturn(100);
  54. $this->assertEquals(100, $verifier->getCount('table', 'column', 'value', null, null, $extra));
  55. }
  56. }