PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/friendsofsymfony/rest-bundle/Tests/Request/ParamFetcherTest.php

https://gitlab.com/cuza/Clinic_Recods
PHP | 440 lines | 313 code | 66 blank | 61 comment | 1 complexity | b0a527103335dc30303481827b63ea33 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the FOSRestBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\RestBundle\Tests\Request;
  11. use Doctrine\Common\Util\ClassUtils;
  12. use FOS\RestBundle\Request\ParamFetcher;
  13. use FOS\RestBundle\Request\ParamReaderInterface;
  14. use FOS\RestBundle\Validator\ViolationFormatterInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\Validator\ConstraintViolationListInterface;
  18. use Symfony\Component\Validator\Validator\ValidatorInterface;
  19. /**
  20. * ParamFetcher test.
  21. *
  22. * @author Alexander <iam.asm89@gmail.com>
  23. * @author Boris Guéry <guery.b@gmail.com>
  24. */
  25. class ParamFetcherTest extends \PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * @var callable
  29. */
  30. private $controller;
  31. /**
  32. * @var ParamReaderInterface
  33. */
  34. private $paramReader;
  35. /**
  36. * @var ParamFetcherTest|ValidatorInterface
  37. */
  38. private $validator;
  39. /**
  40. * @var ViolationFormatterInterface
  41. */
  42. private $violationFormatter;
  43. /**
  44. * @var RequestStack
  45. */
  46. private $requestStack;
  47. /**
  48. * Test setup.
  49. */
  50. public function setup()
  51. {
  52. $this->controller = [new \stdClass(), 'fooAction'];
  53. $this->params = [];
  54. $this->paramReader = $this->getMock(ParamReaderInterface::class);
  55. $this->validator = $this->getMock(ValidatorInterface::class);
  56. $this->violationFormatter = $this->getMock(ViolationFormatterInterface::class);
  57. $this->request = new Request();
  58. $this->requestStack = $this->getMock(RequestStack::class, array());
  59. $this->requestStack
  60. ->expects($this->any())
  61. ->method('getCurrentRequest')
  62. ->willReturn($this->request);
  63. $this->paramFetcherBuilder = $this->getMockBuilder(ParamFetcher::class);
  64. $this->paramFetcherBuilder
  65. ->setConstructorArgs(array(
  66. $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'),
  67. $this->paramReader,
  68. $this->requestStack,
  69. $this->violationFormatter,
  70. $this->validator,
  71. ))
  72. ->setMethods(null);
  73. $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  74. }
  75. public function testParamDynamicCreation()
  76. {
  77. $fetcher = $this->paramFetcherBuilder->getMock();
  78. $fetcher->setController($this->controller);
  79. $param1 = $this->createMockedParam('foo');
  80. $param2 = $this->createMockedParam('foobar');
  81. $param3 = $this->createMockedParam('bar');
  82. $this->setParams(array($param1)); // Controller params
  83. $fetcher->addParam($param2);
  84. $fetcher->addParam($param3);
  85. $this->assertEquals(array('foo' => $param1, 'foobar' => $param2, 'bar' => $param3), $fetcher->getParams());
  86. }
  87. /**
  88. * @expectedException \InvalidArgumentException
  89. * @expectedExceptionMessage No @ParamInterface configuration for parameter 'foo'.
  90. */
  91. public function testInexistentParam()
  92. {
  93. $fetcher = $this->paramFetcherBuilder
  94. ->setMethods(array('getParams'))
  95. ->getMock();
  96. $fetcher
  97. ->expects($this->once())
  98. ->method('getParams')
  99. ->willReturn(array(
  100. 'bar' => $this->createMockedParam('bar'),
  101. ));
  102. $fetcher->get('foo');
  103. }
  104. public function testDefaultReplacement()
  105. {
  106. $fetcher = $this->paramFetcherBuilder
  107. ->setMethods(['getParams', 'cleanParamWithRequirements'])
  108. ->getMock();
  109. $param = $this->createMockedParam('foo', 'bar'); // Default value: bar
  110. $fetcher
  111. ->expects($this->once())
  112. ->method('getParams')
  113. ->willReturn(['foo' => $param]);
  114. $fetcher
  115. ->expects($this->once())
  116. ->method('cleanParamWithRequirements')
  117. ->with($param, 'bar', true)
  118. ->willReturn('foooo');
  119. $this->assertEquals('foooo', $fetcher->get('foo', true));
  120. }
  121. public function testReturnBeforeGettingConstraints()
  122. {
  123. $param = $this->getMock(\FOS\RestBundle\Controller\Annotations\ParamInterface::class);
  124. $param
  125. ->expects($this->once())
  126. ->method('getDefault')
  127. ->willReturn('default');
  128. $param
  129. ->expects($this->never())
  130. ->method('getConstraints');
  131. list($fetcher, $method) = $this->getFetcherToCheckValidation($param);
  132. $this->assertEquals(
  133. 'default',
  134. $method->invokeArgs($fetcher, array($param, 'default', null))
  135. );
  136. }
  137. public function testReturnWhenEmptyConstraints()
  138. {
  139. $param = $this->createMockedParam('foo');
  140. list($fetcher, $method) = $this->getFetcherToCheckValidation($param);
  141. $this->assertEquals(
  142. 'value',
  143. $method->invokeArgs($fetcher, array($param, 'value', null))
  144. );
  145. }
  146. /**
  147. * @expectedException \RuntimeException
  148. * @expectedExceptionMessage The ParamFetcher requirements feature requires the symfony/validator component.
  149. */
  150. public function testEmptyValidator()
  151. {
  152. $param = $this->createMockedParam('none', null, array(), false, null, array('constraint'));
  153. $this->setParams([$param]);
  154. list($fetcher, $method) = $this->getFetcherToCheckValidation(
  155. $param,
  156. array(
  157. $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'),
  158. $this->paramReader,
  159. $this->requestStack,
  160. $this->violationFormatter,
  161. null,
  162. )
  163. );
  164. $fetcher->setController($this->controller);
  165. $fetcher->get('none', '42');
  166. }
  167. public function testNoValidationErrors()
  168. {
  169. $param = $this->createMockedParam('foo', null, array(), false, null, array('constraint'));
  170. list($fetcher, $method) = $this->getFetcherToCheckValidation($param);
  171. $this->validator
  172. ->expects($this->once())
  173. ->method('validate')
  174. ->with('value', array('constraint'))
  175. ->willReturn(array());
  176. $this->assertEquals('value', $method->invokeArgs($fetcher, array($param, 'value', null)));
  177. }
  178. public function testValidationErrors()
  179. {
  180. $param = $this->createMockedParam('foo', 'default', [], false, null, ['constraint']);
  181. list($fetcher, $method) = $this->getFetcherToCheckValidation($param);
  182. $errors = $this->getMock(ConstraintViolationListInterface::class);
  183. $errors
  184. ->expects($this->once())
  185. ->method('count')
  186. ->willReturn(1);
  187. $this->validator
  188. ->expects($this->once())
  189. ->method('validate')
  190. ->with('value', ['constraint'])
  191. ->willReturn($errors);
  192. $this->assertEquals('default', $method->invokeArgs($fetcher, array($param, 'value', false)));
  193. }
  194. /**
  195. * @expectedException Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  196. * @expectedMessage expected exception.
  197. */
  198. public function testValidationErrorsInStrictMode()
  199. {
  200. $param = $this->createMockedParam('foo', null, [], false, null, ['constraint']);
  201. list($fetcher, $method) = $this->getFetcherToCheckValidation($param);
  202. $errors = $this->getMock(ConstraintViolationListInterface::class);
  203. $errors
  204. ->expects($this->once())
  205. ->method('count')
  206. ->willReturn(1);
  207. $this->validator
  208. ->expects($this->once())
  209. ->method('validate')
  210. ->with('value', array('constraint'))
  211. ->willReturn($errors);
  212. $this->violationFormatter
  213. ->expects($this->once())
  214. ->method('formatList')
  215. ->with($param, $errors)
  216. ->willReturn('expected exception.');
  217. $method->invokeArgs($fetcher, array($param, 'value', true));
  218. }
  219. protected function getFetcherToCheckValidation($param, array $constructionArguments = null)
  220. {
  221. $this->paramFetcherBuilder->setMethods(array('checkNotIncompatibleParams'));
  222. if (null !== $constructionArguments) {
  223. $this->paramFetcherBuilder->setConstructorArgs($constructionArguments);
  224. }
  225. $fetcher = $this->paramFetcherBuilder->getMock();
  226. $fetcher
  227. ->expects($this->once())
  228. ->method('checkNotIncompatibleParams')
  229. ->with($param);
  230. $reflection = new \ReflectionClass($fetcher);
  231. $method = $reflection->getMethod('cleanParamWithRequirements');
  232. $method->setAccessible(true);
  233. return [$fetcher, $method];
  234. }
  235. public function testAllGetter()
  236. {
  237. $fetcher = $this->paramFetcherBuilder
  238. ->setMethods(array('getParams', 'get'))
  239. ->getMock();
  240. $fetcher
  241. ->expects($this->once())
  242. ->method('getParams')
  243. ->willReturn(array(
  244. 'foo' => $this->createMockedParam('foo', null, array(), true), // strict
  245. 'bar' => $this->createMockedParam('bar'),
  246. ));
  247. $fetcher
  248. ->expects($this->exactly(2))
  249. ->method('get')
  250. ->with(
  251. $this->logicalOr('foo', 'bar'),
  252. $this->logicalOr(true, false)
  253. )
  254. ->will($this->onConsecutiveCalls('first', 'second'));
  255. $this->assertEquals(array('foo' => 'first', 'bar' => 'second'), $fetcher->all());
  256. }
  257. /**
  258. * @expectedException InvalidArgumentException
  259. * @expectedExceptionMessage Controller and method needs to be set via setController
  260. */
  261. public function testEmptyControllerExceptionWhenInitParams()
  262. {
  263. $fetcher = $this->paramFetcherBuilder->getMock();
  264. $fetcher->all();
  265. }
  266. /**
  267. * @expectedException \InvalidArgumentException
  268. * @expectedExceptionMessage Controller needs to be set as a class instance (closures/functions are not supported)
  269. * @dataProvider invalidControllerProvider
  270. */
  271. public function testNotCallableControllerExceptionWhenInitParams($controller)
  272. {
  273. $fetcher = $this->paramFetcherBuilder->getMock();
  274. $fetcher->setController($controller);
  275. $fetcher->all();
  276. }
  277. public function invalidControllerProvider()
  278. {
  279. return [
  280. ['controller'],
  281. [[null, 'foo']],
  282. [['Foo', null]],
  283. ];
  284. }
  285. /**
  286. * @expectedException \InvalidArgumentException
  287. * @expectedExceptionMessage No @ParamInterface configuration for parameter 'foobar'.
  288. */
  289. public function testInexistentIncompatibleParam()
  290. {
  291. $fetcher = $this->paramFetcherBuilder
  292. ->setMethods(array('getParams'))
  293. ->getMock();
  294. $fetcher
  295. ->expects($this->once())
  296. ->method('getParams')
  297. ->willReturn(array('foo' => $this->createMockedParam('foo')));
  298. $param = $this->createMockedParam('bar', null, array('foobar', 'fos')); // Incompatible with foobar & fos
  299. $reflection = new \ReflectionClass($fetcher);
  300. $method = $reflection->getMethod('checkNotIncompatibleParams');
  301. $method->setAccessible(true);
  302. $method->invokeArgs($fetcher, array($param));
  303. }
  304. /**
  305. * @expectedException Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  306. * @expectedExceptionMessage 'bar' param is incompatible with fos param.
  307. */
  308. public function testIncompatibleParam()
  309. {
  310. $fetcher = $this->paramFetcherBuilder
  311. ->setMethods(array('getParams'))
  312. ->getMock();
  313. $fetcher
  314. ->expects($this->once())
  315. ->method('getParams')
  316. ->willReturn(array(
  317. 'foobar' => $this->createMockedParam('foobar'),
  318. 'fos' => $this->createMockedParam('fos', null, array(), false, 'value'),
  319. ));
  320. $param = $this->createMockedParam('bar', null, array('foobar', 'fos')); // Incompatible with foobar & fos
  321. $reflection = new \ReflectionClass($fetcher);
  322. $method = $reflection->getMethod('checkNotIncompatibleParams');
  323. $method->setAccessible(true);
  324. $method->invokeArgs($fetcher, array($param));
  325. }
  326. protected function setParams(array $params = array())
  327. {
  328. $newParams = array();
  329. foreach ($params as $param) {
  330. $newParams[$param->getName()] = $param;
  331. }
  332. $this->paramReader
  333. ->expects($this->any())
  334. ->method('read')
  335. ->with(new \ReflectionClass(ClassUtils::getClass($this->controller[0])), $this->controller[1])
  336. ->willReturn($newParams);
  337. }
  338. protected function createMockedParam(
  339. $name,
  340. $default = null,
  341. array $incompatibles = [],
  342. $strict = false,
  343. $value = null,
  344. array $constraints = []
  345. ) {
  346. $param = $this->getMock('FOS\RestBundle\Controller\Annotations\ParamInterface');
  347. $param
  348. ->expects($this->any())
  349. ->method('getName')
  350. ->willReturn($name);
  351. $param
  352. ->expects($this->any())
  353. ->method('getDefault')
  354. ->willReturn($default);
  355. $param
  356. ->expects($this->any())
  357. ->method('getIncompatibilities')
  358. ->willReturn($incompatibles);
  359. $param
  360. ->expects($this->any())
  361. ->method('getConstraints')
  362. ->willReturn($constraints);
  363. $param
  364. ->expects($this->any())
  365. ->method('isStrict')
  366. ->willReturn($strict);
  367. $param
  368. ->expects($this->any())
  369. ->method('getValue')
  370. ->with($this->request, $default)
  371. ->will($value !== null ? $this->returnValue($value) : $this->returnArgument(1));
  372. return $param;
  373. }
  374. }