Assasz /
gorynych
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Copyright (c) 2020. |
||||
| 4 | * @author Paweł Antosiak <[email protected]> |
||||
| 5 | */ |
||||
| 6 | |||||
| 7 | declare(strict_types=1); |
||||
| 8 | |||||
| 9 | namespace Gorynych\Testing; |
||||
| 10 | |||||
| 11 | use Gorynych\Adapter\EntityManagerAdapterInterface; |
||||
| 12 | use Gorynych\Http\Kernel; |
||||
| 13 | use Gorynych\Http\KernelClient; |
||||
| 14 | use Gorynych\Http\RequestFactory; |
||||
| 15 | use Gorynych\Util\EnvAccess; |
||||
| 16 | use PHPUnit\Framework\TestCase; |
||||
| 17 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||||
| 18 | |||||
| 19 | abstract class ApiTestCase extends TestCase |
||||
| 20 | { |
||||
| 21 | use ApiAssertionsTrait; |
||||
| 22 | use DatabaseRecreatableTrait; |
||||
| 23 | |||||
| 24 | protected static ?ContainerInterface $container; |
||||
| 25 | protected static ?KernelClient $client; |
||||
| 26 | protected static ?EntityManagerAdapterInterface $entityManager; |
||||
| 27 | |||||
| 28 | public function setUp(): void |
||||
| 29 | { |
||||
| 30 | $kernel = static::createKernel()->boot(EnvAccess::get('APP_ENV', 'test')); |
||||
| 31 | |||||
| 32 | static::$container = $kernel->getContainer(); |
||||
| 33 | static::$client = new KernelClient($kernel, new RequestFactory()); |
||||
| 34 | static::$entityManager = static::$container->get('entity_manager.adapter'); |
||||
|
0 ignored issues
–
show
|
|||||
| 35 | |||||
| 36 | static::recreateDatabaseSchema(); |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | public function tearDown(): void |
||||
| 40 | { |
||||
| 41 | static::dropDatabaseSchema(); |
||||
| 42 | |||||
| 43 | static::$container = static::$client = static::$entityManager = null; |
||||
| 44 | } |
||||
| 45 | |||||
| 46 | /** |
||||
| 47 | * @return Kernel |
||||
| 48 | * @throws \RuntimeException if kernel cannot be retrieved |
||||
| 49 | */ |
||||
| 50 | protected static function createKernel(): Kernel |
||||
| 51 | { |
||||
| 52 | $kernelClass = EnvAccess::get('KERNEL_CLASS'); |
||||
| 53 | |||||
| 54 | if (false === class_exists($kernelClass)) { |
||||
| 55 | throw new \RuntimeException('Unable to retrieve not existent application kernel.'); |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | return new $kernelClass(); |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * @return mixed[] |
||||
| 63 | * @throws \BadMethodCallException |
||||
| 64 | * @throws \RuntimeException |
||||
| 65 | */ |
||||
| 66 | protected static function normalizeResponse(): array |
||||
| 67 | { |
||||
| 68 | if (null === $response = static::$client->getResponse()) { |
||||
|
0 ignored issues
–
show
The method
getResponse() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 69 | throw new \BadMethodCallException('Cannot normalize empty response.'); |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | $contentType = $response->headers->get('Content-Type'); |
||||
| 73 | |||||
| 74 | if ('application/json' === $contentType || 'application/problem+json' === $contentType) { |
||||
| 75 | $data = json_decode($response->getContent(), true); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | if (false === isset($data) && false === empty($response->getContent())) { |
||||
| 79 | throw new \RuntimeException('Failed to normalize non empty response.'); |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | return $data['data'] ?? $data ?? []; |
||||
| 83 | } |
||||
| 84 | } |
||||
| 85 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.