Skip to content

Commit f554dd8

Browse files
committed
🎨 [BC break] make methods static
1 parent fcc959b commit f554dd8

File tree

8 files changed

+123
-110
lines changed

8 files changed

+123
-110
lines changed

UPGRADE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
UPGRADE
22
=======
33

4+
From 4.0 to 5.0
5+
---------------
6+
7+
A bunch of methods are now static. This is affecting you only if you're overriding such methods, since you
8+
need to declare as static your methods too. Calling such method can be left untouched (e.g. you can still call
9+
methods using `$this->` instead of `self::`).
10+
Also, `$em` proteced property is now static. If you were using `$this->em`, you need to use `self::$em` instead.
11+
412
From 3.0 to 4.0
513
---------------
614

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
"symfony/dom-crawler": "^4.4 || ^5.0"
2525
},
2626
"require-dev": {
27-
"doctrine/lexer": "^1.0.1",
27+
"doctrine/lexer": "^1.2",
2828
"doctrine/orm": "^2.7",
2929
"friendsofphp/php-cs-fixer": "^2.16",
3030
"mikey179/vfsstream": "^1.6",
31-
"phpunit/phpunit": "^8.5 || ^9.1",
31+
"phpunit/phpunit": "^8.5 || ^9.2",
3232
"symfony/doctrine-bridge": "^4.4 || ^5.0",
3333
"symfony/framework-bundle": "^4.4 || ^5.0",
3434
"symfony/mime": "^4.4 || ^5.0",
@@ -41,7 +41,7 @@
4141
},
4242
"extra": {
4343
"branch-alias": {
44-
"dev-master": "4.5.x-dev"
44+
"dev-master": "5.1.x-dev"
4545
}
4646
},
4747
"autoload": {

docs/index.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final class MyTest extends WebTestCase
5858

5959
* Browser output debug
6060

61-
You can output the content of response in your browser, just calling `$this->saveOutput()`.
61+
You can output the content of response in your browser, just calling `self::saveOutput()`.
6262
You can define a parameter named `domain`, otherwise standard localhost will be used.
6363
The output will be save under document root and displayed with browser (by default, `/usr/bin/firefox`),
6464
then the page will be deleted.
@@ -92,7 +92,7 @@ final class MyTest extends WebTestCase
9292
This is integrated by default with [BeelabUserBundle](https://github.com/Bee-Lab/BeelabUserBundle).
9393
Anyway, you can use any user provider, passing the name of service as third argument or configuring.
9494
For example, if you want to login users provided by FOSUserBundle in your tests, you can do something like
95-
`$this->login('myuser', 'main', 'fos_user.user_provider.username');`.
95+
`self::login('myuser', 'main', 'fos_user.user_provider.username');`.
9696
Another notable service you can use is Symfony's built-in `security.user.provider.concrete.in_memory`.
9797
For basic usage, just pass the username as first argument.
9898

@@ -118,25 +118,25 @@ final class MyTest extends WebTestCase
118118

119119
* Files for forms
120120

121-
Use `$this->getImageFile()`, `$this->getPdfFile()`, `$this->getZipFile()`, and `$this->getTxtFile()` to get
121+
Use `self::getImageFile()`, `self::getPdfFile()`, `self::getZipFile()`, and `self::getTxtFile()` to get
122122
files of various types for your uploadable fields.
123-
In forms with more than a field of the same type, use `$this->getImageFile(1)`, `$this->getImageFile(2)`, etc.
124-
You can also use `$this->getFile(0, $data, 'png', 'image/png')` and pass directly your file data.
123+
In forms with more than a field of the same type, use `self::getImageFile(1)`, `self::getImageFile(2)`, etc.
124+
You can also use `self::getFile(0, $data, 'png', 'image/png')` and pass directly your file data.
125125

126126
* Form values shortcut
127127

128-
If you need to retrieve the value of a form field, you can use `$this->getFormValue('form_field')`.
128+
If you need to retrieve the value of a form field, you can use `self::getFormValue('form_field')`.
129129
This is useful for retrieving CSRF token values or select values.
130130
In case of a select, note that you need to add `option` after your field's id, and you can pass a third
131131
optional parameter with the position.
132132
If, for example, you want to retrieve the value of your second option in a `bar` field of a `foo` form
133-
(maybe beacuse the first one is empty), you can do `$this->getFormValue('foo_bar option', 1)`
133+
(maybe beacuse the first one is empty), you can do `self::getFormValue('foo_bar option', 1)`
134134

135135
* Forms with collections
136136

137137
Tipically, a form with a collection is a problem during tests, because the values of collections are not displayed
138138
in the HTML (but, instead, added via JavaScript).
139-
You can solve such problema by visiting form URL and then using `$this->postForm('your_form_name', $values)`
139+
You can solve such problema by visiting form URL and then using `self::postForm('your_form_name', $values)`
140140
(where`$values` can include collection values).
141141

142142
### Fixtures-related
@@ -150,7 +150,7 @@ final class MyTest extends WebTestCase
150150
* AbstractContainerAwareFixture
151151

152152
When you need the service container in your fixtures, instead of implementing
153-
`Symfony\Component\DependencyInjection\ContainerAwareInterface`, you can extends
153+
`Symfony\Component\DependencyInjection\ContainerAwareInterface`, you can extend
154154
`Beelab\TestBundle\DataFixtures\AbstractContainerAwareFixture`.
155155

156156
* Get entity by reference
@@ -178,13 +178,13 @@ final class MyTest extends WebTestCase
178178

179179
* Mail sent assertion
180180

181-
Check how many mails has been sent with `$this->assertMailSent(1)` (or 2, 3, etc.).
181+
Check how many mails has been sent with `self::assertMailSent(1)` (or 2, 3, etc.).
182182
You need to call `$self::client->enableProfiler()` before.
183183
Currently, this is working only with SwiftMailer.
184184

185185
### Command related
186186

187187
* Test commands
188188

189-
You can test a command by executing something like `$output = $this->commandTest('app:alert', new AlertCommand());`
189+
You can test a command by executing something like `$output = self::commandTest('app:alert', new AlertCommand());`
190190
and then doing assertions on `$output`.

src/Test/WebTestCase.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class WebTestCase extends SymfonyWebTestCase
2222
/**
2323
* @var EntityManagerInterface|null
2424
*/
25-
protected $em;
25+
protected static $em;
2626

2727
/**
2828
* @var \Symfony\Component\BrowserKit\AbstractBrowser
@@ -34,6 +34,10 @@ abstract class WebTestCase extends SymfonyWebTestCase
3434
*/
3535
private $fixture;
3636

37+
protected static $authUser;
38+
39+
protected static $authPw;
40+
3741
protected function setUp(): void
3842
{
3943
$environment = $_SERVER['APP_ENV'] ?? 'test';
@@ -45,7 +49,7 @@ protected function setUp(): void
4549
$kernel->boot();
4650
static::$container = $kernel->getContainer();
4751
if (static::$container->has('doctrine.orm.entity_manager')) {
48-
$this->em = static::$container->get('doctrine.orm.entity_manager');
52+
self::$em = static::$container->get('doctrine.orm.entity_manager');
4953
}
5054
}
5155
if (!empty(static::$authUser) && !empty(static::$authPw)) {
@@ -60,8 +64,8 @@ protected function setUp(): void
6064

6165
protected function tearDown(): void
6266
{
63-
if (null !== $this->em) {
64-
$this->em->getConnection()->close();
67+
if (null !== self::$em) {
68+
self::$em->getConnection()->close();
6569
}
6670
parent::tearDown();
6771
}
@@ -71,7 +75,7 @@ protected function tearDown(): void
7175
* See http://giorgiocefaro.com/blog/test-symfony-and-automatically-open-the-browser-with-the-response-content
7276
* You can define a "domain" parameter with the current domain of your app.
7377
*/
74-
protected function saveOutput(bool $delete = true): void
78+
protected static function saveOutput(bool $delete = true): void
7579
{
7680
$browser = static::$container->getParameter('beelab_test.browser');
7781
$rootDir = static::$container->getParameter('kernel.project_dir').'/';
@@ -100,7 +104,7 @@ protected function saveOutput(bool $delete = true): void
100104
*
101105
* @throws \InvalidArgumentException
102106
*/
103-
protected function login(string $username = '[email protected]', ?string $firewall = null, ?string $service = null): void
107+
protected static function login(string $username = '[email protected]', ?string $firewall = null, ?string $service = null): void
104108
{
105109
$service = $service ?? static::$container->getParameter('beelab_test.user_service');
106110
if (null === $user = static::$container->get($service)->loadUserByUsername($username)) {
@@ -118,17 +122,17 @@ protected function login(string $username = '[email protected]', ?string $firew
118122
/**
119123
* Get an image file to be used in a form.
120124
*/
121-
protected function getImageFile(int $file = 0): UploadedFile
125+
protected static function getImageFile(int $file = 0): UploadedFile
122126
{
123127
$data = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVQI12P4//8/AAX+Av7czFnnAAAAAElFTkSuQmCC';
124128

125-
return $this->getFile($file, $data, 'png', 'image/png');
129+
return self::getFile($file, $data, 'png', 'image/png');
126130
}
127131

128132
/**
129133
* Get a pdf file to be used in a form.
130134
*/
131-
protected function getPdfFile(int $file = 0): UploadedFile
135+
protected static function getPdfFile(int $file = 0): UploadedFile
132136
{
133137
$data = <<<'EOF'
134138
JVBERi0xLjEKJcKlwrHDqwoKMSAwIG9iagogIDw8IC9UeXBlIC9DYXRhbG9nCiAgICAgL1BhZ2VzIDIgMCBSCiAgPj4KZW5kb2JqCgoyIDAgb2JqCiAgP
@@ -142,30 +146,30 @@ protected function getPdfFile(int $file = 0): UploadedFile
142146
ICAgIC9TaXplIDUKICA+PgpzdGFydHhyZWYKNTY1CiUlRU9GCg==
143147
EOF;
144148

145-
return $this->getFile($file, $data, 'pdf', 'application/pdf');
149+
return self::getFile($file, $data, 'pdf', 'application/pdf');
146150
}
147151

148152
/**
149153
* Get a pdf file to be used in a form.
150154
*/
151-
protected function getZipFile(int $file = 0): UploadedFile
155+
protected static function getZipFile(int $file = 0): UploadedFile
152156
{
153157
$data = <<<'EOF'
154158
UEsDBAoAAgAAAM5RjEVOGigMAgAAAAIAAAAFABwAaC50eHRVVAkAA/OxilTzsYpUdXgLAAEE6AMAAARkAAAAaApQSwECHgMKAAIAAADOUYxF
155159
ThooDAIAAAACAAAABQAYAAAAAAABAAAApIEAAAAAaC50eHRVVAUAA/OxilR1eAsAAQToAwAABGQAAABQSwUGAAAAAAEAAQBLAAAAQQAAAAAA
156160
EOF;
157161

158-
return $this->getFile($file, $data, 'zip', 'application/zip');
162+
return self::getFile($file, $data, 'zip', 'application/zip');
159163
}
160164

161165
/**
162166
* Get a txt file to be used in a form.
163167
*/
164-
protected function getTxtFile(int $file = 0): UploadedFile
168+
protected static function getTxtFile(int $file = 0): UploadedFile
165169
{
166170
$data = 'Lorem ipsum dolor sit amet';
167171

168-
return $this->getFile($file, $data, 'txt', 'text/plain');
172+
return self::getFile($file, $data, 'txt', 'text/plain');
169173
}
170174

171175
/**
@@ -189,7 +193,7 @@ protected function loadFixtures(
189193
throw new \InvalidArgumentException(\sprintf('The service "%s" is not an EntityManager', $manager));
190194
}
191195
} else {
192-
$manager = $this->em;
196+
$manager = self::$em;
193197
}
194198
$manager->getConnection()->exec('SET foreign_key_checks = 0');
195199
$loader = new Loader(static::$container);
@@ -205,21 +209,21 @@ protected function loadFixtures(
205209
* Assert that $num mail has been sent
206210
* Need self::$client->enableProfiler() before calling.
207211
*/
208-
protected function assertMailSent(int $num, string $message = ''): void
212+
protected static function assertMailSent(int $num, string $message = ''): void
209213
{
210214
if (false !== $profile = self::$client->getProfile()) {
211215
$collector = $profile->getCollector('swiftmailer');
212-
$this->assertEquals($num, $collector->getMessageCount(), $message);
216+
self::assertEquals($num, $collector->getMessageCount(), $message);
213217
} else {
214-
$this->markTestSkipped('Profiler not enabled.');
218+
self::markTestSkipped('Profiler not enabled.');
215219
}
216220
}
217221

218222
/**
219223
* Get a form field value, from its id
220224
* Useful for POSTs.
221225
*/
222-
protected function getFormValue(string $fieldId, int $position = 0): string
226+
protected static function getFormValue(string $fieldId, int $position = 0): string
223227
{
224228
return self::$client->getCrawler()->filter('#'.$fieldId)->eq($position)->attr('value');
225229
}
@@ -241,7 +245,7 @@ protected static function ajax(string $method, string $uri, array $params = [],
241245
* @param array $otherCommands Possible other commands to define
242246
* @param array $inputs Possible inputs to set inside command
243247
*/
244-
protected function commandTest(
248+
protected static function commandTest(
245249
string $name,
246250
Command $command,
247251
array $arguments = [],
@@ -283,7 +287,7 @@ protected function getReference(string $name)
283287
/**
284288
* Get a file to be used in a form.
285289
*/
286-
protected function getFile(string $file, string $data, string $ext, string $mime): UploadedFile
290+
protected static function getFile(string $file, string $data, string $ext, string $mime): UploadedFile
287291
{
288292
$name = 'file_'.$file.'.'.$ext;
289293
$path = \tempnam(\sys_get_temp_dir(), 'sf_test_').$name;
@@ -297,13 +301,13 @@ protected function getFile(string $file, string $data, string $ext, string $mime
297301
*
298302
* @param string $name The name of form
299303
* @param array $values The values to submit
300-
* @param array $values The values to submit for $_FILES
304+
* @param array $files The values to submit for $_FILES
301305
* @param string $method The method of form
302306
*/
303-
protected function postForm(string $name, array $values, array $files = [], string $method = 'POST'): void
307+
protected static function postForm(string $name, array $values, array $files = [], string $method = 'POST'): void
304308
{
305309
$formAction = self::$client->getRequest()->getUri();
306-
$values['_token'] = $this->getFormValue($name.'__token');
310+
$values['_token'] = self::getFormValue($name.'__token');
307311
$filesValues = \count($files) > 0 ? [$name => $files] : [];
308312
self::$client->request($method, $formAction, [$name => $values], $filesValues);
309313
}

tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class ConfigurationTest extends \PHPUnit\Framework\TestCase
99
public function testGetConfigTreeBuilder(): void
1010
{
1111
$configuration = new Configuration();
12-
$this->assertInstanceOf(TreeBuilder::class, $configuration->getConfigTreeBuilder());
12+
self::assertInstanceOf(TreeBuilder::class, $configuration->getConfigTreeBuilder());
1313
}
1414
}

tests/FakeFixture.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Doctrine\Common\DataFixtures\FixtureInterface;
66
use Doctrine\Common\Persistence\ObjectManager;
77

8-
class FakeFixture implements FixtureInterface
8+
final class FakeFixture implements FixtureInterface
99
{
1010
public function load(ObjectManager $manager): void
1111
{

tests/FakeFixtureDependent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Doctrine\Common\DataFixtures\FixtureInterface;
77
use Doctrine\Common\Persistence\ObjectManager;
88

9-
class FakeFixtureDependent implements DependentFixtureInterface, FixtureInterface
9+
final class FakeFixtureDependent implements DependentFixtureInterface, FixtureInterface
1010
{
11-
public function getDependencies()
11+
public function getDependencies(): array
1212
{
1313
return [
1414
'Beelab\TestBundle\FakeFixture',

0 commit comments

Comments
 (0)