Skip to content

Commit

Permalink
test: Skip database depend tests when no database is available
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jul 14, 2024
1 parent 4b6e910 commit 7d59ed6
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 70 deletions.
117 changes: 67 additions & 50 deletions tests/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\GeneratedEntity\NSArticle;
use Doctrine\DBAL\Exception;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -20,75 +21,91 @@ public function getManagerRegistry(): ManagerRegistry

public function testRepository(): void
{
$article = $this->getManagerRegistry()->getRepository(NSArticle::class)->findOneBy([]);
$this->assertNotNull($article);
$this->assertInstanceOf(NSArticle::class, $article);
try {
$article = $this->getManagerRegistry()->getRepository(NSArticle::class)->findOneBy([]);
$this->assertNotNull($article);
$this->assertInstanceOf(NSArticle::class, $article);
} catch (Exception $e) {
$this->markTestSkipped('Database connection error.');
}
}

public function testCollection(): void
{
$articleCount = $this->getManagerRegistry()->getRepository(NSArticle::class)->countBy([]);
try {
$articleCount = $this->getManagerRegistry()->getRepository(NSArticle::class)->countBy([]);

static::createClient()->request('GET', '/api/articles');
static::createClient()->request('GET', '/api/articles');

$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@context' => '/api/contexts/Article',
'@id' => '/api/articles',
'@type' => 'hydra:Collection',
'hydra:totalItems' => $articleCount,
]);
$this->assertResponseHasHeader('Content-Type');
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@context' => '/api/contexts/Article',
'@id' => '/api/articles',
'@type' => 'hydra:Collection',
'hydra:totalItems' => $articleCount,
]);
$this->assertResponseHasHeader('Content-Type');
} catch (Exception $e) {
$this->markTestSkipped('Database connection error.');
}
}

public function testSingleArticle(): void
{
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
$article = $this->getManagerRegistry()->getRepository(NSArticle::class)->findOneBy([]);
if (null === $article) {
$this->fail('No article found in database.');
}
try {
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
$article = $this->getManagerRegistry()->getRepository(NSArticle::class)->findOneBy([]);
if (null === $article) {
$this->fail('No article found in database.');
}

$this->assertInstanceOf(NSArticle::class, $article);
$this->assertInstanceOf(NSArticle::class, $article);

static::createClient()->request('GET', '/api/articles/' . $article->getId());
static::createClient()->request('GET', '/api/articles/' . $article->getId());

$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@context' => '/api/contexts/Article',
'@id' => '/api/articles/' . $article->getId(),
'@type' => 'Article',
'title' => $article->getTitle(),
'url' => $urlGenerator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
RouteObjectInterface::ROUTE_OBJECT => $article,
]),
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@context' => '/api/contexts/Article',
'@id' => '/api/articles/' . $article->getId(),
'@type' => 'Article',
'title' => $article->getTitle(),
'url' => $urlGenerator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
RouteObjectInterface::ROUTE_OBJECT => $article,
]),
]);
} catch (Exception $e) {
$this->markTestSkipped('Database connection error.');
}
}

public function testArticleWebResponse(): void
{
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
$article = $this->getManagerRegistry()->getRepository(NSArticle::class)->findOneBy([]);
if (null === $article) {
$this->fail('No article found in database.');
}
$this->assertInstanceOf(NSArticle::class, $article);
try {
$urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
$article = $this->getManagerRegistry()->getRepository(NSArticle::class)->findOneBy([]);
if (null === $article) {
$this->fail('No article found in database.');
}
$this->assertInstanceOf(NSArticle::class, $article);

$path = $urlGenerator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
RouteObjectInterface::ROUTE_OBJECT => $article,
]);
$path = $urlGenerator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
RouteObjectInterface::ROUTE_OBJECT => $article,
]);

static::createClient()->request('GET', '/api/web_response_by_path', [
'query' => [
'path' => $path,
],
]);
static::createClient()->request('GET', '/api/web_response_by_path', [
'query' => [
'path' => $path,
],
]);

$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@context' => '/api/contexts/WebResponse',
'@id' => '/api/web_response_by_path',
'@type' => 'WebResponse',
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@context' => '/api/contexts/WebResponse',
'@id' => '/api/web_response_by_path',
'@type' => 'WebResponse',
]);
} catch (Exception $e) {
$this->markTestSkipped('Database connection error.');
}
}
}
41 changes: 24 additions & 17 deletions tests/OfferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\GeneratedEntity\NSOffer;
use Doctrine\DBAL\Exception;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/*
* This test case requires a running database server and Offer fixtures.
Expand All @@ -20,26 +19,34 @@ public function getManagerRegistry(): ManagerRegistry

public function testRepository(): void
{
$offer = $this->getManagerRegistry()->getRepository(NSOffer::class)->findOneBy([]);
$this->assertNotNull($offer);
$this->assertInstanceOf(NSOffer::class, $offer);
try {
$offer = $this->getManagerRegistry()->getRepository(NSOffer::class)->findOneBy([]);
$this->assertNotNull($offer);
$this->assertInstanceOf(NSOffer::class, $offer);
} catch (Exception $e) {
$this->markTestSkipped('Database connection error.');
}
}

public function testCollection(): void
{
$offerCount = $this->getManagerRegistry()->getRepository(NSOffer::class)->countBy([
'node.nodeType.name' => 'Offer',
]);
try {
$offerCount = $this->getManagerRegistry()->getRepository(NSOffer::class)->countBy([
'node.nodeType.name' => 'Offer',
]);

static::createClient()->request('GET', '/api/offers');
static::createClient()->request('GET', '/api/offers');

$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@context' => '/api/contexts/Offer',
'@id' => '/api/offers',
'@type' => 'hydra:Collection',
'hydra:totalItems' => $offerCount,
]);
$this->assertResponseHasHeader('Content-Type');
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@context' => '/api/contexts/Offer',
'@id' => '/api/offers',
'@type' => 'hydra:Collection',
'hydra:totalItems' => $offerCount,
]);
$this->assertResponseHasHeader('Content-Type');
} catch (Exception $e) {
$this->markTestSkipped('Database connection error.');
}
}
}
6 changes: 3 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* for tests
*/
$dbServerExists = passthru(sprintf(
'APP_ENV=%s php "%s/../bin/console" --env=test doctrine:database:create --if-not-exists',
$_ENV['APP_ENV'],
__DIR__
'APP_ENV=%s php "%s/../bin/console" --env=test doctrine:database:create --if-not-exists',
$_ENV['APP_ENV'],
__DIR__
), $resultCode);

/*
Expand Down

0 comments on commit 7d59ed6

Please sign in to comment.