Skip to content

Commit

Permalink
Merge pull request #604 from alexislefebvre/update-tests
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
alexislefebvre authored May 13, 2022
2 parents a32c4e2 + 8ba82b0 commit 8f4ff79
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 42 deletions.
29 changes: 18 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ jobs:
restore-keys: ${{ runner.os }}-composer-

- name: Require Symfony
run: |
composer require --no-update symfony/browser-kit=${{ matrix.symfony-version }}
composer require --no-update symfony/framework-bundle=${{ matrix.symfony-version }}
composer require --no-update symfony/css-selector=${{ matrix.symfony-version }}
composer require --no-update symfony/form=${{ matrix.symfony-version }}
composer require --no-update symfony/http-kernel=${{ matrix.symfony-version }}
composer require --no-update symfony/phpunit-bridge=${{ matrix.symfony-version }}
composer require --no-update symfony/security-bundle=${{ matrix.symfony-version }}
composer require --no-update symfony/twig-bundle=${{ matrix.symfony-version }}
composer require --no-update symfony/validator=${{ matrix.symfony-version }}
composer require --no-update symfony/yaml=${{ matrix.symfony-version }}
# non-dev then dev dependencies
run: >
composer require --no-update
symfony/framework-bundle=${{ matrix.symfony-version }}
symfony/browser-kit=${{ matrix.symfony-version }}
&&
composer require --dev --no-update
symfony/css-selector=${{ matrix.symfony-version }}
symfony/form=${{ matrix.symfony-version }}
symfony/http-kernel=${{ matrix.symfony-version }}
symfony/phpunit-bridge=${{ matrix.symfony-version }}
symfony/security-bundle=${{ matrix.symfony-version }}
symfony/twig-bundle=${{ matrix.symfony-version }}
symfony/validator=${{ matrix.symfony-version }}
symfony/yaml=${{ matrix.symfony-version }}
- name: Install Composer dependencies
if: matrix.composer-flags == ''
Expand All @@ -68,5 +72,8 @@ jobs:
# Use "update" instead of "install" since it allows using the "--prefer-lowest" option
run: composer update ${{ matrix.composer-flags }}

- name: Show Composer dependencies
run: composer show

- name: Run tests
run: php ./vendor/bin/phpunit --testdox
1 change: 0 additions & 1 deletion tests/App/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class User implements UserInterface
* @var int
* @ORM\Id()
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

Expand Down
3 changes: 2 additions & 1 deletion tests/App/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ security:
entity: { class: Liip\Acme\Tests\App\Entity\User, property: id }
firewalls:
secured_area:
pattern: ^/
pattern: ^/admin
anonymous: lazy
http_basic:
realm: "Admin Area"
provider: chain_provider
access_control:
- { path: ^/admin, roles: IS_AUTHENTICATED_FULLY }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }

services:
Expand Down
4 changes: 4 additions & 0 deletions tests/App/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ liipfunctionaltestbundle_homepage:
path: /
defaults: { _controller: 'Liip\Acme\Tests\App\Controller\DefaultController::indexAction' }

liipfunctionaltestbundle_admin:
path: /admin
defaults: { _controller: 'Liip\Acme\Tests\App\Controller\DefaultController::indexAction' }

liipfunctionaltestbundle_user:
path: /user/{userId}
defaults: { _controller: 'Liip\Acme\Tests\App\Controller\DefaultController::userAction' }
Expand Down
3 changes: 2 additions & 1 deletion tests/AppConfig/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ liip_functional_test:
command_verbosity: "very_verbose"
command_decoration: false
query:
max_query_count: 1
# TODO: it should be 1
max_query_count: 0
authentication:
username: "foobar"
password: "12341234"
2 changes: 1 addition & 1 deletion tests/DependencyInjection/ConfigurationConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function parametersProvider(): array
return [
['command_verbosity', 'very_verbose'],
['command_decoration', false],
['query.max_query_count', 1],
['query.max_query_count', 0],
['authentication.username', 'foobar'],
['authentication.password', '12341234'],
];
Expand Down
45 changes: 35 additions & 10 deletions tests/Test/WebTestCaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Liip\Acme\Tests\Test;

use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
use Liip\Acme\Tests\App\Entity\User;
use Liip\Acme\Tests\AppConfig\AppConfigKernel;
use Liip\Acme\Tests\Traits\LiipAcmeFixturesTrait;
use Liip\FunctionalTestBundle\Annotations\QueryCount;
Expand Down Expand Up @@ -53,7 +54,7 @@ public function testIndexClientWithCredentials(): void
{
$this->client = static::makeClientWithCredentials('foobar', '12341234');

$path = '/';
$path = '/admin';

$crawler = $this->client->request('GET', $path);

Expand Down Expand Up @@ -84,7 +85,7 @@ public function testIndexAuthenticatedClient(): void
{
$this->client = static::makeAuthenticatedClient();

$path = '/';
$path = '/admin';

$crawler = $this->client->request('GET', $path);

Expand All @@ -111,7 +112,6 @@ public function testIndexAuthenticatedClient(): void
*/
public function testIndexAuthenticationLoginAs(): void
{
$this->schemaUpdate();
$user = $this->loadTestFixtures();

$loginAs = $this->loginAs($user, 'secured_area');
Expand Down Expand Up @@ -150,7 +150,6 @@ public function testIndexAuthenticationLoginAs(): void
*/
public function testIndexAuthenticationLoginClient(): void
{
$this->schemaUpdate();
$user = $this->loadTestFixtures();

$this->client = static::makeClient();
Expand Down Expand Up @@ -183,25 +182,49 @@ public function testIndexAuthenticationLoginClient(): void
* Log in as the user defined in the Data Fixtures and except an
* AllowedQueriesExceededException exception.
*
* There will be 2 queries, in the configuration the limit is 1,
* an Exception will be thrown.
* There will be 2 queries:
* - the user 1 is loaded from the database when logging in
* - the user 2 is loaded by the controller
*
* In the configuration the limit is 1, an Exception will be thrown.
*/
public function testAllowedQueriesExceededException(): void
{
$this->schemaUpdate();
$user = $this->loadTestFixtures();

$this->loginAs($user, 'secured_area');
$this->assertInstanceOf(
User::class,
$user
);

$this->client = static::makeClient();

// One another query to load the second user.
$this->loginClient($this->client, $user, 'secured_area');

$path = '/user/2';

$this->expectException(\Liip\FunctionalTestBundle\Exception\AllowedQueriesExceededException::class);

$this->client->request('GET', $path);
$crawler = $this->client->request('GET', $path);

// The following code is called if no exception has been thrown, it should help to understand why
$this->assertStatusCode(200, $this->client);
$this->assertSame(
'LiipFunctionalTestBundle',
$crawler->filter('h1')->text()
);
$this->assertSame(
'Logged in as foo bar.',
$crawler->filter('p#user')->text()
);
$this->assertSame(
'Name: alice bob',
$crawler->filter('div#content p:nth-child(1)')->text()
);
$this->assertSame(
'Email: alice@example.com',
$crawler->filter('div#content p:nth-child(2)')->text()
);
}

/**
Expand All @@ -214,6 +237,8 @@ public function testAllowedQueriesExceededException(): void
*/
public function testAnnotationAndException(): void
{
$user = $this->loadTestFixtures();

$this->client = static::makeClient();

// One query to load the second user
Expand Down
43 changes: 26 additions & 17 deletions tests/Traits/LiipAcmeFixturesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,14 @@

trait LiipAcmeFixturesTrait
{
public function schemaUpdate(): void
{
// Create database
$kernel = $this->getContainer()->get('kernel');

$application = new Application($kernel);

$command = $application->find('doctrine:schema:update');
$commandTester = new CommandTester($command);
$return = $commandTester->execute([
'--force' => true,
]);

$this->assertSame(0, $return, $commandTester->getDisplay());
}

public function loadTestFixtures(): User
{
$this->resetSchema();

$user1 = new User();
$user1->setId(1);
$user1->setName('foo bar');
$user1->setEmail('foo@bar.com');
$user1->setEmail('foo@example');
$user1->setPassword('12341234');
$user1->setAlgorithm('plaintext');
$user1->setEnabled(true);
Expand All @@ -52,10 +38,33 @@ public function loadTestFixtures(): User
$user2 = clone $user1;

$user2->setId(2);
$user2->setName('alice bob');
$user2->setEmail('alice@example.com');

$manager->persist($user2);
$manager->flush();

return $user1;
}

private function resetSchema(): void
{
// Create database
$kernel = $this->getContainer()->get('kernel');

$application = new Application($kernel);

$command = $application->find('doctrine:schema:update');
$commandTester = new CommandTester($command);
$return = $commandTester->execute([
'--force' => true,
]);

$this->assertSame(0, $return, $commandTester->getDisplay());

$manager = $this->getContainer()->get('doctrine')->getManager();

$connection = $manager->getConnection();
$connection->query('DELETE FROM liip_user');
}
}

0 comments on commit 8f4ff79

Please sign in to comment.