diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-06-01 21:27:35 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-06-22 17:59:35 +0200 |
commit | 23634d5d842dabcf5d7475e2becb7e127824239e (patch) | |
tree | b91688722a996c46f27e8fe0542c356424483da3 /tests/Wallabag/CoreBundle/ParamConverter | |
parent | 891a026e31ad54ca90b70f6026f23260cfadb7fd (diff) | |
download | wallabag-23634d5d842dabcf5d7475e2becb7e127824239e.tar.gz wallabag-23634d5d842dabcf5d7475e2becb7e127824239e.tar.zst wallabag-23634d5d842dabcf5d7475e2becb7e127824239e.zip |
Jump to Symfony 3.1
Diffstat (limited to 'tests/Wallabag/CoreBundle/ParamConverter')
-rw-r--r-- | tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php b/tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php new file mode 100644 index 00000000..e29b58b5 --- /dev/null +++ b/tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php | |||
@@ -0,0 +1,219 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Command; | ||
4 | |||
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | ||
6 | use Symfony\Component\HttpFoundation\Request; | ||
7 | use Wallabag\CoreBundle\ParamConverter\UsernameRssTokenConverter; | ||
8 | use Wallabag\UserBundle\Entity\User; | ||
9 | |||
10 | class UsernameRssTokenConverterTest extends \PHPUnit_Framework_TestCase | ||
11 | { | ||
12 | public function testSupportsWithNoRegistry() | ||
13 | { | ||
14 | $params = new ParamConverter([]); | ||
15 | $converter = new UsernameRssTokenConverter(); | ||
16 | |||
17 | $this->assertFalse($converter->supports($params)); | ||
18 | } | ||
19 | |||
20 | public function testSupportsWithNoRegistryManagers() | ||
21 | { | ||
22 | $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') | ||
23 | ->disableOriginalConstructor() | ||
24 | ->getMock(); | ||
25 | |||
26 | $registry->expects($this->once()) | ||
27 | ->method('getManagers') | ||
28 | ->will($this->returnValue([])); | ||
29 | |||
30 | $params = new ParamConverter([]); | ||
31 | $converter = new UsernameRssTokenConverter($registry); | ||
32 | |||
33 | $this->assertFalse($converter->supports($params)); | ||
34 | } | ||
35 | |||
36 | public function testSupportsWithNoConfigurationClass() | ||
37 | { | ||
38 | $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') | ||
39 | ->disableOriginalConstructor() | ||
40 | ->getMock(); | ||
41 | |||
42 | $registry->expects($this->once()) | ||
43 | ->method('getManagers') | ||
44 | ->will($this->returnValue(['default' => null])); | ||
45 | |||
46 | $params = new ParamConverter([]); | ||
47 | $converter = new UsernameRssTokenConverter($registry); | ||
48 | |||
49 | $this->assertFalse($converter->supports($params)); | ||
50 | } | ||
51 | |||
52 | public function testSupportsWithNotTheGoodClass() | ||
53 | { | ||
54 | $meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata') | ||
55 | ->disableOriginalConstructor() | ||
56 | ->getMock(); | ||
57 | |||
58 | $meta->expects($this->once()) | ||
59 | ->method('getName') | ||
60 | ->will($this->returnValue('nothingrelated')); | ||
61 | |||
62 | $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager') | ||
63 | ->disableOriginalConstructor() | ||
64 | ->getMock(); | ||
65 | |||
66 | $em->expects($this->once()) | ||
67 | ->method('getClassMetadata') | ||
68 | ->with('superclass') | ||
69 | ->will($this->returnValue($meta)); | ||
70 | |||
71 | $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') | ||
72 | ->disableOriginalConstructor() | ||
73 | ->getMock(); | ||
74 | |||
75 | $registry->expects($this->once()) | ||
76 | ->method('getManagers') | ||
77 | ->will($this->returnValue(['default' => null])); | ||
78 | |||
79 | $registry->expects($this->once()) | ||
80 | ->method('getManagerForClass') | ||
81 | ->with('superclass') | ||
82 | ->will($this->returnValue($em)); | ||
83 | |||
84 | $params = new ParamConverter(['class' => 'superclass']); | ||
85 | $converter = new UsernameRssTokenConverter($registry); | ||
86 | |||
87 | $this->assertFalse($converter->supports($params)); | ||
88 | } | ||
89 | |||
90 | public function testSupportsWithGoodClass() | ||
91 | { | ||
92 | $meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata') | ||
93 | ->disableOriginalConstructor() | ||
94 | ->getMock(); | ||
95 | |||
96 | $meta->expects($this->once()) | ||
97 | ->method('getName') | ||
98 | ->will($this->returnValue('Wallabag\UserBundle\Entity\User')); | ||
99 | |||
100 | $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager') | ||
101 | ->disableOriginalConstructor() | ||
102 | ->getMock(); | ||
103 | |||
104 | $em->expects($this->once()) | ||
105 | ->method('getClassMetadata') | ||
106 | ->with('WallabagUserBundle:User') | ||
107 | ->will($this->returnValue($meta)); | ||
108 | |||
109 | $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') | ||
110 | ->disableOriginalConstructor() | ||
111 | ->getMock(); | ||
112 | |||
113 | $registry->expects($this->once()) | ||
114 | ->method('getManagers') | ||
115 | ->will($this->returnValue(['default' => null])); | ||
116 | |||
117 | $registry->expects($this->once()) | ||
118 | ->method('getManagerForClass') | ||
119 | ->with('WallabagUserBundle:User') | ||
120 | ->will($this->returnValue($em)); | ||
121 | |||
122 | $params = new ParamConverter(['class' => 'WallabagUserBundle:User']); | ||
123 | $converter = new UsernameRssTokenConverter($registry); | ||
124 | |||
125 | $this->assertTrue($converter->supports($params)); | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * @expectedException InvalidArgumentException | ||
130 | * @expectedExceptionMessage Route attribute is missing | ||
131 | */ | ||
132 | public function testApplyEmptyRequest() | ||
133 | { | ||
134 | $params = new ParamConverter([]); | ||
135 | $converter = new UsernameRssTokenConverter(); | ||
136 | |||
137 | $converter->apply(new Request(), $params); | ||
138 | } | ||
139 | |||
140 | /** | ||
141 | * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException | ||
142 | * @expectedExceptionMessage User not found | ||
143 | */ | ||
144 | public function testApplyUserNotFound() | ||
145 | { | ||
146 | $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') | ||
147 | ->disableOriginalConstructor() | ||
148 | ->getMock(); | ||
149 | |||
150 | $repo->expects($this->once()) | ||
151 | ->method('findOneByUsernameAndRsstoken') | ||
152 | ->with('test', 'test') | ||
153 | ->will($this->returnValue(null)); | ||
154 | |||
155 | $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager') | ||
156 | ->disableOriginalConstructor() | ||
157 | ->getMock(); | ||
158 | |||
159 | $em->expects($this->once()) | ||
160 | ->method('getRepository') | ||
161 | ->with('WallabagUserBundle:User') | ||
162 | ->will($this->returnValue($repo)); | ||
163 | |||
164 | $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') | ||
165 | ->disableOriginalConstructor() | ||
166 | ->getMock(); | ||
167 | |||
168 | $registry->expects($this->once()) | ||
169 | ->method('getManagerForClass') | ||
170 | ->with('WallabagUserBundle:User') | ||
171 | ->will($this->returnValue($em)); | ||
172 | |||
173 | $params = new ParamConverter(['class' => 'WallabagUserBundle:User']); | ||
174 | $converter = new UsernameRssTokenConverter($registry); | ||
175 | $request = new Request([], [], ['username' => 'test', 'token' => 'test']); | ||
176 | |||
177 | $converter->apply($request, $params); | ||
178 | } | ||
179 | |||
180 | public function testApplyUserFound() | ||
181 | { | ||
182 | $user = new User(); | ||
183 | |||
184 | $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') | ||
185 | ->disableOriginalConstructor() | ||
186 | ->getMock(); | ||
187 | |||
188 | $repo->expects($this->once()) | ||
189 | ->method('findOneByUsernameAndRsstoken') | ||
190 | ->with('test', 'test') | ||
191 | ->will($this->returnValue($user)); | ||
192 | |||
193 | $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager') | ||
194 | ->disableOriginalConstructor() | ||
195 | ->getMock(); | ||
196 | |||
197 | $em->expects($this->once()) | ||
198 | ->method('getRepository') | ||
199 | ->with('WallabagUserBundle:User') | ||
200 | ->will($this->returnValue($repo)); | ||
201 | |||
202 | $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') | ||
203 | ->disableOriginalConstructor() | ||
204 | ->getMock(); | ||
205 | |||
206 | $registry->expects($this->once()) | ||
207 | ->method('getManagerForClass') | ||
208 | ->with('WallabagUserBundle:User') | ||
209 | ->will($this->returnValue($em)); | ||
210 | |||
211 | $params = new ParamConverter(['class' => 'WallabagUserBundle:User', 'name' => 'user']); | ||
212 | $converter = new UsernameRssTokenConverter($registry); | ||
213 | $request = new Request([], [], ['username' => 'test', 'token' => 'test']); | ||
214 | |||
215 | $converter->apply($request, $params); | ||
216 | |||
217 | $this->assertEquals($user, $request->attributes->get('user')); | ||
218 | } | ||
219 | } | ||