Skip to content

Commit c58432d

Browse files
committed
feature #381 [make:*-test] Use the new WebTestAssertionsTrait methods in the generated functional tests (adrienlucas)
This PR was merged into the 1.0-dev branch. Discussion ---------- [make:*-test] Use the new WebTestAssertionsTrait methods in the generated functional tests Hi, this PR is to fix the way of calling the assertions static methods of PHPunit in the generated unit and functional tests. [First commit](8f4af14) : As pointed out in [this article](https://arkadiuszkondas.com/the-right-way-to-call-assertion-in-phpunit/), we should not be calling the assertions using `$this->assert*` but instead with `static::assert*`. [Second commit](cfb0715) : To go further, we can use [the new assertion API](symfony/symfony#30813) provided by the `WebTestCase`. I don't kown if this could be considered a BC break... but i hope not. Commits ------- 5982f3c Use the new api provided by the WebTestAssertionsTrait
2 parents d262c2c + 5982f3c commit c58432d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/Maker/MakeFunctionalTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Input\InputArgument;
2121
use Symfony\Component\Console\Input\InputInterface;
2222
use Symfony\Component\CssSelector\CssSelectorConverter;
23+
use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait;
2324

2425
/**
2526
* @author Javier Eguiluz <[email protected]>
@@ -52,7 +53,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
5253
$generator->generateClass(
5354
$testClassNameDetails->getFullName(),
5455
'test/Functional.tpl.php',
55-
[]
56+
[
57+
'web_assertions_are_available' => class_exists(WebTestAssertionsTrait::class),
58+
]
5659
);
5760

5861
$generator->writeChanges();

src/Resources/skeleton/test/Functional.tpl.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ public function testSomething()
1111
$client = static::createClient();
1212
$crawler = $client->request('GET', '/');
1313

14+
<?php if($web_assertions_are_available) { ?>
15+
$this->assertResponseIsSuccessful();
16+
$this->assertSelectorTextContains('h1', 'Hello World');
17+
<?php } else { ?>
1418
$this->assertSame(200, $client->getResponse()->getStatusCode());
1519
$this->assertContains('Hello World', $crawler->filter('h1')->text());
20+
<?php } ?>
1621
}
1722
}

0 commit comments

Comments
 (0)