forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationDatabasePresenceVerifierTest.php
More file actions
72 lines (64 loc) · 3.8 KB
/
Copy pathValidationDatabasePresenceVerifierTest.php
File metadata and controls
72 lines (64 loc) · 3.8 KB
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
<?php
namespace Illuminate\Tests\Validation;
use Closure;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Validation\DatabasePresenceVerifier;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;
class ValidationDatabasePresenceVerifierTest extends TestCase
{
public function testBasicCount()
{
$verifier = new DatabasePresenceVerifier($db = m::mock(ConnectionResolverInterface::class));
$verifier->setConnection('connection');
$db->shouldReceive('connection')->once()->with('connection')->andReturn($conn = m::mock(stdClass::class));
$conn->shouldReceive('table')->once()->with('table')->andReturn($builder = m::mock(stdClass::class));
$builder->shouldReceive('useWritePdo')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('column', '=', 'value')->andReturn($builder);
$extra = ['foo' => 'NULL', 'bar' => 'NOT_NULL', 'baz' => 'taylor', 'faz' => true, 'not' => '!admin'];
$builder->shouldReceive('whereNull')->with('foo');
$builder->shouldReceive('whereNotNull')->with('bar');
$builder->shouldReceive('where')->with('baz', 'taylor');
$builder->shouldReceive('where')->with('faz', true);
$builder->shouldReceive('where')->with('not', '!=', 'admin');
$builder->shouldReceive('count')->once()->andReturn(100);
$this->assertEquals(100, $verifier->getCount('table', 'column', 'value', null, null, $extra));
}
public function testBasicCountWithClosures()
{
$verifier = new DatabasePresenceVerifier($db = m::mock(ConnectionResolverInterface::class));
$verifier->setConnection('connection');
$db->shouldReceive('connection')->once()->with('connection')->andReturn($conn = m::mock(stdClass::class));
$conn->shouldReceive('table')->once()->with('table')->andReturn($builder = m::mock(stdClass::class));
$builder->shouldReceive('useWritePdo')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('column', '=', 'value')->andReturn($builder);
$closure = function ($query) {
$query->where('closure', 1);
};
$extra = ['foo' => 'NULL', 'bar' => 'NOT_NULL', 'baz' => 'taylor', 'faz' => true, 'not' => '!admin', 0 => $closure];
$builder->shouldReceive('whereNull')->with('foo');
$builder->shouldReceive('whereNotNull')->with('bar');
$builder->shouldReceive('where')->with('baz', 'taylor');
$builder->shouldReceive('where')->with('faz', true);
$builder->shouldReceive('where')->with('not', '!=', 'admin');
$builder->shouldReceive('where')->with(m::type(Closure::class))->andReturnUsing(function () use ($builder, $closure) {
$closure($builder);
});
$builder->shouldReceive('where')->with('closure', 1);
$builder->shouldReceive('count')->once()->andReturn(100);
$this->assertEquals(100, $verifier->getCount('table', 'column', 'value', null, null, $extra));
}
public function testGetCountWithValidExcludeId()
{
$verifier = new DatabasePresenceVerifier($db = m::mock(ConnectionResolverInterface::class));
$verifier->setConnection('connection');
$db->shouldReceive('connection')->once()->with('connection')->andReturn($conn = m::mock(stdClass::class));
$conn->shouldReceive('table')->once()->with('table')->andReturn($builder = m::mock(stdClass::class));
$builder->shouldReceive('useWritePdo')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('column', '=', 'value')->andReturn($builder);
$builder->shouldReceive('where')->with('id', '<>', 123)->andReturn($builder);
$builder->shouldReceive('count')->once()->andReturn(100);
$this->assertEquals(100, $verifier->getCount('table', 'column', 'value', 123, 'id', []));
}
}