-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functional tests for all actions
- Loading branch information
Showing
10 changed files
with
331 additions
and
5 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
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
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,75 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NucleosUserBundle package. | ||
* | ||
* (c) Christian Gripp <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\UserBundle\Tests\Functional\Action; | ||
|
||
use Nucleos\UserBundle\Action\AccountDeletionAction; | ||
use Nucleos\UserBundle\Tests\App\Entity\TestUser; | ||
use Nucleos\UserBundle\Tests\Functional\DoctrineSetupTrait; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
|
||
#[CoversClass(AccountDeletionAction::class)] | ||
final class AccountDeletionWebTest extends WebTestCase | ||
{ | ||
use DoctrineSetupTrait; | ||
|
||
#[Test] | ||
public function execute(): void | ||
{ | ||
$client = self::createClient(); | ||
|
||
$this->persist( | ||
$user = self::createUser(), | ||
); | ||
|
||
$client->loginUser($user); | ||
$client->request('GET', '/delete'); | ||
|
||
self::assertResponseIsSuccessful(); | ||
|
||
$client->submitForm('account_deletion_form[delete]', [ | ||
'account_deletion_form[current_password]' => $user->getPlainPassword(), | ||
'account_deletion_form[confirm]' => true, | ||
]); | ||
|
||
self::assertResponseRedirects('/logout'); | ||
|
||
self::assertNull($this->getEntityManager()->find(TestUser::class, $user->getId())); | ||
} | ||
|
||
#[Test] | ||
public function executeWithInvalidPassword(): void | ||
{ | ||
$client = self::createClient(); | ||
|
||
$this->persist( | ||
$user = self::createUser(), | ||
); | ||
|
||
$client->loginUser($user); | ||
$client->request('GET', '/delete'); | ||
|
||
self::assertResponseIsSuccessful(); | ||
|
||
$client->submitForm('account_deletion_form[delete]', [ | ||
'account_deletion_form[current_password]' => 'wrong', | ||
'account_deletion_form[confirm]' => true, | ||
]); | ||
|
||
self::assertResponseIsSuccessful(); | ||
|
||
self::assertSelectorTextContains('#account_deletion_form', 'The entered password is invalid'); | ||
|
||
self::assertNotNull($this->getEntityManager()->find(TestUser::class, $user->getId())); | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NucleosUserBundle package. | ||
* | ||
* (c) Christian Gripp <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\UserBundle\Tests\Functional\Action; | ||
|
||
use Nucleos\UserBundle\Action\LoginAction; | ||
use Nucleos\UserBundle\Tests\Functional\DoctrineSetupTrait; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
|
||
#[CoversClass(LoginAction::class)] | ||
final class LoginWebTest extends WebTestCase | ||
{ | ||
use DoctrineSetupTrait; | ||
|
||
#[Test] | ||
public function execute(): void | ||
{ | ||
$client = self::createClient(); | ||
|
||
$this->persist( | ||
$user = self::createUser(), | ||
); | ||
|
||
$client->request('GET', '/login'); | ||
|
||
self::assertResponseIsSuccessful(); | ||
|
||
$client->submitForm('save', [ | ||
'_username' => $user->getUsername(), | ||
'_password' => $user->getPlainPassword(), | ||
]); | ||
|
||
self::assertResponseRedirects('/profile'); | ||
} | ||
|
||
#[Test] | ||
public function executeWithInvalidPassword(): void | ||
{ | ||
$client = self::createClient(); | ||
|
||
$this->persist( | ||
$user = self::createUser(), | ||
); | ||
|
||
$client->request('GET', '/login'); | ||
$client->followRedirects(); | ||
|
||
self::assertResponseIsSuccessful(); | ||
|
||
$client->submitForm('save', [ | ||
'_username' => $user->getUsername(), | ||
'_password' => 'wrong', | ||
]); | ||
|
||
self::assertSelectorTextContains('li', 'Invalid credentials.'); | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the NucleosUserBundle package. | ||
* | ||
* (c) Christian Gripp <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\UserBundle\Tests\Functional; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Nucleos\UserBundle\Tests\App\Entity\TestUser; | ||
|
||
trait DoctrineSetupTrait | ||
{ | ||
private static int $index = 1; | ||
|
||
/** | ||
* @param string[] $roles | ||
*/ | ||
public static function createUser( | ||
string $username = null, | ||
array $roles = [] | ||
): TestUser { | ||
$username ??= ('my-user'.self::$index++); | ||
|
||
$entity = new TestUser(); | ||
$entity->setId(random_int(1, 9999999)); | ||
$entity->setUsername($username); | ||
$entity->setPlainPassword('password'); | ||
$entity->setEmail(sprintf('%s@localhost', $username)); | ||
$entity->setRoles($roles); | ||
|
||
return $entity; | ||
} | ||
|
||
protected function persist(object ...$objects): void | ||
{ | ||
$manager = $this->getEntityManager(); | ||
|
||
foreach ($objects as $object) { | ||
$manager->persist($object); | ||
} | ||
|
||
$manager->flush(); | ||
} | ||
|
||
private function getEntityManager(): EntityManagerInterface | ||
{ | ||
$manager = self::getContainer()->get('doctrine.orm.entity_manager'); | ||
|
||
\assert($manager instanceof EntityManagerInterface); | ||
|
||
return $manager; | ||
} | ||
} |
Oops, something went wrong.