|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Test\Unit\Controller\HttpRequestValidator; |
| 9 | + |
| 10 | +use Magento\Framework\App\HttpRequestInterface; |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 12 | +use Magento\GraphQl\Controller\HttpRequestValidator\HttpVerbValidator; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test HttpVerbValidator |
| 18 | + */ |
| 19 | +class HttpVerbValidatorTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var HttpVerbValidator|MockObject |
| 23 | + */ |
| 24 | + private $httpVerbValidator; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var HttpRequestInterface|MockObject |
| 28 | + */ |
| 29 | + private $requestMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritDoc |
| 33 | + */ |
| 34 | + protected function setup(): void |
| 35 | + { |
| 36 | + $objectManager = new ObjectManager($this); |
| 37 | + $this->requestMock = $this->getMockBuilder(HttpRequestInterface::class) |
| 38 | + ->disableOriginalConstructor() |
| 39 | + ->onlyMethods( |
| 40 | + [ |
| 41 | + 'isPost', |
| 42 | + ] |
| 43 | + )->addMethods( |
| 44 | + [ |
| 45 | + 'getParam', |
| 46 | + ] |
| 47 | + ) |
| 48 | + ->getMockForAbstractClass(); |
| 49 | + |
| 50 | + $this->httpVerbValidator = $objectManager->getObject( |
| 51 | + HttpVerbValidator::class |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test for validate method |
| 57 | + * |
| 58 | + * @param string $query |
| 59 | + * @param bool $needException |
| 60 | + * @dataProvider validateDataProvider |
| 61 | + */ |
| 62 | + public function testValidate(string $query, bool $needException): void |
| 63 | + { |
| 64 | + $this->requestMock |
| 65 | + ->expects($this->once()) |
| 66 | + ->method('isPost') |
| 67 | + ->willReturn(false); |
| 68 | + |
| 69 | + $this->requestMock |
| 70 | + ->method('getParam') |
| 71 | + ->with('query', '') |
| 72 | + ->willReturn($query); |
| 73 | + |
| 74 | + if ($needException) { |
| 75 | + $this->expectExceptionMessage('Syntax Error: Unexpected <EOF>'); |
| 76 | + } |
| 77 | + |
| 78 | + $this->httpVerbValidator->validate($this->requestMock); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return array |
| 83 | + */ |
| 84 | + public function validateDataProvider(): array |
| 85 | + { |
| 86 | + return [ |
| 87 | + [ |
| 88 | + 'query' => '', |
| 89 | + 'needException' => false, |
| 90 | + ], |
| 91 | + [ |
| 92 | + 'query' => ' ', |
| 93 | + 'needException' => true |
| 94 | + ], |
| 95 | + ]; |
| 96 | + } |
| 97 | +} |
0 commit comments