Skip to content

Commit 0eb5733

Browse files
committed
Merge remote-tracking branch 'origin/AC-821' into spartans_pr_18042023
2 parents 4f26093 + 81a6323 commit 0eb5733

File tree

2 files changed

+141
-9
lines changed

2 files changed

+141
-9
lines changed

app/code/Magento/GraphQlCache/Controller/Plugin/GraphQl.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\GraphQlCache\Controller\Plugin;
99

1010
use Magento\Framework\App\FrontControllerInterface;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\App\RequestInterface;
1213
use Magento\Framework\App\Response\Http as ResponseHttp;
1314
use Magento\Framework\Controller\ResultInterface;
@@ -16,9 +17,11 @@
1617
use Magento\GraphQlCache\Model\CacheableQuery;
1718
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
1819
use Magento\PageCache\Model\Config;
20+
use Psr\Log\LoggerInterface;
1921

2022
/**
2123
* Plugin for handling controller after controller tags and pre-controller validation.
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2225
*/
2326
class GraphQl
2427
{
@@ -52,28 +55,37 @@ class GraphQl
5255
*/
5356
private $cacheIdCalculator;
5457

58+
/**
59+
* @var LoggerInterface $logger
60+
*/
61+
private $logger;
62+
5563
/**
5664
* @param CacheableQuery $cacheableQuery
65+
* @param CacheIdCalculator $cacheIdCalculator
5766
* @param Config $config
58-
* @param ResponseHttp $response
67+
* @param LoggerInterface $logger
5968
* @param HttpRequestProcessor $requestProcessor
69+
* @param ResponseHttp $response
6070
* @param Registry $registry
61-
* @param CacheIdCalculator $cacheIdCalculator
6271
*/
6372
public function __construct(
6473
CacheableQuery $cacheableQuery,
74+
CacheIdCalculator $cacheIdCalculator,
6575
Config $config,
66-
ResponseHttp $response,
76+
LoggerInterface $logger,
6777
HttpRequestProcessor $requestProcessor,
68-
Registry $registry,
69-
CacheIdCalculator $cacheIdCalculator
78+
ResponseHttp $response,
79+
Registry $registry = null
7080
) {
7181
$this->cacheableQuery = $cacheableQuery;
82+
$this->cacheIdCalculator = $cacheIdCalculator;
7283
$this->config = $config;
73-
$this->response = $response;
84+
$this->logger = $logger;
7485
$this->requestProcessor = $requestProcessor;
75-
$this->registry = $registry;
76-
$this->cacheIdCalculator = $cacheIdCalculator;
86+
$this->response = $response;
87+
$this->registry = $registry ?: ObjectManager::getInstance()
88+
->get(Registry::class);
7789
}
7890

7991
/**
@@ -87,7 +99,12 @@ public function __construct(
8799
public function beforeDispatch(
88100
FrontControllerInterface $subject,
89101
RequestInterface $request
90-
) {
102+
): void {
103+
try {
104+
$this->requestProcessor->validateRequest($request);
105+
} catch (\Exception $error) {
106+
$this->logger->critical($error->getMessage());
107+
}
91108
/** @var \Magento\Framework\App\Request\Http $request */
92109
$this->requestProcessor->processHeaders($request);
93110
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\GraphQlCache\Test\Unit\Controller\Plugin;
9+
10+
use Magento\Framework\App\FrontControllerInterface;
11+
use Magento\Framework\App\Request\Http;
12+
use Magento\Framework\App\Response\Http as ResponseHttp;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\GraphQl\Controller\HttpRequestProcessor;
15+
use Magento\GraphQlCache\Controller\Plugin\GraphQl;
16+
use Magento\GraphQlCache\Model\CacheableQuery;
17+
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
18+
use Magento\PageCache\Model\Config;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use PHPUnit\Framework\TestCase;
21+
use Psr\Log\LoggerInterface;
22+
23+
/**
24+
* Test beforeDispatch
25+
*/
26+
class GraphQlTest extends TestCase
27+
{
28+
/**
29+
* @var GraphQl
30+
*/
31+
private $graphql;
32+
33+
/**
34+
* @var CacheableQuery|MockObject
35+
*/
36+
private $cacheableQueryMock;
37+
38+
/**
39+
* @var Config|MockObject
40+
*/
41+
private $configMock;
42+
43+
/**
44+
* @var ResponseHttp|MockObject
45+
*/
46+
private $responseMock;
47+
48+
/**
49+
* @var HttpRequestProcessor|MockObject
50+
*/
51+
private $requestProcessorMock;
52+
53+
/**
54+
* @var CacheIdCalculator|MockObject
55+
*/
56+
private $cacheIdCalculatorMock;
57+
58+
/**
59+
* @var LoggerInterface|MockObject
60+
*/
61+
private $loggerMock;
62+
63+
/**
64+
* @var FrontControllerInterface|MockObject
65+
*/
66+
private $subjectMock;
67+
68+
/**
69+
* @var Http|MockObject
70+
*/
71+
private $requestMock;
72+
73+
protected function setUp(): void
74+
{
75+
$this->cacheableQueryMock = $this->createMock(CacheableQuery::class);
76+
$this->cacheIdCalculatorMock = $this->createMock(CacheIdCalculator::class);
77+
$this->configMock = $this->createMock(Config::class);
78+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
79+
->onlyMethods(['critical'])
80+
->disableOriginalConstructor()
81+
->getMockForAbstractClass();
82+
$this->requestProcessorMock = $this->getMockBuilder(HttpRequestProcessor::class)
83+
->onlyMethods(['validateRequest','processHeaders'])
84+
->disableOriginalConstructor()
85+
->getMockForAbstractClass();
86+
$this->responseMock = $this->createMock(ResponseHttp::class);
87+
$this->subjectMock = $this->createMock(FrontControllerInterface::class);
88+
$this->requestMock = $this->createMock(Http::class);
89+
$this->graphql = new GraphQl(
90+
$this->cacheableQueryMock,
91+
$this->cacheIdCalculatorMock,
92+
$this->configMock,
93+
$this->loggerMock,
94+
$this->requestProcessorMock,
95+
$this->responseMock
96+
);
97+
}
98+
99+
/**
100+
* test beforeDispatch function for validation purpose
101+
*/
102+
public function testBeforeDispatch(): void
103+
{
104+
$this->requestProcessorMock
105+
->expects($this->any())
106+
->method('validateRequest');
107+
$this->requestProcessorMock
108+
->expects($this->any())
109+
->method('processHeaders');
110+
$this->loggerMock
111+
->expects($this->any())
112+
->method('critical');
113+
$this->assertNull($this->graphql->beforeDispatch($this->subjectMock, $this->requestMock));
114+
}
115+
}

0 commit comments

Comments
 (0)