72 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			PHP
		
	
	
| <?php
 | |
| 
 | |
| namespace Functional;
 | |
| 
 | |
| use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
 | |
| use ApiPlatform\Symfony\Bundle\Test\Client;
 | |
| use Faker\Factory;
 | |
| use Faker\Generator;
 | |
| use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
 | |
| use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
 | |
| use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
 | |
| use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
 | |
| use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
 | |
| use Zenstruck\Foundry\Test\Factories;
 | |
| use Zenstruck\Foundry\Test\ResetDatabase;
 | |
| 
 | |
| abstract class AbstractTest extends ApiTestCase
 | |
| {
 | |
|     use ResetDatabase, Factories;
 | |
| 
 | |
|     protected static Client $client;
 | |
|     protected static Generator $faker;
 | |
|     private ?string $token = null;
 | |
| 
 | |
|     public function setUp(): void
 | |
|     {
 | |
|         parent::setUp();
 | |
|         self::bootKernel();
 | |
| 
 | |
|         self::$client = static::createClient();
 | |
|         self::$faker = Factory::create();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @throws TransportExceptionInterface
 | |
|      * @throws ServerExceptionInterface
 | |
|      * @throws RedirectionExceptionInterface
 | |
|      * @throws DecodingExceptionInterface
 | |
|      * @throws ClientExceptionInterface
 | |
|      */
 | |
|     protected function getToken($body = []): string
 | |
|     {
 | |
|         if ($this->token) {
 | |
|             return $this->token;
 | |
|         }
 | |
| 
 | |
|         $response = static::createClient()->request('POST', '/auth/login', ['json' => $body ?: [
 | |
|             'username' => 'ogadmin',
 | |
|             'password' => '12345678',
 | |
|         ]]);
 | |
| 
 | |
|         $this->assertResponseIsSuccessful();
 | |
|         $data = $response->toArray();
 | |
|         $this->token = $data['token'];
 | |
| 
 | |
|         return $data['token'];
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @throws TransportExceptionInterface
 | |
|      * @throws ServerExceptionInterface
 | |
|      * @throws RedirectionExceptionInterface
 | |
|      * @throws DecodingExceptionInterface
 | |
|      * @throws ClientExceptionInterface
 | |
|      */
 | |
|     protected function createClientWithCredentials($token = null): Client
 | |
|     {
 | |
|         $token = $token ?: $this->getToken();
 | |
| 
 | |
|         return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]);
 | |
|     }
 | |
| } |