Skip to content

Commit

Permalink
add tests for pages
Browse files Browse the repository at this point in the history
  • Loading branch information
oallain committed Jan 6, 2024
1 parent d07f079 commit 8381e58
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
20 changes: 19 additions & 1 deletion tests/PHPUnit/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,31 @@
*/
abstract class AbstractTest extends WebTestCase
{
protected function getChannel(): Channel
{
/** @var EntityManager $manager */
$manager = $this->getContainer()->get('doctrine')->getManager();

return $manager->getRepository(Channel::class)->findOneBy([]);
}

protected function useNoneTheme(): void
{
$this->switchTheme(null);
}

protected function useBootstrapTheme(): void
{
$this->switchTheme('sylius/bootstrap-theme');
}

private function switchTheme(?string $theme): void
{
/** @var EntityManager $manager */
$manager = $this->getContainer()->get('doctrine')->getManager();

$channel = $manager->getRepository(Channel::class)->findOneBy([]);
$channel->setThemeName(null);
$channel->setThemeName($theme);
$manager->flush();
}
}
3 changes: 2 additions & 1 deletion tests/PHPUnit/ChangeThemeInAdminAreaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ public function testChangeThemeAffectFront(): void
{
/** @var UrlGeneratorInterface $urlGenerator */
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
$channel = $this->getChannel();

$crawler = $this->client->request('GET', '/');
$semanticBodyClass = $crawler->filter('body')->attr('class');

$this->adminLogin();
$url = $urlGenerator->generate('sylius_admin_channel_update', ['id' => 1]);
$url = $urlGenerator->generate('sylius_admin_channel_update', ['id' => $channel->getId()]);
$this->client->request('GET', $url);
self::assertSelectorTextContains('h1', 'Edit channel');

Expand Down
130 changes: 130 additions & 0 deletions tests/PHPUnit/PagesCanBeAccessedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace PHPUnit;

use App\Entity\Addressing\Address;
use App\Entity\Order\Order;
use App\Entity\User\ShopUser;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Tests\BootstrapTheme\AbstractTest;

/**
* @internal
*/
final class PagesCanBeAccessedTest extends AbstractTest
{
private KernelBrowser $client;

protected function setUp(): void
{
$this->client = self::createClient();
$this->useBootstrapTheme();
}

/**
* @dataProvider guestPagesProvider
*/
public function testGuestPagesCanBeAccessed(string $routeName, array $params): void
{
/** @var UrlGeneratorInterface $urlGenerator */
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
$channel = $this->getChannel();
$params = \array_merge(['_locale' => $channel->getDefaultLocale()->getCode()], $params);

$url = $urlGenerator->generate($routeName, $params);
$this->client->request('GET', $url);

self::assertResponseIsSuccessful();
}

/**
* @dataProvider accountPagesProvider
*/
public function testAccountPagesCanBeAccessed(string $routeName, array $params): void
{
$user = $this->shopUserLogin();

/** @var UrlGeneratorInterface $urlGenerator */
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
$channel = $this->getChannel();

$params = \array_merge([
'_locale' => $channel->getDefaultLocale()->getCode(), // for all
'number' => $this->getOrderNumber($user), // for sylius_shop_account_order_show
'id' => $this->getAddressId($user), // for sylius_shop_account_address_book_update
], $params);

$url = $urlGenerator->generate($routeName, $params);
$this->client->request('GET', $url);

self::assertResponseIsSuccessful();
self::assertPageTitleContains('Fashion Web Store');
}

private function guestPagesProvider(): \Generator
{
yield 'sylius_shop_homepage' => ['sylius_shop_homepage', []];
yield 'sylius_shop_contact_request' => ['sylius_shop_contact_request', []];

yield 'sylius_shop_login' => ['sylius_shop_login', []];
yield 'sylius_shop_register' => ['sylius_shop_register', []];
yield 'sylius_shop_request_password_reset_token' => ['sylius_shop_request_password_reset_token', []];

yield 'sylius_shop_product_index' => ['sylius_shop_product_index', ['slug' => 'category']];
yield 'sylius_shop_product_show' => ['sylius_shop_product_show', ['slug' => 'everyday-white-basic-t-shirt']];
yield 'sylius_shop_product_review_index' => ['sylius_shop_product_review_index', ['slug' => 'everyday-white-basic-t-shirt']];
yield 'sylius_shop_product_review_create' => ['sylius_shop_product_review_create', ['slug' => 'everyday-white-basic-t-shirt']];

yield 'sylius_shop_cart_summary' => ['sylius_shop_cart_summary', []];
}

private function accountPagesProvider(): \Generator
{
yield 'sylius_shop_account_dashboard' => ['sylius_shop_account_dashboard', []];
yield 'sylius_shop_account_profile_update' => ['sylius_shop_account_profile_update', []];
yield 'sylius_shop_account_change_password' => ['sylius_shop_account_change_password', []];

yield 'sylius_shop_account_order_index' => ['sylius_shop_account_order_index', []];
yield 'sylius_shop_account_order_show' => ['sylius_shop_account_order_show', []];

yield 'sylius_shop_account_address_book_index' => ['sylius_shop_account_address_book_index', []];
yield 'sylius_shop_account_address_book_create' => ['sylius_shop_account_address_book_create', []];
yield 'sylius_shop_account_address_book_update' => ['sylius_shop_account_address_book_update', []];
}


private function shopUserLogin(): ShopUser
{
/** @var EntityManager $manager */
$manager = $this->getContainer()->get('doctrine')->getManager();

$user = $manager->getRepository(ShopUser::class)->findOneBy([]);
$this->client->loginUser($user, 'shop');

return $user;
}

private function getOrderNumber(ShopUser $user): string
{
/** @var EntityManager $manager */
$manager = $this->getContainer()->get('doctrine')->getManager();

/** @var Order $order */
$order = $manager->getRepository(Order::class)->findByCustomer($user->getCustomer())[0];

return $order->getNumber();
}

private function getAddressId(ShopUser $user): int
{
/** @var EntityManager $manager */
$manager = $this->getContainer()->get('doctrine')->getManager();
$address = $manager->getRepository(Address::class)->findByCustomer($user->getCustomer())[0];

return $address->getId();
}
}

0 comments on commit 8381e58

Please sign in to comment.