forked from Svaroh/JsFormValidatorBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAjaxControllerTest.php
More file actions
148 lines (123 loc) · 4.83 KB
/
Copy pathAjaxControllerTest.php
File metadata and controls
148 lines (123 loc) · 4.83 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
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
<?php
namespace Fp\JsFormValidatorBundle\Tests\Controller;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectRepository;
use Fp\JsFormValidatorBundle\Controller\AjaxController;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Class AjaxControllerTest
*
* @package Fp\JsFormValidatorBundle\Controller
*/
class AjaxControllerTest extends TestCase
{
/**
* Test action to check UniqueEntity constraint
*/
public function testCheckUniqueEntityAction()
{
$data = array(
'entityName' => 'Fp\JsFormValidatorBundle\Tests\TestBundles\DefaultTestBundle\Entity\BasicConstraintsEntity',
'data' => array(),
'ignoreNull' => '1',
'repositoryMethod' => 'findBy'
);
$repository = new InMemoryRepository();
$controller = new AjaxController($this->createRegistry($repository));
// Check a nonexistent email
$data['data']['email'] = 'test_email';
$response = $controller->checkUniqueEntityAction(new Request(array(), $data));
$this->assertTrue(json_decode($response->getContent()), 'A nonexistent is unique');
// Check an empty email
$data['data']['email'] = null;
$response = $controller->checkUniqueEntityAction(new Request(array(), $data));
$this->assertTrue(json_decode($response->getContent()), 'An empty email is unique');
// Check an existing email
$data['data']['email'] = 'existing_email';
$response = $controller->checkUniqueEntityAction(new Request(array(), $data));
$this->assertFalse(json_decode($response->getContent()), 'An existing email is NOT unique');
// Check the identical pair
$data['data']['email'] = 'existing_email';
$data['data']['url'] = 'existing_url';
$response = $controller->checkUniqueEntityAction(new Request(array(), $data));
$this->assertFalse(json_decode($response->getContent()), 'A pair of fields is NOT unique');
// Check the pair with ignore null
$data['data']['email'] = 'wrong_email';
$data['data']['url'] = null;
$response = $controller->checkUniqueEntityAction(new Request(array(), $data));
$this->assertTrue(
json_decode($response->getContent()),
'A pair of fields is unique where one of them is empty and ignoreNull = true'
);
// Check the pair without ignore null
$data['ignoreNull'] = '0';
$response = $controller->checkUniqueEntityAction(new Request(array(), $data));
$this->assertFalse(
json_decode($response->getContent()),
'A pair of fields is NOT unique where one of them is empty and ignoreNull = false'
);
// Check the another repository method
$data['repositoryMethod'] = 'find';
$response = $controller->checkUniqueEntityAction(new Request(array(), $data));
$this->assertFalse(json_decode($response->getContent()), 'Another repository method works');
}
public function testDoctrineIsRequired()
{
$this->expectException(\LogicException::class);
$controller = new AjaxController();
$controller->checkUniqueEntityAction(new Request(array(), array()));
}
public function testUniqueEntityRequestDataIsRequired()
{
$this->expectException(BadRequestHttpException::class);
$repository = new InMemoryRepository();
$controller = new AjaxController($this->createRegistry($repository));
$controller->checkUniqueEntityAction(new Request(array(), array()));
}
private function createRegistry(ObjectRepository $repository)
{
$registry = $this->createMock(ManagerRegistry::class);
$registry
->method('getRepository')
->willReturn($repository)
;
return $registry;
}
}
class InMemoryEntity
{
}
class InMemoryRepository implements ObjectRepository
{
public function find(mixed $id): ?object
{
return (object) array('id' => $id);
}
public function findAll(): array
{
return array();
}
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
if (isset($criteria['email']) && 'existing_email' === $criteria['email']) {
return array((object) $criteria);
}
if (array_key_exists('url', $criteria) && null === $criteria['url']) {
return array((object) $criteria);
}
return array();
}
public function findOneBy(array $criteria): ?object
{
return null;
}
/**
* @return class-string<object>
*/
public function getClassName(): string
{
return InMemoryEntity::class;
}
}