Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace Tests;
- use Doctrine\ORM\EntityManagerInterface;
- use Faker\Factory;
- use Faker\Generator;
- use Symfony\Bundle\FrameworkBundle\Client;
- use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
- use Symfony\Component\Routing\Generator\UrlGenerator;
- use Symfony\Component\Routing\RequestContext;
- use Tests\Config\Config;
- abstract class TestHelper extends WebTestCase
- {
- /**
- * @var Client
- */
- private $client;
- /**
- * @var UrlGenerator
- */
- private $urlGenerator;
- /**
- * @var Config
- */
- protected static $config;
- public function setUp()
- {
- $this->client = static::createClient();
- $this->urlGenerator = new UrlGenerator($this->client->getContainer()->get('router')->getRouteCollection(), new RequestContext(''));
- if (!self::$config) {
- $this->setConfig();
- }
- }
- /**
- * @param string $path
- * @param bool $authorize
- */
- protected function get(string $path, bool $authorize = false)
- {
- if ($authorize) {
- $this->client->request(
- 'GET',
- $path,
- [],
- [],
- ['HTTP_AUTHORIZATION' => 'Bearer ' . self::$config->getToken()]
- );
- } else {
- $this->client->request(
- 'GET',
- $path
- );
- }
- }
- /**
- * @param string $path
- * @param array $body
- * @param bool $authorize
- */
- protected function post(string $path, array $body, bool $authorize = false)
- {
- if ($authorize) {
- $this->client->request(
- 'POST',
- $path,
- [],
- [],
- ['CONTENT_TYPE' => 'application/json', 'HTTP_AUTHORIZATION' => 'Bearer ' . self::$config->getToken()],
- json_encode($body)
- );
- } else {
- $this->client->request(
- 'POST',
- $path,
- [],
- [],
- ['CONTENT_TYPE' => 'application/json'],
- json_encode($body)
- );
- }
- }
- /**
- * @param string $path
- * @param array $body
- * @param bool $authorize
- */
- protected function put(string $path, array $body, bool $authorize = false)
- {
- if ($authorize) {
- $this->client->request(
- 'PUT',
- $path,
- [],
- [],
- ['CONTENT_TYPE' => 'application/json', 'HTTP_AUTHORIZATION' => 'Bearer ' . self::$config->getToken()],
- json_encode($body)
- );
- } else {
- $this->client->request(
- 'PUT',
- $path,
- [],
- [],
- ['CONTENT_TYPE' => 'application/json'],
- json_encode($body)
- );
- }
- }
- /**
- * @param string $path
- * @param bool $authorize
- */
- protected function delete(string $path, bool $authorize = false)
- {
- if ($authorize) {
- $this->client->request(
- 'DELETE',
- $path,
- [],
- [],
- ['HTTP_AUTHORIZATION' => 'Bearer ' . self::$config->getToken()]
- );
- } else {
- $this->client->request(
- 'DELETE',
- $path
- );
- }
- }
- /**
- * @param string $pathName
- * @param array $params
- *
- * @return string
- */
- protected function getPathUrl(string $pathName, array $params = []): string
- {
- if (empty($params)) {
- $path = $this->urlGenerator->generate($pathName);
- } else {
- $path = $this->urlGenerator->generate($pathName, $params);
- }
- return $path;
- }
- /**
- * @return EntityManagerInterface
- */
- protected function getEntityManager(): EntityManagerInterface
- {
- return $this->client->getContainer()->get('doctrine.orm.entity_manager');
- }
- /**
- * @return Generator
- */
- protected function getFaker(): Generator
- {
- return Factory::create();
- }
- /**
- * @param array $requiredFields
- */
- protected function validateSuccessfulResponse(array $requiredFields = [])
- {
- $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
- $this->assertKeysRecursively($requiredFields, $this->getResponseBody(true));
- }
- /**
- * @param bool $isSuccessfulResponse
- *
- * @return array
- */
- protected function getResponseBody(bool $isSuccessfulResponse): array
- {
- $data = $isSuccessfulResponse ? 'data' : 'errors';
- return json_decode($this->client->getResponse()->getContent(), true)[$data];
- }
- /**
- * @param array $requiredFields
- * @param array $response
- * @param string|int|null $tmpKey
- */
- private function assertKeysRecursively(array $requiredFields, array $response, $tmpKey = null)
- {
- foreach ($requiredFields as $key => $requiredField) {
- if (is_array($requiredField)) {
- if (!is_string($key)) {
- $this->assertKeysRecursively($requiredField, $response[$tmpKey], $key);
- }
- if (!is_null($tmpKey)) {
- $this->assertArrayHasKey($key, $response[$tmpKey]);
- $this->assertKeysRecursively($requiredField, $response[$tmpKey], $key);
- } else {
- $this->assertArrayHasKey($key, $response);
- $this->assertKeysRecursively($requiredField, $response, $key);
- }
- } else {
- if (!is_null($tmpKey)) {
- $this->assertArrayHasKey($requiredField, $response[$tmpKey]);
- } else {
- $this->assertArrayHasKey($requiredField, $response);
- }
- }
- }
- }
- private function setConfig()
- {
- $className = '\Tests\Config\\' . $this->getGroups()[0];
- self::$config = new $className;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment