8
8
namespace Magento \GraphQl \GraphQlCache ;
9
9
10
10
use Magento \Customer \Test \Fixture \Customer ;
11
+ use Magento \Framework \App \RequestInterface ;
12
+ use Magento \Framework \Registry ;
11
13
use Magento \TestFramework \Fixture \DataFixture ;
12
14
use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
13
15
use Magento \TestFramework \TestCase \GraphQlAbstract ;
16
+ use Magento \Framework \App \FrontControllerInterface ;
17
+ use Magento \Framework \App \Request \Http ;
18
+ use Magento \Framework \App \Response \Http as ResponseHttp ;
19
+ use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
20
+ use Magento \GraphQl \Controller \HttpRequestProcessor ;
21
+ use Magento \GraphQlCache \Controller \Plugin \GraphQl ;
22
+ use Magento \GraphQlCache \Model \CacheableQuery ;
23
+ use Magento \GraphQlCache \Model \CacheId \CacheIdCalculator ;
24
+ use Magento \PageCache \Model \Config ;
25
+ use PHPUnit \Framework \MockObject \MockObject ;
26
+ use PHPUnit \Framework \TestCase ;
27
+ use Psr \Log \LoggerInterface ;
28
+
14
29
15
30
class GraphQlTest extends GraphQlAbstract
16
31
{
32
+
33
+ /**
34
+ * @var GraphQl
35
+ */
36
+ private $ graphql ;
37
+
38
+ /**
39
+ * @var CacheableQuery|MockObject
40
+ */
41
+ private $ cacheableQueryMock ;
42
+
43
+ /**
44
+ * @var Config|MockObject
45
+ */
46
+ private $ configMock ;
47
+
48
+ /**
49
+ * @var ResponseHttp|MockObject
50
+ */
51
+ private $ responseMock ;
52
+
53
+ /**
54
+ * @var HttpRequestProcessor|MockObject
55
+ */
56
+ private $ requestProcessorMock ;
57
+
58
+ /**
59
+ * @var CacheIdCalculator|MockObject
60
+ */
61
+ private $ cacheIdCalculatorMock ;
62
+
63
+ /**
64
+ * @var LoggerInterface|MockObject
65
+ */
66
+ private $ loggerMock ;
67
+
68
+ /**
69
+ * @var FrontControllerInterface|MockObject
70
+ */
71
+ private $ subjectMock ;
72
+
73
+ /**
74
+ * @var Http|MockObject
75
+ */
76
+ private $ requestMock ;
77
+
78
+ /**
79
+ * @var Registry
80
+ */
81
+ private $ registryMock ;
82
+
83
+ protected function setUp (): void
84
+ {
85
+ $ this ->cacheableQueryMock = $ this ->createMock (CacheableQuery::class);
86
+ $ this ->cacheIdCalculatorMock = $ this ->createMock (CacheIdCalculator::class);
87
+ $ this ->configMock = $ this ->createMock (Config::class);
88
+ $ this ->loggerMock = $ this ->getMockBuilder (LoggerInterface::class)
89
+ ->onlyMethods (['critical ' ])
90
+ ->disableOriginalConstructor ()
91
+ ->getMockForAbstractClass ();
92
+ $ this ->requestProcessorMock = $ this ->getMockBuilder (HttpRequestProcessor::class)
93
+ ->onlyMethods (['validateRequest ' ,'processHeaders ' ])
94
+ ->disableOriginalConstructor ()
95
+ ->getMockForAbstractClass ();
96
+ $ this ->registryMock = $ this ->createMock (Registry::class);
97
+ $ this ->subjectMock = $ this ->createMock (FrontControllerInterface::class);
98
+ $ this ->requestMock = $ this
99
+ ->getMockBuilder (Http::class)
100
+ ->disableOriginalConstructor ()
101
+ ->getMockForAbstractClass ();
102
+ $ this ->graphql = new GraphQl (
103
+ $ this ->cacheableQueryMock ,
104
+ $ this ->cacheIdCalculatorMock ,
105
+ $ this ->configMock ,
106
+ $ this ->loggerMock ,
107
+ $ this ->requestProcessorMock ,
108
+ $ this ->registryMock
109
+ );
110
+ }
17
111
#[
18
112
DataFixture(Customer::class, as: 'customer ' ),
19
113
]
@@ -40,4 +134,35 @@ public function testMutation(): void
40
134
$ tokenResponse ['headers ' ]['Cache-Control ' ]
41
135
);
42
136
}
137
+
138
+ public function testBeforeDispatch (): void
139
+ {
140
+ $ this ->requestProcessorMock
141
+ ->expects ($ this ->once ())
142
+ ->method ('validateRequest ' );
143
+ $ this ->requestProcessorMock
144
+ ->expects ($ this ->once ())
145
+ ->method ('processHeaders ' );
146
+ $ this ->loggerMock
147
+ ->expects ($ this ->never ())
148
+ ->method ('critical ' );
149
+ $ this ->assertNull ($ this ->graphql ->beforeDispatch ($ this ->subjectMock , $ this ->requestMock ));
150
+ }
151
+
152
+ public function testBeforeDispatchForException (): void
153
+ {
154
+ $ this ->requestProcessorMock
155
+ ->expects ($ this ->once ())
156
+ ->method ('validateRequest ' )
157
+ ->willThrowException (new \Exception ('Invalid Headers ' ));
158
+ $ this ->requestProcessorMock
159
+ ->expects ($ this ->never ())
160
+ ->method ('processHeaders ' );
161
+ $ this ->loggerMock
162
+ ->expects ($ this ->once ())
163
+ ->method ('critical ' );
164
+ $ this ->assertNull ($ this ->graphql ->beforeDispatch ($ this ->subjectMock , $ this ->requestMock ));
165
+ }
166
+
167
+
43
168
}
0 commit comments