Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit 1c8bae3

Browse files
author
Teddy Roncin
committed
✅ (GET /users) Enhanced tests + added EtuUTTApiTestCase
Improved GetUsers tests to make them test everything. I can't see any more major issue with this class. Now uses database calls to test API calls. Also created superclass EtuUTTApiTestCase, which should provide a lot of useful methods in the future
1 parent 95a51d2 commit 1c8bae3

File tree

2 files changed

+110
-57
lines changed

2 files changed

+110
-57
lines changed

tests/EtuUTTApiTestCase.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
6+
use App\DataFixtures\UserSeeder;
7+
use App\Entity\User;
8+
use App\Entity\UserBan;
9+
use App\Entity\UserBDEContribution;
10+
use App\Entity\UserEtuUTTTeam;
11+
use App\Entity\UserMailsPhones;
12+
use App\Util\Slug;
13+
use App\Util\Text;
14+
use DateTime;
15+
use Doctrine\Bundle\FixturesBundle\Fixture;
16+
use Doctrine\Common\DataFixtures\Loader;
17+
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
18+
use Doctrine\ORM\EntityManager;
19+
use Faker\Factory;
20+
21+
abstract class EtuUTTApiTestCase extends ApiTestCase
22+
{
23+
24+
protected EntityManager $em;
25+
26+
protected function setUp(): void
27+
{
28+
$this->em = $this->getContainer()->get('doctrine')->getManager();
29+
(new ORMPurger($this->em))->purge();
30+
$user = new User();
31+
$user->setFirstName('test');
32+
$user->setLastName('test');
33+
$user->setLogin('test');
34+
$user->addRole('ROLE_ADMIN');
35+
$this->em->persist($user);
36+
$this->em->flush();
37+
}
38+
39+
protected function loadFixtures(Fixture... $fixtures)
40+
{
41+
$fixtureLoader = new Loader();
42+
foreach ($fixtures as $fixture) {
43+
$fixtureLoader->addFixture($fixture);
44+
}
45+
foreach ($fixtureLoader->getFixtures() as $fixture) {
46+
$fixture->load($this->em);
47+
}
48+
}
49+
50+
}

tests/users/GetUsers.php

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
namespace App\Tests\users;
44

5-
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
6-
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client;
5+
use App\DataFixtures\UserSeeder;
6+
use App\Entity\User;
7+
use App\Tests\EtuUTTApiTestCase;
78
use Symfony\Component\HttpFoundation\Response;
89

9-
class GetUsers extends ApiTestCase
10+
class GetUsers extends EtuUTTApiTestCase
1011
{
1112

12-
private $responseWithNoParameter = array();
13-
private $lastPage;
14-
1513
public function testNotConnected() : void
1614
{
1715
$client = static::createClient();
@@ -21,83 +19,88 @@ public function testNotConnected() : void
2119

2220
public function testNoParameter() : void
2321
{
24-
// TODO : update this code : we need to fill the database, and then run this test
25-
/*$client = static::createClient();
26-
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin' ]]);
22+
$this->loadFixtures(new UserSeeder());
23+
$client = static::createClient();
24+
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]);
2725
$crawler = $client->request('GET', '/users');
2826
$response = json_decode($crawler->getContent());
2927
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
3028
$this->assertIsArray($response->{'hydra:member'});
3129
$this->assertNotEmpty($response->{'hydra:member'});
32-
$this->responseWithNoParameter['member'] = array();
30+
$expectedResults = $this->em->createQueryBuilder()
31+
->select('user.id, user.login, user.firstName, user.lastName, infos.avatar')
32+
->from(User::class, 'user')
33+
->innerJoin('user.infos', 'infos')
34+
->addOrderBy('user.id')
35+
->setMaxResults(10)
36+
->getQuery()
37+
->execute();
3338
foreach ($response->{'hydra:member'} as $i => $member) {
34-
$this->assertMatchesRegularExpression("/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/", $member->{'id'});
35-
$this->assertNotEmpty($member->{'login'});
36-
$this->assertNotEmpty($member->{'firstName'});
37-
$this->assertNotEmpty($member->{'lastName'});
38-
$this->assertMatchesRegularExpression("/^https?:\\/\\/[\w.-]*\\.[\w-].+$/", $member->{'infos'}->{'avatar'});
39+
$this->assertEquals($expectedResults[$i]['id']->jsonSerialize(), $member->{'id'});
40+
$this->assertEquals($expectedResults[$i]['login'], $member->{'login'});
41+
$this->assertNotEmpty($expectedResults[$i]['firstName'], $member->{'firstName'});
42+
$this->assertNotEmpty($expectedResults[$i]['lastName'], $member->{'lastName'});
43+
$this->assertEquals($expectedResults[$i]['avatar'], $member->{'infos'}->{'avatar'});
3944
$this->assertIsArray($member->{'mailsPhones'});
40-
$returnedMember = array();
41-
$returnedMember['login'] = $member->{'login'};
42-
$returnedMember['firstName'] = $member->{'firstName'};
43-
$returnedMember['lastName'] = $member->{'lastName'};
44-
$returnedMember['avatar'] = $member->{'infos'}->{'avatar'};
45-
$this->responseWithNoParameter['member'][$i] = $returnedMember;
4645
}
4746
$this->assertIsNumeric($response->{'hydra:totalItems'});
4847
$this->assertTrue($response->{'hydra:totalItems'} >= 0);
49-
$this->responseWithNoParameter['totalItems'] = $response->{'hydra:totalItems'};
5048
$matches = array();
5149
$this->assertEquals(1, preg_match("/^\\/users\\?page=(?<id>\d+)+$/", $response->{'hydra:view'}->{'@id'}, $matches));
5250
$this->assertArrayHasKey('id', $matches);
53-
$this->responseWithNoParameter['view:id'] = $response->{'hydra:view'}->{'@id'};
5451
$this->assertEquals(1, preg_match("/^\\/users\\?page=(?<id>\d+)+$/", $response->{'hydra:view'}->{'hydra:next'}, $matches));
5552
$this->assertArrayHasKey('id', $matches);
56-
$this->responseWithNoParameter['view:next'] = $response->{'hydra:view'}->{'hydra:next'};
5753
$this->assertEquals(1, preg_match("/^\\/users\\?page=(?<id>\d+)+$/", $response->{'hydra:view'}->{'hydra:last'}, $matches));
5854
$this->assertArrayHasKey('id', $matches);
59-
$this->responseWithNoParameter['view:last'] = $response->{'hydra:view'}->{'hydra:last'};
60-
$this->lastPage = $matches['id'];*/
6155
}
6256

63-
public function testParameter1() : void
57+
public function testPageParameter() : void
6458
{
65-
// TODO : fill the database and then run this test
66-
/*$client = static::createClient();
67-
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin' ]]);
68-
$crawler = $client->request('GET', '/users?page=1');
69-
$response = json_decode($crawler->getContent());
70-
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
71-
foreach ($this->responseWithNoParameter['member'] as $i => $member) {
72-
$this->assertEquals($member['login'], $response->{'hydra:member'}[$i]->{'login'});
73-
$this->assertEquals($member['firstName'], $response->{'hydra:member'}[$i]->{'firstName'});
74-
$this->assertEquals($member['lastName'], $response->{'hydra:member'}[$i]->{'lastName'});
75-
$this->assertEquals($member['avatar'], $response->{'hydra:member'}[$i]->{'infos'}->{'avatar'});
59+
$this->loadFixtures(new UserSeeder());
60+
$client = static::createClient();
61+
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]);
62+
$expectedResults = $this->em->createQueryBuilder()
63+
->select('user.id, user.login, user.firstName, user.lastName, infos.avatar')
64+
->from(User::class, 'user')
65+
->innerJoin('user.infos', 'infos')
66+
->addOrderBy('user.id')
67+
->getQuery()
68+
->execute();
69+
$expectedResultsCount = count($expectedResults);
70+
$lastPage = (int) (($expectedResultsCount - 1) / 10) + 1;
71+
$page = 0;
72+
foreach ($expectedResults as $i => $expectedResult) {
73+
if ($i % 10 == 0) {
74+
$page++;
75+
$crawler = $client->request('GET', '/users?page='.$page);
76+
$response = json_decode($crawler->getContent());
77+
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
78+
$this->assertEquals($expectedResultsCount, $response->{'hydra:totalItems'});
79+
$this->assertEquals("/users?page=".$page, $response->{'hydra:view'}->{'@id'});
80+
if ($page > 1) {
81+
$this->assertEquals("/users?page=".($page - 1), $response->{'hydra:view'}->{'hydra:previous'});
82+
}
83+
if ($page < $lastPage) {
84+
$this->assertEquals("/users?page=".($page + 1), $response->{'hydra:view'}->{'hydra:next'});
85+
}
86+
$this->assertEquals("/users?page=".$lastPage, $response->{'hydra:view'}->{'hydra:last'});
87+
}
88+
$this->assertEquals($expectedResult['login'], $response->{'hydra:member'}[$i%10]->{'login'});
89+
$this->assertEquals($expectedResult['id']->jsonSerialize(), $response->{'hydra:member'}[$i%10]->{'id'});
90+
$this->assertEquals($expectedResult['firstName'], $response->{'hydra:member'}[$i%10]->{'firstName'});
91+
$this->assertEquals($expectedResult['lastName'], $response->{'hydra:member'}[$i%10]->{'lastName'});
92+
$this->assertEquals($expectedResult['avatar'], $response->{'hydra:member'}[$i%10]->{'infos'}->{'avatar'});
7693
}
77-
$this->assertEquals($this->responseWithNoParameter['totalItems'], $response->{'hydra:totalItems'});
78-
$this->assertEquals($this->responseWithNoParameter['view:id'], $response->{'hydra:view'}->{'@id'});
79-
$this->assertEquals($this->responseWithNoParameter['view:next'], $response->{'hydra:view'}->{'hydra:next'});
80-
$this->assertEquals($this->responseWithNoParameter['view:last'], $response->{'hydra:view'}->{'hydra:last'});*/
81-
}
8294

83-
public function testAllParameters() : void
84-
{
85-
// TODO : fill the database and then run this test
86-
/*$client = static::createClient();
87-
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin' ]]);
88-
for ($i = 1; $i <= $this->lastPage; $i++) {
89-
$client->request('GET', '/users?page='.$i);
90-
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
91-
}*/
9295
}
9396

9497
public function testOutOfRangeParameters() : void
9598
{
9699
$client = static::createClient();
97-
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin' ]]);
100+
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]);
98101
$client->request('GET', '/users?page=0');
99102
$this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST);
100-
$crawler = $client->request('GET', '/users?page=100'); // TODO : use database filling and not hard code the value of parameter 'page'
103+
$crawler = $client->request('GET', '/users?page=2');
101104
$response = json_decode($crawler->getContent());
102105
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
103106
$this->assertEmpty($response->{'hydra:member'});
@@ -106,11 +109,11 @@ public function testOutOfRangeParameters() : void
106109
public function testWrongTypeParameter() : void
107110
{
108111
$client = static::createClient();
109-
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'admin' ]]);
110-
$crawler = $client->request('GET', '/users?page=1.5');
112+
$client->setDefaultOptions([ 'headers' => [ 'CAS-LOGIN' => 'test' ]]);
113+
$crawler = $client->request('GET', '/users?page=2.5');
111114
$response = json_decode($crawler->getContent());
112115
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
113-
$this->assertEquals('/users?page=1', $response->{'hydra:view'}->{'@id'});
116+
$this->assertEmpty($response->{'hydra:member'});
114117
$client->request('GET', '/users?page=abc');
115118
$this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST);
116119
}

0 commit comments

Comments
 (0)