|
| 1 | +<?php |
| 2 | +namespace Pitch\AdrBundle\Responder\Handler; |
| 3 | + |
| 4 | +use PHPUnit\Framework\TestCase; |
| 5 | +use Pitch\AdrBundle\Configuration\DefaultContentType; |
| 6 | +use Pitch\AdrBundle\Responder\ResponsePayloadEvent; |
| 7 | +use Symfony\Component\HttpFoundation\AcceptHeader; |
| 8 | +use Symfony\Component\HttpFoundation\Request; |
| 9 | + |
| 10 | +class AcceptPriorityTraitTest extends TestCase |
| 11 | +{ |
| 12 | + public function provideRequestSettings() |
| 13 | + { |
| 14 | + return [ |
| 15 | + 'default to 1' => [ |
| 16 | + 1, |
| 17 | + ], |
| 18 | + 'matching defaultContentType' => [ |
| 19 | + 1, |
| 20 | + ['foo/bar', 'foo/baz'], |
| 21 | + 'foo/baz', |
| 22 | + ], |
| 23 | + 'not matching defaultContentType' => [ |
| 24 | + null, |
| 25 | + ['foo/bar'], |
| 26 | + 'foo/baz', |
| 27 | + ], |
| 28 | + 'matching accept' => [ |
| 29 | + 0.8, |
| 30 | + ['foo/bar'], |
| 31 | + null, |
| 32 | + 'foo/baz;q=1,foo/bar;q=0.8', |
| 33 | + ], |
| 34 | + 'not matching accept' => [ |
| 35 | + null, |
| 36 | + ['foo/bar'], |
| 37 | + 'foo/bar', |
| 38 | + 'foo/baz', |
| 39 | + ], |
| 40 | + 'accept any matching defaultContentType' => [ |
| 41 | + 0.8, |
| 42 | + ['foo/bar'], |
| 43 | + 'foo/bar', |
| 44 | + 'foo/baz;q=1,foo/bar;q=0.1,*/*;q=0.8', |
| 45 | + ], |
| 46 | + 'accept any not matching defaultContentType' => [ |
| 47 | + 0, |
| 48 | + ['foo/bar'], |
| 49 | + 'foo/baz', |
| 50 | + 'foo/baz,foo/bar;q=0.1,*/*;q=0.8', |
| 51 | + ], |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @dataProvider provideRequestSettings |
| 57 | + */ |
| 58 | + public function testGetPriorityFromAcceptQuality( |
| 59 | + ?float $expectedPriority, |
| 60 | + array $supportedContentTypes = [], |
| 61 | + ?string $defaultContentType = null, |
| 62 | + ?string $acceptHeader = null |
| 63 | + ) { |
| 64 | + $handler = $this->getMockForTrait(AcceptPriorityTrait::class); |
| 65 | + $handler->method('getSupportedContentTypes')->willReturn($supportedContentTypes); |
| 66 | + /** @var AcceptPriorityTrait $handler */ |
| 67 | + |
| 68 | + $request = new Request(); |
| 69 | + if ($acceptHeader) { |
| 70 | + $request->headers->set('accept', $acceptHeader); |
| 71 | + } |
| 72 | + if ($defaultContentType) { |
| 73 | + $request->attributes->set( |
| 74 | + '_' . DefaultContentType::class, |
| 75 | + new DefaultContentType($defaultContentType) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + $event = new ResponsePayloadEvent(null, $request); |
| 80 | + |
| 81 | + $this->assertSame($expectedPriority, $handler->getResponseHandlerPriority($event)); |
| 82 | + } |
| 83 | + |
| 84 | + public function testStoreAccessHeaderOnRequestAttributes() |
| 85 | + { |
| 86 | + $handler = $this->getMockForTrait(AcceptPriorityTrait::class); |
| 87 | + $handler->method('getSupportedContentTypes')->willReturn(['foo/baz']); |
| 88 | + /** @var AcceptPriorityTrait $handler */ |
| 89 | + |
| 90 | + $request = new Request(); |
| 91 | + $request->headers->set('accept', 'foo/bar,foo/baz;q=0.2'); |
| 92 | + $event = new ResponsePayloadEvent(null, $request); |
| 93 | + |
| 94 | + $this->assertEquals(0.2, $handler->getResponseHandlerPriority($event)); |
| 95 | + |
| 96 | + /** @var AcceptHeader */ |
| 97 | + $attr = $request->attributes->get(AcceptHeader::class); |
| 98 | + $this->assertInstanceOf(AcceptHeader::class, $attr); |
| 99 | + $this->assertEquals(0.2, $attr->get('foo/baz')->getQuality()); |
| 100 | + |
| 101 | + $request->attributes->set(AcceptHeader::class, AcceptHeader::fromString('foo/baz;q=0.5')); |
| 102 | + |
| 103 | + $this->assertEquals(0.5, $handler->getResponseHandlerPriority($event)); |
| 104 | + } |
| 105 | +} |
0 commit comments