Guest User

Untitled

a guest
Jan 12th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.25 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. namespace Tests;
  5.  
  6.  
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Faker\Factory;
  9. use Faker\Generator;
  10. use Symfony\Bundle\FrameworkBundle\Client;
  11. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  12. use Symfony\Component\Routing\Generator\UrlGenerator;
  13. use Symfony\Component\Routing\RequestContext;
  14. use Tests\Config\Config;
  15.  
  16. abstract class TestHelper extends WebTestCase
  17. {
  18.     /**
  19.      * @var Client
  20.      */
  21.     private $client;
  22.  
  23.     /**
  24.      * @var UrlGenerator
  25.      */
  26.     private $urlGenerator;
  27.  
  28.     /**
  29.      * @var Config
  30.      */
  31.     protected static $config;
  32.  
  33.     public function setUp()
  34.     {
  35.         $this->client = static::createClient();
  36.         $this->urlGenerator = new UrlGenerator($this->client->getContainer()->get('router')->getRouteCollection(), new RequestContext(''));
  37.  
  38.         if (!self::$config) {
  39.             $this->setConfig();
  40.         }
  41.     }
  42.  
  43.     /**
  44.      * @param string $path
  45.      * @param bool $authorize
  46.      */
  47.     protected function get(string $path, bool $authorize = false)
  48.     {
  49.         if ($authorize) {
  50.             $this->client->request(
  51.                 'GET',
  52.                 $path,
  53.                 [],
  54.                 [],
  55.                 ['HTTP_AUTHORIZATION' => 'Bearer ' . self::$config->getToken()]
  56.             );
  57.         } else {
  58.             $this->client->request(
  59.                 'GET',
  60.                 $path
  61.             );
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * @param string $path
  67.      * @param array $body
  68.      * @param bool $authorize
  69.      */
  70.     protected function post(string $path, array $body, bool $authorize = false)
  71.     {
  72.         if ($authorize) {
  73.             $this->client->request(
  74.                 'POST',
  75.                 $path,
  76.                 [],
  77.                 [],
  78.                 ['CONTENT_TYPE' => 'application/json', 'HTTP_AUTHORIZATION' => 'Bearer ' . self::$config->getToken()],
  79.                 json_encode($body)
  80.             );
  81.         } else {
  82.             $this->client->request(
  83.                 'POST',
  84.                 $path,
  85.                 [],
  86.                 [],
  87.                 ['CONTENT_TYPE' => 'application/json'],
  88.                 json_encode($body)
  89.             );
  90.         }
  91.     }
  92.  
  93.     /**
  94.      * @param string $path
  95.      * @param array $body
  96.      * @param bool $authorize
  97.      */
  98.     protected function put(string $path, array $body, bool $authorize = false)
  99.     {
  100.         if ($authorize) {
  101.             $this->client->request(
  102.                 'PUT',
  103.                 $path,
  104.                 [],
  105.                 [],
  106.                 ['CONTENT_TYPE' => 'application/json', 'HTTP_AUTHORIZATION' => 'Bearer ' . self::$config->getToken()],
  107.                 json_encode($body)
  108.             );
  109.         } else {
  110.             $this->client->request(
  111.                 'PUT',
  112.                 $path,
  113.                 [],
  114.                 [],
  115.                 ['CONTENT_TYPE' => 'application/json'],
  116.                 json_encode($body)
  117.             );
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * @param string $path
  123.      * @param bool $authorize
  124.      */
  125.     protected function delete(string $path, bool $authorize = false)
  126.     {
  127.         if ($authorize) {
  128.             $this->client->request(
  129.                 'DELETE',
  130.                 $path,
  131.                 [],
  132.                 [],
  133.                 ['HTTP_AUTHORIZATION' => 'Bearer ' . self::$config->getToken()]
  134.             );
  135.         } else {
  136.             $this->client->request(
  137.                 'DELETE',
  138.                 $path
  139.             );
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * @param string $pathName
  145.      * @param array $params
  146.      *
  147.      * @return string
  148.      */
  149.     protected function getPathUrl(string $pathName, array $params = []): string
  150.     {
  151.         if (empty($params)) {
  152.             $path = $this->urlGenerator->generate($pathName);
  153.         } else {
  154.             $path = $this->urlGenerator->generate($pathName, $params);
  155.         }
  156.  
  157.         return $path;
  158.     }
  159.  
  160.     /**
  161.      * @return EntityManagerInterface
  162.      */
  163.     protected function getEntityManager(): EntityManagerInterface
  164.     {
  165.         return $this->client->getContainer()->get('doctrine.orm.entity_manager');
  166.     }
  167.  
  168.     /**
  169.      * @return Generator
  170.      */
  171.     protected function getFaker(): Generator
  172.     {
  173.         return Factory::create();
  174.     }
  175.  
  176.     /**
  177.      * @param array $requiredFields
  178.      */
  179.     protected function validateSuccessfulResponse(array $requiredFields = [])
  180.     {
  181.         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
  182.  
  183.         $this->assertKeysRecursively($requiredFields, $this->getResponseBody(true));
  184.     }
  185.  
  186.     /**
  187.      * @param bool $isSuccessfulResponse
  188.      *
  189.      * @return array
  190.      */
  191.     protected function getResponseBody(bool $isSuccessfulResponse): array
  192.     {
  193.         $data = $isSuccessfulResponse ? 'data' : 'errors';
  194.  
  195.         return json_decode($this->client->getResponse()->getContent(), true)[$data];
  196.     }
  197.  
  198.     /**
  199.      * @param array $requiredFields
  200.      * @param array $response
  201.      * @param string|int|null $tmpKey
  202.      */
  203.     private function assertKeysRecursively(array $requiredFields, array $response, $tmpKey = null)
  204.     {
  205.         foreach ($requiredFields as $key => $requiredField) {
  206.             if (is_array($requiredField)) {
  207.                 if (!is_string($key)) {
  208.                     $this->assertKeysRecursively($requiredField, $response[$tmpKey], $key);
  209.                 }
  210.                 if (!is_null($tmpKey)) {
  211.                     $this->assertArrayHasKey($key, $response[$tmpKey]);
  212.                     $this->assertKeysRecursively($requiredField, $response[$tmpKey], $key);
  213.                 } else {
  214.                     $this->assertArrayHasKey($key, $response);
  215.                     $this->assertKeysRecursively($requiredField, $response, $key);
  216.                 }
  217.             } else {
  218.                 if (!is_null($tmpKey)) {
  219.                     $this->assertArrayHasKey($requiredField, $response[$tmpKey]);
  220.                 } else {
  221.                     $this->assertArrayHasKey($requiredField, $response);
  222.                 }
  223.             }
  224.         }
  225.     }
  226.  
  227.     private function setConfig()
  228.     {
  229.         $className = '\Tests\Config\\' . $this->getGroups()[0];
  230.         self::$config = new $className;
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment