-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce a ControllerTestCase with getEm(), assertFormError() and us…
…e this in all controllers (#1441)
- Loading branch information
Showing
10 changed files
with
93 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,34 +14,11 @@ | |
|
||
use App\Entity\User; | ||
use App\Validator\NotProhibitedPassword; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | ||
|
||
class ChangePasswordControllerTest extends WebTestCase | ||
class ChangePasswordControllerTest extends ControllerTestCase | ||
{ | ||
private KernelBrowser $client; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->client = self::createClient(); | ||
$this->client->disableReboot(); // Prevent reboot between requests | ||
static::getContainer()->get(Connection::class)->beginTransaction(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
static::getContainer()->get(Connection::class)->rollBack(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
#[TestWith(['SuperSecret123', 'ok'])] | ||
#[TestWith(['[email protected]', 'prohibited-password-error'])] | ||
public function testChangePassword(string $newPassword, string $expectedResult): void | ||
|
@@ -58,7 +35,7 @@ public function testChangePassword(string $newPassword, string $expectedResult): | |
$currentPasswordHash = self::getContainer()->get(UserPasswordHasherInterface::class)->hashPassword($user, $currentPassword); | ||
$user->setPassword($currentPasswordHash); | ||
|
||
$em = static::getContainer()->get(ManagerRegistry::class)->getManager(); | ||
$em = self::getEM(); | ||
$em->persist($user); | ||
$em->flush(); | ||
|
||
|
@@ -83,17 +60,7 @@ public function testChangePassword(string $newPassword, string $expectedResult): | |
|
||
if ($expectedResult === 'prohibited-password-error') { | ||
$this->assertResponseStatusCodeSame(422); | ||
$this->assertFormError((new NotProhibitedPassword)->message, $crawler); | ||
$this->assertFormError((new NotProhibitedPassword)->message, 'change_password_form', $crawler); | ||
} | ||
} | ||
|
||
private function assertFormError(string $message, Crawler $crawler): void | ||
{ | ||
$formCrawler = $crawler->filter('[name="change_password_form"]'); | ||
$this->assertCount( | ||
1, | ||
$formCrawler->filter('.alert-danger:contains("' . $message . '")'), | ||
$formCrawler->html()."\nShould find an .alert-danger within the form with the message: '$message'", | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <[email protected]> | ||
* Nils Adermann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Tests\Controller; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
class ControllerTestCase extends WebTestCase | ||
{ | ||
protected KernelBrowser $client; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->client = self::createClient(); | ||
$this->client->disableReboot(); // prevent reboot to keep the transaction | ||
|
||
static::getContainer()->get(Connection::class)->beginTransaction(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
static::getContainer()->get(Connection::class)->rollBack(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function getEM(): EntityManagerInterface | ||
{ | ||
return static::getContainer()->get(ManagerRegistry::class)->getManager(); | ||
} | ||
|
||
protected function assertFormError(string $message, string $formName, Crawler $crawler): void | ||
{ | ||
$formCrawler = $crawler->filter(sprintf('[name="%s"]', $formName)); | ||
$this->assertCount( | ||
1, | ||
$formCrawler->filter('.alert-danger:contains("' . $message . '")'), | ||
$formCrawler->html()."\nShould find an .alert-danger within the form with the message: '$message'", | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,33 +14,10 @@ | |
|
||
use App\Entity\User; | ||
use App\Validator\NotProhibitedPassword; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
class RegistrationControllerTest extends WebTestCase | ||
class RegistrationControllerTest extends ControllerTestCase | ||
{ | ||
private KernelBrowser $client; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->client = self::createClient(); | ||
$this->client->disableReboot(); // Prevent reboot between requests | ||
static::getContainer()->get(Connection::class)->beginTransaction(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
static::getContainer()->get(Connection::class)->rollBack(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testRegisterWithoutOAuth(): void | ||
{ | ||
$crawler = $this->client->request('GET', '/register/'); | ||
|
@@ -56,7 +33,7 @@ public function testRegisterWithoutOAuth(): void | |
$this->client->submit($form); | ||
$this->assertResponseStatusCodeSame(302); | ||
|
||
$em = static::getContainer()->get(ManagerRegistry::class)->getManager(); | ||
$em = self::getEM(); | ||
$user = $em->getRepository(User::class)->findOneBy(['username' => 'max.example']); | ||
$this->assertInstanceOf(User::class, $user); | ||
$this->assertSame('[email protected]', $user->getEmailCanonical(), "user email should have been canonicalized"); | ||
|
@@ -80,16 +57,6 @@ public function testRegisterWithTooSimplePasswords(string $password): void | |
$crawler = $this->client->submit($form); | ||
$this->assertResponseStatusCodeSame(422, 'Should be invalid because password is the same as email or username'); | ||
|
||
$this->assertFormError((new NotProhibitedPassword)->message, $crawler); | ||
} | ||
|
||
private function assertFormError(string $message, Crawler $crawler): void | ||
{ | ||
$formCrawler = $crawler->filter('[name="registration_form"]'); | ||
$this->assertCount( | ||
1, | ||
$formCrawler->filter('.alert-danger:contains("' . $message . '")'), | ||
$formCrawler->html()."\nShould find an .alert-danger within the form with the message: '$message'", | ||
); | ||
$this->assertFormError((new NotProhibitedPassword)->message, 'registration_form', $crawler); | ||
} | ||
} |
Oops, something went wrong.