diff --git a/tests/Fixtures/TestBundle/Document/Issue7349/Foo7349.php b/tests/Fixtures/TestBundle/Document/Issue7349/Foo7349.php new file mode 100644 index 00000000000..bed74e5eecf --- /dev/null +++ b/tests/Fixtures/TestBundle/Document/Issue7349/Foo7349.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Tests\Fixtures\TestBundle\Document\Issue7349; + +use ApiPlatform\Metadata\ApiResource; +use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; + +/** + * Foo7349. + * + * @author Maxime Valin + */ +#[ApiResource] +#[ODM\Document] +class Foo7349 +{ + /** + * @var int id + */ + #[ODM\Id(type: 'int', strategy: 'INCREMENT')] + private int $id; + + #[ODM\Field(type: 'string')] + private string $name; + + public function getId(): ?int + { + return $this->id; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): void + { + $this->name = $name; + } +} diff --git a/tests/Functional/Issues/Issue7349Test.php b/tests/Functional/Issues/Issue7349Test.php new file mode 100644 index 00000000000..d81fd82c4f2 --- /dev/null +++ b/tests/Functional/Issues/Issue7349Test.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Tests\Functional\Issues; + +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; +use ApiPlatform\Tests\RecreateSchemaTrait; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Orchestra\Testbench\Concerns\WithWorkbench; +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; + +class Issue7349Test extends ApiTestCase +{ + use RecreateSchemaTrait; + use RefreshDatabase; + use WithWorkbench; + + /** + * When using partial pagination, totalItems should not be present. + */ + public function testGetPartialNoItemCount(): void + { + if (!$this->isMongoDB()) { + $this->markTestSkipped(); + } + + $response = self::createClient()->request('GET', '/foo7349s?page=1&itemsPerPage=3', [ + 'headers' => [ + 'Accept' => 'application/ld+json', + ], + ]); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertArrayNotHasKey('hydra:totalItems', $response->toArray()); + } + + /** + * When not using partial pagination, totalItems should be present. + * + * @throws RedirectionExceptionInterface + * @throws DecodingExceptionInterface + * @throws ClientExceptionInterface + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + */ + public function testGetItemCount(): void + { + if (!$this->isMongoDB()) { + $this->markTestSkipped(); + } + + $response = self::createClient()->request('GET', '/foo7349s?page=1&itemsPerPage=3', [ + 'headers' => [ + 'Accept' => 'application/ld+json', + ], + ]); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertArrayHasKey('hydra:totalItems', $response->toArray()); + } +}