88namespace Magento \GraphQl \GraphQlCache ;
99
1010use Magento \Customer \Test \Fixture \Customer ;
11+ use Magento \Framework \Registry ;
1112use Magento \TestFramework \Fixture \DataFixture ;
1213use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
1314use Magento \TestFramework \TestCase \GraphQlAbstract ;
15+ use Magento \Framework \App \FrontControllerInterface ;
16+ use Magento \Framework \App \Request \Http ;
17+ use Magento \Framework \App \Response \Http as ResponseHttp ;
18+ use Magento \GraphQl \Controller \HttpRequestProcessor ;
19+ use Magento \GraphQlCache \Controller \Plugin \GraphQl ;
20+ use Magento \GraphQlCache \Model \CacheableQuery ;
21+ use Magento \GraphQlCache \Model \CacheId \CacheIdCalculator ;
22+ use Magento \PageCache \Model \Config ;
23+ use PHPUnit \Framework \MockObject \MockObject ;
24+ use Psr \Log \LoggerInterface ;
1425
1526class GraphQlTest extends GraphQlAbstract
1627{
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 HttpRequestProcessor|MockObject
45+ */
46+ private $ requestProcessorMock ;
47+
48+ /**
49+ * @var CacheIdCalculator|MockObject
50+ */
51+ private $ cacheIdCalculatorMock ;
52+
53+ /**
54+ * @var LoggerInterface|MockObject
55+ */
56+ private $ loggerMock ;
57+
58+ /**
59+ * @var FrontControllerInterface|MockObject
60+ */
61+ private $ subjectMock ;
62+
63+ /**
64+ * @var Http|MockObject
65+ */
66+ private $ requestMock ;
67+
68+ /**
69+ * @var Registry
70+ */
71+ private $ registryMock ;
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 ->registryMock = $ this ->createMock (Registry::class);
87+ $ this ->subjectMock = $ this ->createMock (FrontControllerInterface::class);
88+ $ this ->requestMock = $ this
89+ ->getMockBuilder (Http::class)
90+ ->disableOriginalConstructor ()
91+ ->getMockForAbstractClass ();
92+ $ this ->graphql = new GraphQl (
93+ $ this ->cacheableQueryMock ,
94+ $ this ->cacheIdCalculatorMock ,
95+ $ this ->configMock ,
96+ $ this ->loggerMock ,
97+ $ this ->requestProcessorMock ,
98+ $ this ->registryMock
99+ );
100+ }
17101 #[
18102 DataFixture(Customer::class, as: 'customer ' ),
19103 ]
@@ -40,4 +124,33 @@ public function testMutation(): void
40124 $ tokenResponse ['headers ' ]['Cache-Control ' ]
41125 );
42126 }
127+
128+ public function testBeforeDispatch (): void
129+ {
130+ $ this ->requestProcessorMock
131+ ->expects ($ this ->once ())
132+ ->method ('validateRequest ' );
133+ $ this ->requestProcessorMock
134+ ->expects ($ this ->once ())
135+ ->method ('processHeaders ' );
136+ $ this ->loggerMock
137+ ->expects ($ this ->never ())
138+ ->method ('critical ' );
139+ $ this ->assertNull ($ this ->graphql ->beforeDispatch ($ this ->subjectMock , $ this ->requestMock ));
140+ }
141+
142+ public function testBeforeDispatchForException (): void
143+ {
144+ $ this ->requestProcessorMock
145+ ->expects ($ this ->once ())
146+ ->method ('validateRequest ' )
147+ ->willThrowException (new \Exception ('Invalid Headers ' ));
148+ $ this ->requestProcessorMock
149+ ->expects ($ this ->never ())
150+ ->method ('processHeaders ' );
151+ $ this ->loggerMock
152+ ->expects ($ this ->once ())
153+ ->method ('critical ' );
154+ $ this ->assertNull ($ this ->graphql ->beforeDispatch ($ this ->subjectMock , $ this ->requestMock ));
155+ }
43156}
0 commit comments