Skip to content

Commit 9445cdd

Browse files
authored
[ApiTestCase] Add a helper method to find the IRI of a resource (#3085)
* [ApiTestCase] Add an helper method to find the IRI of a resource * MongoDB support * Better implem * Update src/Bridge/Symfony/Bundle/Test/ApiTestCase.php Co-Authored-By: Teoh Han Hui <[email protected]> * Update src/Bridge/Symfony/Bundle/Test/ApiTestCase.php Co-Authored-By: Teoh Han Hui <[email protected]> * Better error message * Review * Update src/Bridge/Symfony/Bundle/Test/ApiTestCase.php Co-Authored-By: Teoh Han Hui <[email protected]> * Review
1 parent b0939ad commit 9445cdd

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ parameters:
8585
-
8686
message: '#Service "api_platform\.json_schema\.schema_factory" is private\.#'
8787
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php
88+
-
89+
message: '#Service "api_platform.iri_converter" is private\.#'
90+
path: %currentWorkingDirectory%/src/Bridge/Symfony/Bundle/Test/ApiTestCase.php
8891

8992
# Expected, due to optional interfaces
9093
- '#Method ApiPlatform\\Core\\Bridge\\Doctrine\\Orm\\Extension\\QueryCollectionExtensionInterface::applyToCollection\(\) invoked with 5 parameters, 3-4 required\.#'

src/Bridge/Symfony/Bundle/Test/ApiTestCase.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,34 @@ protected static function createClient(array $kernelOptions = [], array $default
6969

7070
return $client;
7171
}
72+
73+
/**
74+
* Finds the IRI of a resource item macthing the resource class and the specified criteria.
75+
*/
76+
protected function findIriBy(string $resourceClass, array $criteria): ?string
77+
{
78+
if (null === static::$container) {
79+
throw new \RuntimeException(sprintf('The container is not available. You must call "bootKernel()" or "createClient()" before calling "%s".', __METHOD__));
80+
}
81+
82+
if (
83+
(
84+
!static::$container->has('doctrine') ||
85+
null === $objectManager = static::$container->get('doctrine')->getManagerForClass($resourceClass)
86+
) &&
87+
(
88+
!static::$container->has('doctrine_mongodb') ||
89+
null === $objectManager = static::$container->get('doctrine_mongodb')->getManagerForClass($resourceClass)
90+
)
91+
) {
92+
throw new \RuntimeException(sprintf('"%s" only supports classes managed by Doctrine ORM or Doctrine MongoDB ODM. Override this method to implement your own retrieval logic if you don\'t use those libraries.', __METHOD__));
93+
}
94+
95+
$item = $objectManager->getRepository($resourceClass)->findOneBy($criteria);
96+
if (null === $item) {
97+
return null;
98+
}
99+
100+
return static::$container->get('api_platform.iri_converter')->getIriFromItem($item);
101+
}
72102
}

tests/Bridge/Symfony/Bundle/Test/ApiTestCaseTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Bundle\Test;
1515

1616
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
17+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Dummy as DummyDocument;
18+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
1719
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Model\ResourceInterface;
20+
use Doctrine\ORM\EntityManagerInterface;
21+
use Doctrine\ORM\Tools\SchemaTool;
1822
use PHPUnit\Framework\ExpectationFailedException;
1923
use PHPUnit\Runner\Version;
2024

@@ -153,4 +157,31 @@ public function testAssertArraySubsetDoesNothingForValidScenario(): void
153157

154158
$this->assertArraySubset([1, 2], [1, 2, 3]);
155159
}
160+
161+
public function testFindIriBy(): void
162+
{
163+
self::bootKernel();
164+
/**
165+
* @var EntityManagerInterface
166+
*/
167+
$manager = self::$container->get('doctrine')->getManager();
168+
$classes = $manager->getMetadataFactory()->getAllMetadata();
169+
$schemaTool = new SchemaTool($manager);
170+
171+
$schemaTool->dropSchema($classes);
172+
$schemaTool->createSchema($classes);
173+
174+
self::createClient()->request('POST', '/dummies', [
175+
'headers' => [
176+
'content-type' => 'application/json',
177+
'accept' => 'text/xml',
178+
],
179+
'body' => '{"name": "Kevin"}',
180+
]);
181+
$this->assertResponseIsSuccessful();
182+
183+
$resource = 'mongodb' === self::$container->getParameter('kernel.environment') ? DummyDocument::class : Dummy::class;
184+
$this->assertRegExp('~^/dummies/\d+~', self::findIriBy($resource, ['name' => 'Kevin']));
185+
$this->assertNull(self::findIriBy($resource, ['name' => 'not-exist']));
186+
}
156187
}

0 commit comments

Comments
 (0)