You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.9 KiB

<?php
namespace App\Tests;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Symfony\Bundle\Test\Client;
use App\Entity\Thing;
use Doctrine\ORM\EntityManagerInterface;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;
use DateTimeZone;
abstract class AbstractTest extends ApiTestCase
{
// This trait provided by Foundry will take care of refreshing the database content to a known state before each test
use ResetDatabase, Factories;
public function setUp(): void
{
self::bootKernel();
}
private static function generateAuthToken(string $authName, string $seedKey)
{
$sign_seed = sodium_base642bin($_ENV[$seedKey], SODIUM_BASE64_VARIANT_ORIGINAL);
$sign_pair = sodium_crypto_sign_seed_keypair($sign_seed);
$sign_secret = sodium_crypto_sign_secretkey($sign_pair);
$now = new \DateTimeImmutable("now", new DateTimeZone("UTC"));
$message = $authName . "|" . $now->format("c");
return sodium_bin2base64(sodium_crypto_sign($message, $sign_secret), SODIUM_BASE64_VARIANT_URLSAFE);
}
protected static function createClientWithToken($authName = "FooMan", string $seedKey = 'AUTH_SEED'): Client
{
return static::createClient([],
[
'headers' => [
'accept' => 'application/json',
'X-AUTH-TOKEN' => static::generateAuthToken($authName, $seedKey)
]
]);
}
protected function getIri(Thing|Proxy $thing)
{
if ($thing instanceof Proxy) {
return $this->getIriFromResource($thing->object());
}
return $this->getIriFromResource($thing);
}
protected function getEntityManager(): EntityManagerInterface
{
return static::$kernel->getContainer()->get('doctrine.orm.entity_manager');
}
}