| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace App\Tests\Application;
- use Zenstruck\Foundry\Proxy;
- use App\Entity\Access\Access;
- use Doctrine\ORM\EntityManagerInterface;
- use ApiPlatform\Symfony\Bundle\Test\Client;
- use Symfony\Component\HttpFoundation\Request;
- use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
- use Symfony\Contracts\HttpClient\ResponseInterface;
- class ParametersTest extends ApiTestCase
- {
- protected Access | Proxy | null $user = null;
- private $securityToken;
- protected Client $client;
- private EntityManagerInterface $em;
- protected function setUp(): void
- {
- self::bootKernel();
- $this->em = self::getContainer()->get(EntityManagerInterface::class);
- $this->client = static::createClient();
- }
- // public function testGetParameters(): void
- // {
- // $this->loginAs($this->user);
- // $this->assertResponseIsSuccessful();
- // $this->get('/api/parameters/6262');
- // $this->assertResponseStatusCodeSame(200);
- // $this->assertJsonContains([
- // '@context' => '/api/contexts/Parameters',
- // '@id' => '/api/parameters/6262',
- // '@type' => 'Parameters',
- // 'id' => 6262,
- // 'organization' => '/api/organizations/616013',
- // 'financialDate' => '2024-01-23T00:00:00+00:00',
- // 'musicalDate' => '2024-01-23T00:00:00+00:00',
- // 'startCourseDate' => '2024-01-23T00:00:00+00:00',
- // 'endCourseDate' => '2024-01-23T00:00:00+00:00',
- // 'average' => 20,
- // 'editCriteriaNotationByAdminOnly'=> true,
- // 'smsSenderName' => 'MySender',
- // 'logoDonorsMove' => false,
- // 'subDomain' => 'subdomain',
- // 'website' => 'https://www.example.com',
- // 'otherWebsite' => 'https://www.otherwebsite.com',
- // 'customDomain' => 'https://www.customdomain.com',
- // 'desactivateOpentalentSiteWeb' => false,
- // 'bulletinPeriod' => 'SomePeriod',
- // 'bulletinWithTeacher' => false,
- // 'bulletinPrintAddress' => false,
- // 'bulletinSignatureDirector' => true,
- // 'bulletinDisplayLevelAcquired' => true,
- // 'bulletinShowEducationWithoutEvaluation' => false,
- // 'bulletinViewTestResults' => false,
- // 'bulletinShowAbsences' => false,
- // 'bulletinShowAverages' => true,
- // 'bulletinOutput' => 'SomeOutput',
- // 'bulletinEditWithoutEvaluation' => true,
- // 'bulletinReceiver' => 'STUDENTS_AND_THEIR_GUARDIANS',
- // 'bulletinCriteriaSort' => 'BY_CRITERIA_INSERT',
- // 'usernameSMS' => 'username',
- // 'passwordSMS' => 'password',
- // 'showAdherentList' => true,
- // 'studentsAreAdherents' => false,
- // 'timezone' => 'Europe/Paris',
- // 'educationPeriodicity' => 'ANNUAL',
- // 'advancedEducationNotationType' => 'BY_EDUCATION',
- // 'sendAttendanceEmail' => false,
- // 'sendAttendanceSms' => false,
- // 'generateAttendanceReport' => true,
- // 'consultPedagogicResult' => true,
- // 'consultTeacherListing' => true,
- // 'periodValidation' => true,
- // 'notifyAdministrationAbsence' => false,
- // ]);
- // }
- // public function testBadPutBulletinCriteriasort()
- // {
- // $this->loginAs($this->user);
- // $this->put('/api/parameters/6262', [
- // 'bulletinCriteriaSort' => 'BAD_CRITERIA_SORT'
- // ]);
- // $this->assertResponseStatusCodeSame(400);
- // }
- public function testBadadvancedEducationNotationType()
- {
- $this->loginAs($this->user);
- $this->put('/api/parameters/6262', [
- 'bulletinCriteriaSort' => 'bad_advancedEducation'
- ]);
- $this->assertResponseStatusCodeSame(400);
- }
- // public function testDeleteParameters()
- // {
- // $this->loginAs($this->user);
- // $this->delete('/api/parameters/6262');
- // $this->assertResponseStatusCodeSame(405);
- // }
- // public function testBadTimeZone()
- // {
- // $this->loginAs($this->user);
- // $this->put('/api/parameters/6262', [
- // 'timezone' => 'bad_timezone'
- // ]);
- // $this->assertResponseStatusCodeSame(500);
- // }
- // public function testBadPeridicity()
- // {
- // $this->loginAs($this->user);
- // $this->put('/api/parameters/6262', [
- // 'educationPeriodicity' => 'bad_periodicity'
- // ]);
- // $this->assertResponseStatusCodeSame(500);
- // }
- public function loginAs()
- {
- // on récupère l'access qui a l'id 641003 dans l'entity manager
- $access = $this->em->getRepository(Access::class)->find(642459);
- $person = $access->getPerson();
- $response = $this->post(
- '/login_check',
- ['username' => $person->getUsername(), 'password' => $person->getPassword()]
- );
- $content = $response->getContent();
- $this->securityToken = json_decode($content)->token;
- $this->user = $access;
- }
- /**
- * Send a GET request and return the response parsed content
- *
- * @param string $route
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function get(string $route, array $headers = []): ResponseInterface
- {
- return $this->request(
- Request::METHOD_GET,
- $route,
- null,
- $headers
- );
- }
- /**
- * Send a POST request and return the response parsed content
- *
- * @param string $route
- * @param array<mixed> $data
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function post(string $route, array $data, array $headers = []): ResponseInterface
- {
- return $this->request(
- Request::METHOD_POST,
- $route,
- $data,
- $headers
- );
- }
- /**
- * Send a requests, parse the hydra response and return an object or a Collection
- *
- * @param string $method
- * @param string $route
- * @param array<mixed> $data
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function request(string $method, string $route, array | null $data = null, array $headers = []): ResponseInterface
- {
- if ($this->user) {
- $headers = array_merge(
- ['x-accessid' => $this->user->getId(), 'authorization' => 'BEARER ' . $this->securityToken],
- $headers
- );
- }
- $parameters = ['headers' => $headers];
- if ($data) {
- $parameters['json'] = $data;
- }
- return $this->client->request(
- $method,
- $route,
- $parameters
- );
- }
- /**
- * Send a PUT request and return the response parsed content
- *
- * @param string $route
- * @param array<mixed> $data
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function put(string $route, array $data, array $headers = []): ResponseInterface
- {
- return $this->request(
- Request::METHOD_PUT,
- $route,
- $data,
- $headers
- );
- }
- /**
- * Send a DELETE request and return the response parsed content
- *
- * @param string $route
- * @param array<mixed> $headers
- * @return ResponseInterface
- */
- protected function delete(string $route, array $headers = []): ResponseInterface
- {
- return $this->request(
- Request::METHOD_DELETE,
- $route,
- null,
- $headers
- );
- }
- /**
- * Assert that the response has the expected status code and is well formated
- *
- * @param string $resourceClass
- * @param int $expectedStatus
- * @return void
- */
- protected function validateCollectionSchema(string $resourceClass, int $expectedStatus = 200): void
- {
- $this->assertResponseStatusCodeSame($expectedStatus);
- if ($expectedStatus == 200) {
- $this->assertResponseIsSuccessful();
- }
- // Asserts that the returned content type is JSON-LD (the default)
- $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
- // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
- // >>> Issue with the json typed PublicStructure::addresses properties
- // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
- }
- }
|