Skip to content

Commit

Permalink
Merge pull request #419 from loic425/feature/use-factories-on-api-tests
Browse files Browse the repository at this point in the history
Use factories on api tests
  • Loading branch information
loic425 committed Apr 26, 2022
2 parents cb652f6 + 1208730 commit 54501a1
Show file tree
Hide file tree
Showing 28 changed files with 208 additions and 95 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"src/Monofony/MetaPack/AdminMeta/.recipe/src/Controller/DashboardController.php",
"src/Monofony/MetaPack/AdminMeta/.recipe/src/Menu/AdminMenuBuilder.php",
"src/Monofony/MetaPack/FrontMeta/.recipe/src/Menu/AccountMenuBuilder.php",
"src/Monofony/MetaPack/CoreMeta/.recipe/src/Command/Installer/InstallSampleDataCommand.php"
"src/Monofony/MetaPack/CoreMeta/.recipe/src/Command/Installer/InstallSampleDataCommand.php",
"src/Monofony/MetaPack/ApiMeta/.recipe/src/Story/TestAppUsersStory.php"
]
},
"require": {
Expand Down
7 changes: 7 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@
<directory name="src/Monofony/MetaPack/CoreMeta/.recipe/src/Factory" />
</errorLevel>
</ImplementedReturnTypeMismatch>

<PossiblyUndefinedMethod>
<errorLevel type="suppress">
<referencedMethod name="App\Entity\User\AppUser::disableAutoRefresh" />
<referencedMethod name="App\Entity\User\AppUser::save" />
</errorLevel>
</PossiblyUndefinedMethod>
</issueHandlers>
</psalm>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Story;

use App\Factory\AppUserFactory;
use Zenstruck\Foundry\Story;

final class TestAppUsersStory extends Story
{
public function build(): void
{
AppUserFactory::createOne([
'email' => 'api@sylius.com',
'username' => 'sylius',
'password' => 'sylius',
'first_name' => 'Sam',
'last_name' => 'Identifie',
]);

AppUserFactory::createOne([
'email' => 'another-customer@example.com',
'username' => 'monofony',
'password' => 'monofony',
'first_name' => 'Another',
'last_name' => 'Customer',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@

namespace App\Tests\Controller\Customer;

use App\Factory\AppUserFactory;
use App\Story\TestAppUsersStory;
use App\Tests\Controller\AuthorizedHeaderTrait;
use App\Tests\Controller\JsonApiTestCase;
use Sylius\Component\Customer\Model\CustomerInterface;
use App\Tests\Controller\PurgeDatabaseTrait;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;

class ChangePasswordApiTest extends JsonApiTestCase
{
use Factories;
use AuthorizedHeaderTrait;
use PurgeDatabaseTrait;

/** @test */
public function it_does_not_allow_to_change_password_for_non_authenticated_user(): void
{
$resources = $this->loadFixturesFromFile('resources/fixtures.yaml');
/** @var CustomerInterface $customer */
$customer = $resources['customer'];
TestAppUsersStory::load();

$customer = AppUserFactory::find(['username' => 'sylius'])->getCustomer();

$this->client->request('PUT', '/api/customers/'.$customer->getId().'/password', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');

Expand All @@ -29,9 +34,9 @@ public function it_does_not_allow_to_change_password_for_non_authenticated_user(
/** @test */
public function it_does_not_allow_to_change_password_without_required_data(): void
{
$resources = $this->loadFixturesFromFile('resources/fixtures.yaml');
/** @var CustomerInterface $customer */
$customer = $resources['customer'];
TestAppUsersStory::load();

$customer = AppUserFactory::find(['username' => 'sylius'])->getCustomer();

$this->client->request('PUT', '/api/customers/'.$customer->getId().'/password', [], [], self::$authorizedHeaderWithContentType, '{}');

Expand All @@ -42,9 +47,9 @@ public function it_does_not_allow_to_change_password_without_required_data(): vo
/** @test */
public function it_does_not_allow_to_change_password_with_wrong_current_password(): void
{
$resources = $this->loadFixturesFromFile('resources/fixtures.yaml');
/** @var CustomerInterface $customer */
$customer = $resources['customer'];
TestAppUsersStory::load();

$customer = AppUserFactory::find(['username' => 'sylius'])->getCustomer();

$data =
<<<EOT
Expand All @@ -63,9 +68,9 @@ public function it_does_not_allow_to_change_password_with_wrong_current_password
/** @test */
public function it_allows_to_change_password(): void
{
$resources = $this->loadFixturesFromFile('resources/fixtures.yaml');
/** @var CustomerInterface $customer */
$customer = $resources['customer'];
TestAppUsersStory::load();

$customer = AppUserFactory::find(['username' => 'sylius'])->getCustomer();

$data =
<<<EOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@

namespace App\Tests\Controller\Customer;

use App\Factory\AppUserFactory;
use App\Story\TestAppUsersStory;
use App\Tests\Controller\AuthorizedHeaderTrait;
use App\Tests\Controller\JsonApiTestCase;
use Sylius\Component\Customer\Model\CustomerInterface;
use App\Tests\Controller\PurgeDatabaseTrait;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;

final class GetUserProfileApiTest extends JsonApiTestCase
{
use AuthorizedHeaderTrait;
use Factories;
use PurgeDatabaseTrait;

/** @test */
public function it_does_not_allow_to_get_user_profile_for_non_authenticated_user(): void
{
$resources = $this->loadFixturesFromFile('resources/fixtures.yaml');
/** @var CustomerInterface $customer */
$customer = $resources['customer'];
TestAppUsersStory::load();

$customer = AppUserFactory::find(['username' => 'sylius'])->getCustomer();

$this->client->request('GET', '/api/customers/'.$customer->getId());

Expand All @@ -29,9 +34,9 @@ public function it_does_not_allow_to_get_user_profile_for_non_authenticated_user
/** @test */
public function it_does_not_allows_to_get_another_profile(): void
{
$resources = $this->loadFixturesFromFile('resources/fixtures.yaml');
/** @var CustomerInterface $customer */
$customer = $resources['another_customer'];
TestAppUsersStory::load();

$customer = AppUserFactory::find(['username' => 'monofony'])->getCustomer();

$this->client->request('GET', '/api/customers/'.$customer->getId(), [], [], self::$authorizedHeaderWithContentType);

Expand All @@ -42,9 +47,9 @@ public function it_does_not_allows_to_get_another_profile(): void
/** @test */
public function it_allows_to_get_user_profile_when_it_is_myself(): void
{
$resources = $this->loadFixturesFromFile('resources/fixtures.yaml');
/** @var CustomerInterface $customer */
$customer = $resources['customer'];
TestAppUsersStory::load();

$customer = AppUserFactory::find(['username' => 'sylius'])->getCustomer();

$this->client->request('GET', '/api/customers/'.$customer->getId(), [], [], static::$authorizedHeaderWithContentType);

Expand All @@ -55,7 +60,9 @@ public function it_allows_to_get_user_profile_when_it_is_myself(): void
/** @test */
public function it_allows_to_get_my_user_profile(): void
{
$this->loadFixturesFromFile('resources/fixtures.yaml');
TestAppUsersStory::load();

AppUserFactory::find(['username' => 'sylius'])->getCustomer();

$this->client->request('GET', '/api/customers/me', [], [], static::$authorizedHeaderWithContentType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@

namespace App\Tests\Controller\Customer;

use App\Story\TestAppUsersStory;
use App\Tests\Controller\AuthorizedHeaderTrait;
use App\Tests\Controller\JsonApiTestCase;
use App\Tests\Controller\PurgeDatabaseTrait;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;

class LoginApiTest extends JsonApiTestCase
{
use AuthorizedHeaderTrait;
use Factories;
use PurgeDatabaseTrait;

/** @test */
public function it_provides_an_access_token(): void
{
$this->loadFixturesFromFile('resources/fixtures.yaml');
TestAppUsersStory::load();

$data =
<<<EOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@

declare(strict_types=1);

/*
* This file is part of the Monofony package.
*
* (c) Monofony
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Tests\Controller\Customer;

use App\Story\TestAppUsersStory;
use App\Tests\Controller\AuthorizedHeaderTrait;
use App\Tests\Controller\JsonApiTestCase;
use App\Tests\Controller\PurgeDatabaseTrait;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;

class RefreshTokenApiTest extends JsonApiTestCase
{
use AuthorizedHeaderTrait;
use Factories;
use PurgeDatabaseTrait;

/** @test */
public function it_allows_to_refresh_an_access_token(): void
{
$this->loadFixturesFromFile('resources/fixtures.yaml');
TestAppUsersStory::load();

$refreshToken = $this->getRefreshToken();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace App\Tests\Controller\Customer;

use App\Story\TestAppUsersStory;
use App\Tests\Controller\AuthorizedHeaderTrait;
use App\Tests\Controller\JsonApiTestCase;
use App\Tests\Controller\PurgeDatabaseTrait;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;

final class RegisterAppUserApiTest extends JsonApiTestCase
{
use AuthorizedHeaderTrait;
use Factories;
use PurgeDatabaseTrait;

/** @test */
public function it_does_not_allow_to_register_an_app_user_without_required_data(): void
{
Expand All @@ -19,7 +27,7 @@ public function it_does_not_allow_to_register_an_app_user_without_required_data(
}

/** @test */
public function it_does_not_allow_to_register_a_too_short_password()
public function it_does_not_allow_to_register_a_too_short_password(): void
{
$data =
<<<EOT
Expand All @@ -36,9 +44,9 @@ public function it_does_not_allow_to_register_a_too_short_password()
}

/** @test */
public function it_does_not_allow_to_register_an_already_registered_user()
public function it_does_not_allow_to_register_an_already_registered_user(): void
{
$this->loadFixturesFromFile('resources/fixtures.yaml');
TestAppUsersStory::load();

$data =
<<<EOT
Expand All @@ -55,7 +63,7 @@ public function it_does_not_allow_to_register_an_already_registered_user()
}

/** @test */
public function it_allows_to_register_an_app_user()
public function it_allows_to_register_an_app_user(): void
{
$data =
<<<EOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

namespace App\Tests\Controller\Customer;

use App\Factory\AppUserFactory;
use App\Story\TestAppUsersStory;
use App\Tests\Controller\JsonApiTestCase;
use App\Tests\Controller\PurgeDatabaseTrait;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\Factories;

class ResetPasswordApiTest extends JsonApiTestCase
{
use Factories;
use PurgeDatabaseTrait;

/** @test */
public function it_does_not_allow_to_request_password_without_required_data(): void
{
Expand All @@ -28,7 +35,7 @@ public function it_does_not_allow_to_request_password_without_required_data(): v
/** @test */
public function it_allows_to_request_new_password(): void
{
$this->loadFixturesFromFile('resources/fixtures.yaml');
TestAppUsersStory::load();

$data =
<<<EOT
Expand All @@ -46,7 +53,7 @@ public function it_allows_to_request_new_password(): void
/** @test */
public function it_does_not_allow_to_reset_password_without_required_data(): void
{
$this->loadFixturesFromFile('resources/fixtures.yaml');
TestAppUsersStory::load();

$data =
<<<EOT
Expand Down Expand Up @@ -80,7 +87,13 @@ public function it_does_not_allow_to_reset_password_with_token_not_found(): void
/** @test */
public function it_does_not_allow_to_reset_password_with_token_expired(): void
{
$this->loadFixturesFromFile('resources/fixtures.yaml');
TestAppUsersStory::load();

$user = AppUserFactory::find(['username' => 'sylius']);
$user->disableAutoRefresh();
$user->setPasswordRequestedAt(new \DateTimeImmutable('-1 day'));
$user->setPasswordResetToken('expired_t0ken');
$user->save();

$data =
<<<EOT
Expand All @@ -98,7 +111,13 @@ public function it_does_not_allow_to_reset_password_with_token_expired(): void
/** @test */
public function it_allows_to_reset_password(): void
{
$this->loadFixturesFromFile('resources/fixtures.yaml');
TestAppUsersStory::load();

$user = AppUserFactory::find(['username' => 'sylius']);
$user->disableAutoRefresh();
$user->setPasswordRequestedAt(new \DateTimeImmutable());
$user->setPasswordResetToken('t0ken');
$user->save();

$data =
<<<EOT
Expand Down

0 comments on commit 54501a1

Please sign in to comment.