Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HttpMethodValidatorTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
13 use PHPUnit\Framework\TestCase;
15 use Magento\Framework\App\Request\Http as HttpRequest;
16 
17 class HttpMethodValidatorTest extends TestCase
18 {
22  private $validator;
23 
27  private $request;
28 
32  private $map;
33 
37  protected function setUp()
38  {
40  $this->validator = $objectManager->get(HttpMethodValidator::class);
41  $this->request = $objectManager->get(RequestInterface::class);
42  if (!$this->request instanceof HttpRequest) {
43  throw new \RuntimeException('We need HTTP request');
44  }
45  $this->map = $objectManager->get(HttpMethodMap::class);
46  }
47 
51  private function getMap(): array
52  {
53  $map = $this->map->getMap();
54  if (count($map) < 2) {
55  throw new \RuntimeException(
56  'We need at least 2 HTTP methods allowed'
57  );
58  }
59 
60  $sorted = [];
61  foreach ($map as $method => $interface) {
62  $sorted[] = ['method' => $method, 'interface' => $interface];
63  }
64 
65  return $sorted;
66  }
67 
73  public function testAllowed()
74  {
75  $map = $this->getMap();
76 
77  $action1 = $this->getMockForAbstractClass($map[0]['interface']);
78  $this->request->setMethod($map[0]['method']);
79  $this->validator->validate($this->request, $action1);
80 
81  $action2 = $this->getMockForAbstractClass(ActionInterface::class);
82  $this->validator->validate($this->request, $action2);
83  }
84 
90  public function testNotAllowedMethod()
91  {
92  $this->request->setMethod('method' .rand(0, 1000));
93  $action = $this->getMockForAbstractClass(ActionInterface::class);
94 
95  $this->validator->validate($this->request, $action);
96  }
97 
101  public function testRestrictedMethod()
102  {
103  $map = $this->getMap();
104 
105  $this->request->setMethod($map[1]['method']);
106  $action = $this->getMockForAbstractClass($map[0]['interface']);
107 
108  $this->validator->validate($this->request, $action);
109  }
110 }
$objectManager
Definition: bootstrap.php:17
$method
Definition: info.phtml:13