1515use Symfony \Bundle \FrameworkBundle \Client ;
1616use Symfony \Bundle \FrameworkBundle \KernelBrowser ;
1717use Symfony \Bundle \FrameworkBundle \Test \WebTestCase ;
18+ use Symfony \Cmf \Component \Testing \Functional \DbManager \ORM ;
19+ use Symfony \Cmf \Component \Testing \Functional \DbManager \PHPCR ;
1820use Symfony \Cmf \Component \Testing \Functional \DbManager \PhpcrDecorator ;
1921use Symfony \Component \DependencyInjection \ContainerInterface ;
2022use Symfony \Component \HttpFoundation \Response ;
@@ -48,45 +50,62 @@ abstract class BaseTestCase extends WebTestCase
4850 * * environment - The environment to use (defaults to 'phpcr')
4951 * * debug - If debug should be enabled/disabled (defaults to true)
5052 */
51- protected function getKernelConfiguration (): array
53+ protected static function getKernelConfiguration (): array
5254 {
5355 return [];
5456 }
5557
5658 /**
57- * @return Client|KernelBrowser
59+ * {@inheritdoc}
60+ *
61+ * Overwritten to set the default environment to 'phpcr'.
5862 */
59- public function getFrameworkBundleClient ()
63+ protected static function createKernel ( array $ options = []): KernelInterface
6064 {
61- if (null === $ this ->client ) {
62- // property does not exist in all symfony versions
63- if (property_exists (self ::class, 'booted ' ) && self ::$ booted ) {
64- self ::ensureKernelShutdown ();
65- }
66- $ this ->client = self ::createClient ($ this ->getKernelConfiguration ());
65+ // default environment is 'phpcr'
66+ if (!isset ($ options ['environment ' ])) {
67+ $ options ['environment ' ] = 'phpcr ' ;
6768 }
6869
69- return $ this ->client ;
70+ return parent ::createKernel ($ options );
71+ }
72+
73+ /**
74+ * {@inheritdoc}
75+ *
76+ * Overwritten to set the kernel configuration from getKernelConfiguration.
77+ */
78+ protected static function bootKernel (array $ options = [])
79+ {
80+ return parent ::bootKernel (static ::getKernelConfiguration ());
7081 }
7182
72- public function getContainer (): ContainerInterface
83+ /**
84+ * BC with Symfony < 5.3 - when minimum version raises to ^5.3, we can remove this method.
85+ */
86+ protected static function getContainer (): ContainerInterface
7387 {
74- return $ this ->getKernel ()->getContainer ();
88+ if (method_exists (KernelTestCase::class, 'getContainer ' )) {
89+ return parent ::getContainer ();
90+ }
91+
92+ return self ::getKernel ()->getContainer ();
7593 }
7694
77- public function getKernel (): KernelInterface
95+ protected static function getKernel (): KernelInterface
7896 {
7997 if (null === static ::$ kernel ) {
80- parent ::bootKernel (static :: getKernelConfiguration () );
98+ static ::bootKernel ();
8199 }
82100
83- if (static ::$ kernel instanceof KernelInterface) {
101+ if (static ::$ kernel instanceof KernelInterface) {
84102 $ kernelEnvironment = static ::$ kernel ->getEnvironment ();
85- $ expectedEnvironment = isset ($ this -> getKernelConfiguration ()['environment ' ])
86- ? $ this -> getKernelConfiguration ()['environment ' ]
103+ $ expectedEnvironment = isset (static :: getKernelConfiguration ()['environment ' ])
104+ ? static :: getKernelConfiguration ()['environment ' ]
87105 : 'phpcr ' ;
88106 if ($ kernelEnvironment !== $ expectedEnvironment ) {
89- parent ::bootKernel (static ::getKernelConfiguration ());
107+ var_dump ($ kernelEnvironment , $ expectedEnvironment );
108+ static ::bootKernel ();
90109 }
91110 }
92111
@@ -98,23 +117,35 @@ public function getKernel(): KernelInterface
98117 }
99118
100119 /**
101- * Gets the DbManager.
120+ * @return Client|KernelBrowser
121+ */
122+ protected function getFrameworkBundleClient ()
123+ {
124+ if (null === $ this ->client ) {
125+ // property does not exist in all symfony versions
126+ if (property_exists (self ::class, 'booted ' ) && self ::$ booted ) {
127+ self ::ensureKernelShutdown ();
128+ }
129+ $ this ->client = self ::createClient ($ this ->getKernelConfiguration ());
130+ }
131+
132+ return $ this ->client ;
133+ }
134+
135+ /**
136+ * Shortcut for getDbManager.
102137 *
103138 * @see self::getDbManager
104139 */
105- public function db ($ type )
140+ protected function db ($ type )
106141 {
107142 return $ this ->getDbManager ($ type );
108143 }
109144
110145 /**
111- * Gets the DbManager.
112- *
113- * @param string $type The Db type
114- *
115- * @return object
146+ * @return ORM|PHPCR
116147 */
117- public function getDbManager ($ type )
148+ protected function getDbManager (string $ type )
118149 {
119150 if (isset ($ this ->dbManagers [$ type ])) {
120151 return $ this ->dbManagers [$ type ];
@@ -143,36 +174,7 @@ public function getDbManager($type)
143174 return $ dbManager ;
144175 }
145176
146- public static function getKernelClass (): string
147- {
148- if (isset ($ _SERVER ['KERNEL_CLASS ' ]) || isset ($ _ENV ['KERNEL_CLASS ' ])) {
149- $ class = isset ($ _SERVER ['KERNEL_CLASS ' ]) ? $ _SERVER ['KERNEL_CLASS ' ] : $ _ENV ['KERNEL_CLASS ' ];
150- if (!class_exists ($ class )) {
151- throw new \RuntimeException (sprintf ('Class "%s" doesn \'t exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the %s::createKernel() method. ' , $ class , static ::class));
152- }
153-
154- return $ class ;
155- }
156-
157- return parent ::getKernelClass ();
158- }
159-
160- /**
161- * {@inheritdoc}
162- *
163- * This is overriden to set the default environment to 'phpcr'
164- */
165- protected static function createKernel (array $ options = []): KernelInterface
166- {
167- // default environment is 'phpcr'
168- if (!isset ($ options ['environment ' ])) {
169- $ options ['environment ' ] = 'phpcr ' ;
170- }
171-
172- return parent ::createKernel ($ options );
173- }
174-
175- protected function assertResponseSuccess (Response $ response )
177+ protected static function assertResponseSuccess (Response $ response )
176178 {
177179 libxml_use_internal_errors (true );
178180
@@ -186,6 +188,6 @@ protected function assertResponseSuccess(Response $response)
186188 $ exception = $ result ->item (0 )->nodeValue ;
187189 }
188190
189- $ this -> assertEquals (200 , $ response ->getStatusCode (), $ exception ? 'Exception: " ' .$ exception .'" ' : '' );
191+ self :: assertEquals (200 , $ response ->getStatusCode (), $ exception ? 'Exception: " ' .$ exception .'" ' : '' );
190192 }
191193}
0 commit comments