TraceableValueResolverTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\TraceableValueResolver;
  14. use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
  15. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  16. use Symfony\Component\Stopwatch\Stopwatch;
  17. class TraceableValueResolverTest extends TestCase
  18. {
  19. public function testTimingsInSupports()
  20. {
  21. $stopwatch = new Stopwatch();
  22. $resolver = new TraceableValueResolver(new ResolverStub(), $stopwatch);
  23. $argument = new ArgumentMetadata('dummy', 'string', false, false, null);
  24. $request = new Request();
  25. $this->assertTrue($resolver->supports($request, $argument));
  26. $event = $stopwatch->getEvent(ResolverStub::class.'::supports');
  27. $this->assertCount(1, $event->getPeriods());
  28. }
  29. public function testTimingsInResolve()
  30. {
  31. $stopwatch = new Stopwatch();
  32. $resolver = new TraceableValueResolver(new ResolverStub(), $stopwatch);
  33. $argument = new ArgumentMetadata('dummy', 'string', false, false, null);
  34. $request = new Request();
  35. $iterable = $resolver->resolve($request, $argument);
  36. foreach ($iterable as $index => $resolved) {
  37. $event = $stopwatch->getEvent(ResolverStub::class.'::resolve');
  38. $this->assertTrue($event->isStarted());
  39. $this->assertEmpty($event->getPeriods());
  40. switch ($index) {
  41. case 0:
  42. $this->assertEquals('first', $resolved);
  43. break;
  44. case 1:
  45. $this->assertEquals('second', $resolved);
  46. break;
  47. }
  48. }
  49. $event = $stopwatch->getEvent(ResolverStub::class.'::resolve');
  50. $this->assertCount(1, $event->getPeriods());
  51. }
  52. }
  53. class ResolverStub implements ArgumentValueResolverInterface
  54. {
  55. public function supports(Request $request, ArgumentMetadata $argument)
  56. {
  57. return true;
  58. }
  59. public function resolve(Request $request, ArgumentMetadata $argument)
  60. {
  61. yield 'first';
  62. yield 'second';
  63. }
  64. }