15
15
use Symfony \Bundle \FrameworkBundle \Client ;
16
16
use Symfony \Bundle \FrameworkBundle \KernelBrowser ;
17
17
use Symfony \Bundle \FrameworkBundle \Test \WebTestCase ;
18
+ use Symfony \Cmf \Component \Testing \Functional \DbManager \ORM ;
19
+ use Symfony \Cmf \Component \Testing \Functional \DbManager \PHPCR ;
18
20
use Symfony \Cmf \Component \Testing \Functional \DbManager \PhpcrDecorator ;
19
21
use Symfony \Component \DependencyInjection \ContainerInterface ;
20
22
use Symfony \Component \HttpFoundation \Response ;
@@ -48,45 +50,62 @@ abstract class BaseTestCase extends WebTestCase
48
50
* * environment - The environment to use (defaults to 'phpcr')
49
51
* * debug - If debug should be enabled/disabled (defaults to true)
50
52
*/
51
- protected function getKernelConfiguration (): array
53
+ protected static function getKernelConfiguration (): array
52
54
{
53
55
return [];
54
56
}
55
57
56
58
/**
57
- * @return Client|KernelBrowser
59
+ * {@inheritdoc}
60
+ *
61
+ * Overwritten to set the default environment to 'phpcr'.
58
62
*/
59
- public function getFrameworkBundleClient ()
63
+ protected static function createKernel ( array $ options = []): KernelInterface
60
64
{
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 ' ;
67
68
}
68
69
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 ());
70
81
}
71
82
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
73
87
{
74
- return $ this ->getKernel ()->getContainer ();
88
+ if (method_exists (KernelTestCase::class, 'getContainer ' )) {
89
+ return parent ::getContainer ();
90
+ }
91
+
92
+ return self ::getKernel ()->getContainer ();
75
93
}
76
94
77
- public function getKernel (): KernelInterface
95
+ protected static function getKernel (): KernelInterface
78
96
{
79
97
if (null === static ::$ kernel ) {
80
- parent ::bootKernel (static :: getKernelConfiguration () );
98
+ static ::bootKernel ();
81
99
}
82
100
83
- if (static ::$ kernel instanceof KernelInterface) {
101
+ if (static ::$ kernel instanceof KernelInterface) {
84
102
$ kernelEnvironment = static ::$ kernel ->getEnvironment ();
85
- $ expectedEnvironment = isset ($ this -> getKernelConfiguration ()['environment ' ])
86
- ? $ this -> getKernelConfiguration ()['environment ' ]
103
+ $ expectedEnvironment = isset (static :: getKernelConfiguration ()['environment ' ])
104
+ ? static :: getKernelConfiguration ()['environment ' ]
87
105
: 'phpcr ' ;
88
106
if ($ kernelEnvironment !== $ expectedEnvironment ) {
89
- parent ::bootKernel (static ::getKernelConfiguration ());
107
+ var_dump ($ kernelEnvironment , $ expectedEnvironment );
108
+ static ::bootKernel ();
90
109
}
91
110
}
92
111
@@ -98,23 +117,35 @@ public function getKernel(): KernelInterface
98
117
}
99
118
100
119
/**
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.
102
137
*
103
138
* @see self::getDbManager
104
139
*/
105
- public function db ($ type )
140
+ protected function db ($ type )
106
141
{
107
142
return $ this ->getDbManager ($ type );
108
143
}
109
144
110
145
/**
111
- * Gets the DbManager.
112
- *
113
- * @param string $type The Db type
114
- *
115
- * @return object
146
+ * @return ORM|PHPCR
116
147
*/
117
- public function getDbManager ($ type )
148
+ protected function getDbManager (string $ type )
118
149
{
119
150
if (isset ($ this ->dbManagers [$ type ])) {
120
151
return $ this ->dbManagers [$ type ];
@@ -143,36 +174,7 @@ public function getDbManager($type)
143
174
return $ dbManager ;
144
175
}
145
176
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 )
176
178
{
177
179
libxml_use_internal_errors (true );
178
180
@@ -186,6 +188,6 @@ protected function assertResponseSuccess(Response $response)
186
188
$ exception = $ result ->item (0 )->nodeValue ;
187
189
}
188
190
189
- $ this -> assertEquals (200 , $ response ->getStatusCode (), $ exception ? 'Exception: " ' .$ exception .'" ' : '' );
191
+ self :: assertEquals (200 , $ response ->getStatusCode (), $ exception ? 'Exception: " ' .$ exception .'" ' : '' );
190
192
}
191
193
}
0 commit comments