44
55namespace Overblog \GraphQLBundle \Tests \Functional ;
66
7- use Overblog \GraphQLBundle \ExpressionLanguage \ExpressionFunction ;
8- use Overblog \GraphQLBundle \Tests \Functional \App \TestKernel ;
9- use Symfony \Bundle \FrameworkBundle \KernelBrowser ;
10- use Symfony \Bundle \FrameworkBundle \Test \WebTestCase ;
117use Symfony \Component \DependencyInjection \Container ;
128use Symfony \Component \DependencyInjection \ContainerInterface ;
13- use Symfony \Component \Filesystem \Filesystem ;
14- use Symfony \Component \HttpFoundation \Request ;
159use Symfony \Component \HttpKernel \Kernel ;
16- use Symfony \Component \HttpKernel \KernelInterface ;
17-
18- use function call_user_func ;
19- use function func_get_args ;
20- use function implode ;
21- use function is_callable ;
22- use function json_decode ;
23- use function json_encode ;
24- use function sprintf ;
25- use function strtolower ;
26- use function sys_get_temp_dir ;
2710
2811if (Kernel::VERSION_ID < 70000 ) {
2912 abstract class TestCase extends BaseTestCase
@@ -44,153 +27,3 @@ protected static function getContainer(): Container
4427 }
4528 }
4629}
47-
48-
49- /**
50- * TestCase.
51- */
52- abstract class BaseTestCase extends WebTestCase
53- {
54- public const USER_RYAN = 'ryan ' ;
55- public const USER_ADMIN = 'admin ' ;
56- public const ANONYMOUS_USER = null ;
57- public const DEFAULT_PASSWORD = '123 ' ;
58-
59- /**
60- * {@inheritdoc}
61- */
62- protected static function getKernelClass (): string
63- {
64- return TestKernel::class;
65- }
66-
67- /**
68- * {@inheritdoc}
69- */
70- protected static function createKernel (array $ options = []): KernelInterface
71- {
72- if (null === static ::$ class ) {
73- static ::$ class = static ::getKernelClass ();
74- }
75-
76- $ options ['test_case ' ] ??= '' ;
77-
78- $ env = $ options ['environment ' ] ?? 'test ' . strtolower ($ options ['test_case ' ]);
79- $ debug = $ options ['debug ' ] ?? true ;
80-
81- return new static::$ class ($ env , $ debug , $ options ['test_case ' ]);
82- }
83-
84- /**
85- * {@inheritdoc}
86- */
87- public static function setUpBeforeClass (): void
88- {
89- $ fs = new Filesystem ();
90- $ fs ->remove (sys_get_temp_dir () . '/OverblogGraphQLBundle/ ' );
91- }
92-
93- protected function tearDown (): void
94- {
95- static ::ensureKernelShutdown ();
96- }
97-
98- protected static function executeGraphQLRequest (string $ query , array $ rootValue = [], string $ schemaName = null , array $ variables = []): array
99- {
100- $ request = new Request ();
101- $ request ->query ->set ('query ' , $ query );
102- if (!empty ($ variables )) {
103- $ request ->query ->set ('variables ' , $ variables );
104- }
105-
106- // @phpstan-ignore-next-line
107- $ req = static ::getContainer ()->get ('overblog_graphql.request_parser ' )->parse ($ request );
108- // @phpstan-ignore-next-line
109- $ res = static ::getContainer ()->get ('overblog_graphql.request_executor ' )->execute ($ schemaName , $ req , $ rootValue );
110-
111- return $ res ->toArray ();
112- }
113-
114- protected static function assertGraphQL (string $ query , array $ expectedData = null , array $ expectedErrors = null , array $ rootValue = [], string $ schemaName = null ): void
115- {
116- $ result = static ::executeGraphQLRequest ($ query , $ rootValue , $ schemaName );
117-
118- $ expected = [];
119-
120- if (null !== $ expectedErrors ) {
121- $ expected ['errors ' ] = $ expectedErrors ;
122- }
123-
124- if (null !== $ expectedData ) {
125- $ expected ['data ' ] = $ expectedData ;
126- }
127-
128- static ::assertSame ($ expected , $ result , json_encode ($ result ));
129- }
130-
131- protected static function query (string $ query , string $ username , string $ testCase , string $ password = self ::DEFAULT_PASSWORD ): KernelBrowser
132- {
133- $ client = static ::createClientAuthenticated ($ username , $ testCase , $ password );
134- $ client ->request ('GET ' , '/ ' , ['query ' => $ query ]);
135-
136- return $ client ;
137- }
138-
139- protected static function createClientAuthenticated (?string $ username , string $ testCase , ?string $ password = self ::DEFAULT_PASSWORD ): KernelBrowser
140- {
141- static ::ensureKernelShutdown ();
142- $ client = static ::createClient (['test_case ' => $ testCase ]);
143-
144- if (null !== $ username ) {
145- $ client ->setServerParameters ([
146- 'PHP_AUTH_USER ' => $ username ,
147- 'PHP_AUTH_PW ' => $ password ,
148- ]);
149- }
150-
151- return $ client ;
152- }
153-
154- protected static function assertResponse (string $ query , array $ expected , ?string $ username , string $ testCase , ?string $ password = self ::DEFAULT_PASSWORD , array $ variables = null ): KernelBrowser
155- {
156- $ client = self ::createClientAuthenticated ($ username , $ testCase , $ password );
157- $ result = self ::sendRequest ($ client , $ query , false , $ variables );
158-
159- static ::assertSame ($ expected , json_decode ($ result , true ), $ result );
160-
161- return $ client ;
162- }
163-
164- /**
165- * @return mixed
166- */
167- protected static function sendRequest (KernelBrowser $ client , string $ query , bool $ isDecoded = false , array $ variables = null )
168- {
169- $ client ->request ('GET ' , '/ ' , ['query ' => $ query , 'variables ' => json_encode ($ variables )]);
170- $ result = $ client ->getResponse ()->getContent ();
171-
172- return $ isDecoded ? json_decode ($ result , true ) : $ result ;
173- }
174-
175- /**
176- * @return mixed|ExpressionFunction
177- */
178- public static function expressionFunctionFromPhp (string $ phpFunctionName )
179- {
180- if (is_callable ([ExpressionFunction::class, 'fromPhp ' ])) {
181- return call_user_func ([ExpressionFunction::class, 'fromPhp ' ], $ phpFunctionName );
182- }
183-
184- return new ExpressionFunction ($ phpFunctionName , fn () => sprintf ('\%s(%s) ' , $ phpFunctionName , implode (', ' , func_get_args ())), function (): void {});
185- }
186-
187- /**
188- * @param KernelBrowser $client
189- */
190- protected function disableCatchExceptions ($ client ): void
191- {
192- if (is_callable ([$ client , 'catchExceptions ' ])) {
193- $ client ->catchExceptions (false );
194- }
195- }
196- }
0 commit comments