Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application tests #324

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,26 @@ jobs:
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Unit tests
run: ./bin/phpunit --coverage-clover=coverage/unit.xml
run: SYMFONY_DEPRECATIONS_HELPER='disabled=1' ./bin/phpunit --coverage-clover=coverage/unit.xml --testsuite=Unit

application_tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
name: Application tests
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Application tests
run: |
docker network create frontend
docker compose up --detach
docker run -v $(pwd):/app --workdir /app node:16 yarn install
docker run -v $(pwd):/app --workdir /app node:16 yarn build
docker compose exec phpfpm composer install --no-interaction
docker compose exec phpfpm bin/console doctrine:migrations:migrate --no-interaction
docker compose exec phpfpm bin/console hautelook:fixtures:load --no-bundles --purge-with-truncate --no-interaction
docker compose exec --env SYMFONY_DEPRECATIONS_HELPER='disabled=1' phpfpm bin/phpunit --testsuite Application

# end-to-end-tests:
# runs-on: ubuntu-20.04
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ about writing changes to this log.

## [Unreleased]

- Added application tests.

## [1.1.2] 2023-02-17

- [SUPP0RT-874](https://jira.itkdev.dk/browse/SUPP0RT-874)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"hautelook/alice-bundle": "^2.8",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^1.3",
"phpunit/phpunit": "^9.5",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have this installed through the dev requirementsymfony/phpunit-bridge (https://symfony.com/doc/5.4/components/phpunit_bridge.html), so is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, but https://symfony.com/doc/current/testing.html#the-phpunit-testing-framework recommends (i.e. tells us) to install symfony/test-pack which requires phpunit/phpunit (cf. https://github.com/symfony/test-pack/blob/main/composer.json).

"symfony/browser-kit": "5.4.*",
"symfony/css-selector": "5.4.*",
"symfony/debug-bundle": "5.4.*",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions docs/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,13 @@ in the JavaScript Standard Style FAQ for more information.
* [Cypress documentation](https://docs.cypress.io/guides/overview/why-cypress.html#In-a-nutshell)
* [Guidelines for writing and organizing tests](https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html)
* [FAQ answer about ignoring functions in the global namespace](https://standardjs.com/#i-use-a-library-that-pollutes-the-global-namespace-how-do-i-prevent-variable-is-not-defined-errors)

## Running application tests

```sh
docker compose exec --env SYMFONY_DEPRECATIONS_HELPER='disabled=1' phpfpm bin/phpunit --testsuite Application
```

See [Disabling the Deprecation
Helper](https://symfony.com/doc/5.4/components/phpunit_bridge.html#disabling-the-deprecation-helper)
for details on `SYMFONY_DEPRECATIONS_HELPER='disabled=1'`.
7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
<server name="SYMFONY_PHPUNIT_VERSION" value="9"/>
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Application">
<directory>tests/Application</directory>
</testsuite>
</testsuites>
<listeners>
Expand Down
15 changes: 15 additions & 0 deletions tests/Application/AuthenticatedWebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Tests\Application;

use Symfony\Bundle\FrameworkBundle\KernelBrowser;

abstract class AuthenticatedWebTestCase extends WebTestCase
{
protected static array $userCriteria;

protected static function createClient(array $options = [], array $server = []): KernelBrowser
{
return parent::createAuthenticatedClient(static::$userCriteria);
}
}
19 changes: 19 additions & 0 deletions tests/Application/Controller/CaseControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Tests\Application\Controller;

use App\Tests\Application\AuthenticatedWebTestCase;

class CaseControllerTest extends AuthenticatedWebTestCase
{
protected static array $userCriteria = ['email' => 'caseworker@example.com'];

public function testSomething(): void
{
$client = static::createClient();
$client->request('GET', '/case/');

$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Sager');
}
}
62 changes: 62 additions & 0 deletions tests/Application/Controller/DefaultControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Tests\Application\Controller;

use App\Tests\Application\WebTestCase;

class DefaultControllerTest extends WebTestCase
{
public function testAnonymousSagsoverblik(): void
{
$client = static::createClient();
$client->request('GET', '/');

$this->assertResponseRedirects('/login');
}

public function testSagsoverblik(): void
{
$client = static::createAuthenticatedClient(['email' => 'caseworker@example.com']);

$client->request('GET', '/');
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Sagsoverblik');

$client->clickLink('Sagsoverblik');
$this->assertResponseIsSuccessful();
$this->assertSame('/', $client->getRequest()->getPathInfo());
$client->back();

$client->clickLink('Sagsliste');
$this->assertResponseIsSuccessful();
$this->assertSame('/case/', $client->getRequest()->getPathInfo());
$client->back();

$client->clickLink('Dagsordensliste');
$this->assertResponseIsSuccessful();
$this->assertSame('/agenda/', $client->getRequest()->getPathInfo());
$client->back();

$client->clickLink('Indstillinger');
$this->assertResponseRedirects();
$this->assertSame('/admin', $client->getRequest()->getPathInfo());
$client->back();
}

public function testSagsoverblikCreateCase(): void
{
$client = static::createAuthenticatedClient(['email' => 'caseworker@example.com']);

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

$client->clickLink('Ny sag');
$this->assertResponseIsSuccessful();
$this->assertSame('/case/new', $client->getRequest()->getPathInfo());
$client->back();

$client->clickLink('Ny dagsorden');
$this->assertResponseIsSuccessful();
$this->assertSame('/agenda/create', $client->getRequest()->getPathInfo());
$client->back();
}
}
36 changes: 36 additions & 0 deletions tests/Application/WebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Tests\Application;

use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;

abstract class WebTestCase extends BaseWebTestCase
{
protected static function createAuthenticatedClient(array $userCriteria, array $options = [], array $server = []): KernelBrowser
{
// Important: We MUST use self::createClient() (as opposed to
// static::createClient()) here to prevent an infinite loop when
// calling AuthenticatedWebTestCase::createClient().
$client = self::createClient($options, $server);

return static::loginUser($userCriteria, $client);
}

/**
* @see https://symfony.com/doc/current/testing.html#logging-in-users-authentication
*/
protected static function loginUser(array $userCriteria, KernelBrowser $client): KernelBrowser
{
$repository = static::getContainer()->get(UserRepository::class);
$user = $repository->findOneBy($userCriteria);

if (null === $user) {
throw new UserNotFoundException(json_encode($userCriteria));
}

return $client->loginUser($user);
}
}