Skip to content

Commit

Permalink
Add functional tests for all actions
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Aug 14, 2024
1 parent d2c2d4f commit 76b749d
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 10 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"require": {
"php": "^8.1",
"doctrine/persistence": "^2.2.2 || ^3.0.0",
"nucleos/user-bundle": "^2.3 || ^3.0",
"nucleos/user-bundle": "^2.3 || ^3.4.0",
"symfony/config": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"symfony/event-dispatcher": "^6.4 || ^7.0",
Expand All @@ -61,10 +61,12 @@
},
"require-dev": {
"alcaeus/mongo-php-adapter": "^1.1",
"dama/doctrine-test-bundle": "^8.0",
"doctrine/doctrine-bundle": "^2.5",
"doctrine/orm": "^2.18 || ^3.0",
"ergebnis/composer-normalize": "^2.0.1",
"symfony/browser-kit": "^6.4 || ^7.0",
"symfony/css-selector": "^6.4 || ^7.0",
"symfony/doctrine-bridge": "^6.4 || ^7.0",
"symfony/yaml": "^6.4 || ^7.0"
},
Expand Down
7 changes: 7 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
<coverage/>
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
<env name="APP_DEBUG" value="false"/>
<env name="APP_ENV" value="test"/>
<env name="KERNEL_CLASS" value="Nucleos\ProfileBundle\Tests\App\AppKernel"/>
<ini name="date.timezone" value="UTC"/>
</php>
<testsuites>
<testsuite name="ProfileBundle Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<extensions>
<bootstrap class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
</extensions>
<source>
<include>
<directory suffix=".php">./src/</directory>
Expand Down
3 changes: 0 additions & 3 deletions src/EventListener/EmailConfirmationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public function __construct(
$this->requestStack = $requestStack;
}

/**
* @return array<string, string>
*/
public static function getSubscribedEvents(): array
{
return [
Expand Down
7 changes: 5 additions & 2 deletions tests/App/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Nucleos\ProfileBundle\Tests\App;

use DAMA\DoctrineTestBundle\DAMADoctrineTestBundle;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Nucleos\ProfileBundle\NucleosProfileBundle;
use Nucleos\UserBundle\NucleosUserBundle;
Expand All @@ -29,9 +30,9 @@ final class AppKernel extends Kernel
{
use MicroKernelTrait;

public function __construct()
public function __construct(string $environment = 'test', bool $debug = false)
{
parent::__construct('test', false);
parent::__construct($environment, $debug);
}

public function registerBundles(): iterable
Expand All @@ -46,6 +47,8 @@ public function registerBundles(): iterable

yield new NucleosUserBundle();

yield new DAMADoctrineTestBundle();

yield new NucleosProfileBundle();
}

Expand Down
12 changes: 11 additions & 1 deletion tests/App/Entity/TestGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

namespace Nucleos\ProfileBundle\Tests\App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Nucleos\UserBundle\Model\Group;

class TestGroup extends Group {}
#[ORM\Entity]
#[ORM\Table(name: 'user__group')]
class TestGroup extends Group
{
#[ORM\Id]
#[ORM\Column(type: Types::INTEGER)]
#[ORM\GeneratedValue]
protected ?int $id = null;
}
39 changes: 37 additions & 2 deletions tests/App/Entity/TestUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,44 @@

namespace Nucleos\ProfileBundle\Tests\App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Nucleos\UserBundle\Model\GroupInterface;
use Nucleos\UserBundle\Model\User;

/**
* @phpstan-extends User<\Nucleos\UserBundle\Model\GroupInterface>
* @phpstan-extends User<GroupInterface>
*/
class TestUser extends User {}
#[ORM\Entity]
#[ORM\Table(name: 'user__user')]
class TestUser extends User
{
#[ORM\Id]
#[ORM\Column(type: Types::INTEGER)]
#[ORM\GeneratedValue]
protected int $id;

/**
* @var Collection<array-key, GroupInterface>
*/
#[ORM\ManyToMany(targetEntity: TestGroup::class)]
#[ORM\JoinTable(name: 'user__user_group')]
protected Collection $groups;

private static int $index = 1;

public function __construct()
{
parent::__construct();

$this->id = self::$index++;
$this->groups = new ArrayCollection();
}

public function getId(): int
{
return $this->id;
}
}
12 changes: 11 additions & 1 deletion tests/App/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@

$containerConfigurator->extension('framework', ['session' => ['storage_factory_id' => 'session.storage.factory.mock_file', 'handler_id' => null]]);

$containerConfigurator->extension('framework', [
'mailer' => [
'dsn' => 'null://null',
],
]);

$containerConfigurator->extension('twig', ['strict_variables' => true]);

$containerConfigurator->extension('twig', ['exception_controller' => null]);
Expand Down Expand Up @@ -77,5 +83,9 @@

$containerConfigurator->extension('nucleos_user', ['group' => ['group_class' => TestGroup::class]]);

$containerConfigurator->extension('nucleos_user', ['loggedin' => ['route' => 'home']]);
$containerConfigurator->extension('nucleos_user', ['loggedin' => ['route' => 'nucleos_user_update_security']]);

$containerConfigurator->extension('nucleos_profile', ['registration' => ['confirmation' => ['enabled' => true, 'from_email' => '[email protected]']]]);

$containerConfigurator->extension('dama_doctrine_test', ['enable_static_connection' => true, 'enable_static_meta_data_cache' => true, 'enable_static_query_cache' => true]);
};
48 changes: 48 additions & 0 deletions tests/Functional/Action/EditProfileWebTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the NucleosProfileBundle 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\ProfileBundle\Tests\Functional\Action;

use Nucleos\ProfileBundle\Action\EditProfileAction;
use Nucleos\ProfileBundle\Tests\Functional\DoctrineSetupTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

#[CoversClass(EditProfileAction::class)]
class EditProfileWebTest extends WebTestCase
{
use DoctrineSetupTrait;

#[Test]
public function execute(): void
{
$client = self::createClient();

$this->persist(
$user = self::createUser(),
);

$client->loginUser($user);
$client->request('GET', '/profile/edit');

self::assertResponseIsSuccessful();

$client->submitForm('profile_form[save]', [
'profile_form[locale]' => 'de_DE',
]);

self::assertResponseRedirects('/profile/');
$client->followRedirect();

self::assertSame('de_DE', $this->getUser($user->getId())?->getLocale());
}
}
94 changes: 94 additions & 0 deletions tests/Functional/Action/RegistrationWebTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the NucleosProfileBundle 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\ProfileBundle\Tests\Functional\Action;

use Nucleos\ProfileBundle\Action\CheckRegistrationMailAction;
use Nucleos\ProfileBundle\Action\ConfirmRegistrationAction;
use Nucleos\ProfileBundle\Action\RegistrationAction;
use Nucleos\ProfileBundle\Action\RegistrationConfirmedAction;
use Nucleos\ProfileBundle\Tests\App\Entity\TestUser;
use Nucleos\ProfileBundle\Tests\Functional\DoctrineSetupTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

#[CoversClass(RegistrationAction::class)]
#[CoversClass(ConfirmRegistrationAction::class)]
#[CoversClass(CheckRegistrationMailAction::class)]
#[CoversClass(RegistrationConfirmedAction::class)]
class RegistrationWebTest extends WebTestCase
{
use DoctrineSetupTrait;

#[Test]
public function execute(): void
{
$client = self::createClient();

$this->performRegister($client);

$user = $this->getEntityManager()->getRepository(TestUser::class)->findOneBy([
'username' => 'new_username',
]);

self::assertNotNull($user);
self::assertFalse($user->isEnabled());
self::assertNotNull($user->getConfirmationToken());

$this->performConfirm($client, $user);
}

#[Test]
public function executeWithLoggedInUser(): void
{
$client = self::createClient();

$this->persist(
$user = self::createUser(),
);

$client->loginUser($user);
$client->request('GET', '/register/');

self::assertResponseRedirects('/profile/');
}

private function performRegister(KernelBrowser $client): void
{
$client->request('GET', '/register/');

self::assertResponseIsSuccessful();

$client->submitForm('registration_form[save]', [
'registration_form[username]' => 'new_username',
'registration_form[email]' => '[email protected]',
'registration_form[plainPassword][first]' => 'super_secret_password',
'registration_form[plainPassword][second]' => 'super_secret_password',
]);

self::assertResponseRedirects('/register/check-email');
}

private function performConfirm(KernelBrowser $client, TestUser $user): void
{
$client->request('GET', sprintf('/register/confirm/%s', $user->getConfirmationToken()));

self::assertResponseRedirects('/register/confirmed');

$user = $this->getEntityManager()->find(TestUser::class, $user->getId());

self::assertNotNull($user);
self::assertTrue($user->isEnabled());
self::assertNull($user->getConfirmationToken());
}
}
41 changes: 41 additions & 0 deletions tests/Functional/Action/ShowProfileWebTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the NucleosProfileBundle 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\ProfileBundle\Tests\Functional\Action;

use Nucleos\ProfileBundle\Action\ShowProfileAction;
use Nucleos\ProfileBundle\Tests\Functional\DoctrineSetupTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

#[CoversClass(ShowProfileAction::class)]
class ShowProfileWebTest extends WebTestCase
{
use DoctrineSetupTrait;

#[Test]
public function execute(): void
{
$client = self::createClient();

$this->persist(
$user = self::createUser(),
);

$client->loginUser($user);
$client->request('GET', '/profile/');

self::assertResponseIsSuccessful();

self::assertSelectorTextContains('.nucleos_profile_user_show', $user->getUsername());
}
}
Loading

0 comments on commit 76b749d

Please sign in to comment.