Skip to content

Commit

Permalink
add PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oallain committed Jan 5, 2024
1 parent 4dd453e commit 5fc9d8b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ endif
rm -rf tests/Application
.PHONY: reset

phpunit: phpunit-configure phpunit-run ## Run PHPUnit
.PHONY: phpunit

###
### OTHER
### ¯¯¯¯¯¯
Expand Down Expand Up @@ -72,6 +75,12 @@ install-sylius:
${YARN} build
${CONSOLE} cache:clear

phpunit-configure:
cp phpunit.xml.dist ${TEST_DIRECTORY}/phpunit.xml

phpunit-run:
cd ${TEST_DIRECTORY} && ./vendor/bin/phpunit --testdox

help: SHELL=/bin/bash
help: ## Dislay this help
@IFS=$$'\n'; for line in `grep -h -E '^[a-zA-Z_#-]+:?.*?##.*$$' $(MAKEFILE_LIST)`; do if [ "$${line:0:2}" = "##" ]; then \
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\Acme\\SyliusExamplePlugin\\": "tests/"
},
"classmap": ["tests/Application/Kernel.php"]
"Tests\\BootstrapTheme\\": "tests/PHPUnit/"
}
},
"extra": {
"sylius-theme": {
Expand Down
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.5/phpunit.xsd"
colors="true"
bootstrap="config/bootstrap.php">
<testsuites>
<testsuite name="Test Suite">
<directory>vendor/sylius/bootstrap-theme/tests/PHPUnit</directory>
</testsuite>
</testsuites>

<php>
<ini name="error_reporting" value="-1" />

<server name="KERNEL_CLASS_PATH" value="src/Kernel.php" />
<server name="IS_DOCTRINE_ORM_SUPPORTED" value="true" />

<env name="APP_ENV" value="test"/>
<env name="SHELL_VERBOSITY" value="-1" />
</php>
</phpunit>
25 changes: 25 additions & 0 deletions tests/PHPUnit/AbstractTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tests\BootstrapTheme;

use Doctrine\ORM\EntityManager;
use Sylius\Component\Core\Model\Channel;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
* @internal
*/
abstract class AbstractTest extends WebTestCase
{
protected function useNoneTheme(): void
{
/** @var EntityManager $manager */
$manager = $this->getContainer()->get('doctrine')->getManager();

$channel = $manager->getRepository(Channel::class)->findOneBy([]);
$channel->setThemeName(null);
$manager->flush();
}
}
71 changes: 71 additions & 0 deletions tests/PHPUnit/ChangeThemeInAdminAreaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Tests\BootstrapTheme;

use App\Entity\User\AdminUser;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

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

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

public function testAccessToAdminArea(): void
{
$this->client->request('GET', '/admin/login');

self::assertResponseIsSuccessful();
self::assertPageTitleContains('Sylius | Administration panel login');

$this->adminLogin();
$this->client->request('GET', '/admin/');

self::assertResponseIsSuccessful();
self::assertPageTitleContains('Dashboard | Sylius');
}

public function testChangeThemeAffectFront(): void
{
/** @var UrlGeneratorInterface $urlGenerator */
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);

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

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

$crawler = $this->client->submitForm('Save changes', ['sylius_channel[themeName]' => 'sylius/bootstrap-theme',]);

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

self::assertNotSame($semanticBodyClass, $bootstrapBodyClass);
self::assertStringContainsString('pushable', $semanticBodyClass);
self::assertStringContainsString('d-flex', $bootstrapBodyClass);
}

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

$user = $manager->getRepository(AdminUser::class)->findOneBy(['username' => 'sylius']);
$this->client->loginUser($user, 'admin');
}
}

0 comments on commit 5fc9d8b

Please sign in to comment.