Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ jobs:
continue-on-error: true

postgresql:
name: Behat (PHP ${{ matrix.php }}) (PostgreSQL)
name: PHPUnit + Behat (PHP ${{ matrix.php }}) (PostgreSQL)
runs-on: ubuntu-latest
timeout-minutes: 20
strategy:
Expand Down Expand Up @@ -524,12 +524,16 @@ jobs:
composer global link .
- name: Clear test app cache
run: tests/Fixtures/app/console cache:clear --ansi
- name: Run PHPUnit tests
run: vendor/bin/phpunit
- name: Clear test app cache
run: tests/Fixtures/app/console cache:clear --ansi
- name: Run Behat tests
run: |
vendor/bin/behat --out=std --format=progress --profile=postgres --no-interaction -vv

mysql:
name: Behat (PHP ${{ matrix.php }}) (MySQL)
name: PHPUnit + Behat (PHP ${{ matrix.php }}) (MySQL)
runs-on: ubuntu-latest
timeout-minutes: 20
strategy:
Expand Down Expand Up @@ -575,6 +579,10 @@ jobs:
composer global link .
- name: Clear test app cache
run: tests/Fixtures/app/console cache:clear --ansi
- name: Run PHPUnit tests
run: vendor/bin/phpunit
- name: Clear test app cache
run: tests/Fixtures/app/console cache:clear --ansi
- name: Run Behat tests
run: vendor/bin/behat --out=std --format=progress --profile=default --no-interaction --tags '~@!mysql'

Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/TestBundle/Document/FilteredOrderParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
'date_null_always_first' => new QueryParameter(
filter: new OrderFilter(),
property: 'createdAt',
filterContext: OrderFilterInterface::NULLS_ALWAYS_FIRST,
filterContext: ['nulls_comparison' => OrderFilterInterface::NULLS_ALWAYS_FIRST],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: this was "tested" but in reality this is not implemented in ODM

nativeType: new BuiltinType(TypeIdentifier::STRING)
),
'date_null_always_first_old_way' => new QueryParameter(
filter: new OrderFilter(properties: ['createdAt' => OrderFilterInterface::NULLS_ALWAYS_FIRST]),
filter: new OrderFilter(properties: ['createdAt' => ['nulls_comparison' => OrderFilterInterface::NULLS_ALWAYS_FIRST]]),
property: 'createdAt',
nativeType: new BuiltinType(TypeIdentifier::STRING)
),
'order[:property]' => new QueryParameter(
filter: new OrderFilter(),
filterContext: OrderFilterInterface::NULLS_ALWAYS_FIRST,
filterContext: ['nulls_comparison' => OrderFilterInterface::NULLS_ALWAYS_FIRST],
),
],
)]
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestBundle/Entity/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function handleLinks(QueryBuilder $queryBuilder, array $uriVariabl
->addGroupBy(\sprintf('%s.id', $rootAlias));
}

public ?int $totalQuantity;
public int|string|null $totalQuantity;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on the DB it's an int or a string


#[ORM\Id]
#[ORM\GeneratedValue]
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixtures/TestBundle/Entity/FilteredOrderParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
'date_null_always_first' => new QueryParameter(
filter: new OrderFilter(),
property: 'createdAt',
filterContext: OrderFilterInterface::NULLS_ALWAYS_FIRST,
filterContext: ['nulls_comparison' => OrderFilterInterface::NULLS_ALWAYS_FIRST],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't correctly configured/tested ; I updated it.

nativeType: new BuiltinType(TypeIdentifier::STRING)
),
'date_null_always_first_old_way' => new QueryParameter(
filter: new OrderFilter(properties: ['createdAt' => OrderFilterInterface::NULLS_ALWAYS_FIRST]),
filter: new OrderFilter(properties: ['createdAt' => ['nulls_comparison' => OrderFilterInterface::NULLS_ALWAYS_FIRST]]),
property: 'createdAt',
nativeType: new BuiltinType(TypeIdentifier::STRING)
),
'order[:property]' => new QueryParameter(
filter: new OrderFilter(),
filterContext: OrderFilterInterface::NULLS_ALWAYS_FIRST,
filterContext: ['nulls_comparison' => OrderFilterInterface::NULLS_ALWAYS_FIRST],
),
],
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class NumericValidated
private ?int $id = null;

#[Assert\Range(min: 1, max: 10)]
#[ORM\Column]
#[ORM\Column(name: '_range')]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range is a reserved property in Mysql/Postgres

public int $range;

#[Assert\GreaterThan(value: 10)]
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/JsonStreamerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function testJsonStreamerWriteJsonLd(): void
$this->assertSame(0, $res['views']);
$this->assertSame(0, $res['rating']);
$this->assertFalse($res['isFeatured']);
$this->assertSame('0', $res['price']);
$this->assertContains($res['price'], ['0', '0.00']); // Depends on DB
$this->assertStringStartsWith('/json_stream_resources/', $res['@id']);
$this->assertSame('/contexts/JsonStreamResource', $res['@context']);

Expand Down Expand Up @@ -273,7 +273,7 @@ public function testJsonStreamerWriteJson(): void
$this->assertSame(0, $res['views']);
$this->assertSame(0, $res['rating']);
$this->assertFalse($res['isFeatured']);
$this->assertSame('0', $res['price']);
$this->assertContains($res['price'], ['0', '0.00']); // Depends on DB
$this->assertArrayNotHasKey('@id', $res);
$this->assertArrayNotHasKey('@type', $res);
$this->assertArrayNotHasKey('@context', $res);
Expand Down
47 changes: 38 additions & 9 deletions tests/Functional/Parameters/OrderFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Tests\Functional\Parameters;

use ApiPlatform\Doctrine\Odm\Filter\OrderFilter;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\FilteredOrderParameter as FilteredOrderParameterDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\FilteredOrderParameter;
Expand Down Expand Up @@ -58,6 +59,12 @@ public function testOrderFilterResponses(string $url, array $expectedOrder): voi

$actualOrder = array_map(fn ($item) => $item['createdAt'] ?? null, $orderedItems);

// Default NULL order is different in PostgreSQL.
if ($this->isPostgres()) {
$actualOrder = array_values(array_filter($actualOrder));
$expectedOrder = array_values(array_filter($expectedOrder));
}

$this->assertSame($expectedOrder, $actualOrder, \sprintf('Expected order does not match for URL %s', $url));
}

Expand All @@ -79,29 +86,51 @@ public static function orderFilterScenariosProvider(): \Generator
'/filtered_order_parameters?date=desc',
['2024-12-25T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-01-01T00:00:00+00:00', null],
];
yield 'date_null_always_first_alias_nulls_first' => [
}

#[DataProvider('orderFilterNullsComparisonScenariosProvider')]
public function testOrderFilterNullsComparisonResponses(string $url, array $expectedOrder): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped(\sprintf('Not implemented in %s', OrderFilter::class));
}

$response = self::createClient()->request('GET', $url);
$this->assertResponseIsSuccessful();

$responseData = $response->toArray();
$orderedItems = $responseData['hydra:member'];

$actualOrder = array_map(fn ($item) => $item['createdAt'] ?? null, $orderedItems);

$this->assertSame($expectedOrder, $actualOrder, \sprintf('Expected order does not match for URL %s', $url));
}

public static function orderFilterNullsComparisonScenariosProvider(): \Generator
{
yield 'date_null_always_first_alias_asc' => [
'/filtered_order_parameters?date_null_always_first=asc',
[null, '2024-01-01T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-12-25T00:00:00+00:00'],
];
yield 'date_null_always_first_alias_nulls_last' => [
yield 'date_null_always_first_alias_desc' => [
'/filtered_order_parameters?date_null_always_first=desc',
['2024-12-25T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-01-01T00:00:00+00:00', null],
[null, '2024-12-25T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-01-01T00:00:00+00:00'],
];
yield 'date_null_always_first_old_way_alias_nulls_first' => [
yield 'date_null_always_first_old_way_alias_asc' => [
'/filtered_order_parameters?date_null_always_first_old_way=asc',
[null, '2024-01-01T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-12-25T00:00:00+00:00'],
];
yield 'date_null_always_first_old_way_alias_nulls_last' => [
yield 'date_null_always_first_old_way_alias_desc' => [
'/filtered_order_parameters?date_null_always_first_old_way=desc',
['2024-12-25T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-01-01T00:00:00+00:00', null],
[null, '2024-12-25T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-01-01T00:00:00+00:00'],
];
yield 'order_property_created_at_nulls_first' => [
yield 'order_property_created_at_null_first_asc' => [
'/filtered_order_parameters?order[createdAt]=asc',
[null, '2024-01-01T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-12-25T00:00:00+00:00'],
];
yield 'order_property_created_at_nulls_last' => [
yield 'order_property_created_at_null_first_desc' => [
'/filtered_order_parameters?order[createdAt]=desc',
['2024-12-25T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-01-01T00:00:00+00:00', null],
[null, '2024-12-25T00:00:00+00:00', '2024-06-15T00:00:00+00:00', '2024-01-01T00:00:00+00:00'],
];
}

Expand Down
42 changes: 36 additions & 6 deletions tests/RecreateSchemaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,52 @@ private function recreateSchema(array $classes = []): void
return;
}

/** @var ClassMetadata[] $cl */
$cl = [];
foreach ($classes as $c) {
$cl[] = $manager->getMetadataFactory()->getMetadataFor($c);
/** @var ClassMetadata[] $metadataCollection */
$metadataCollection = [];
$processedClasses = [];

foreach ($classes as $class) {
$this->addMetadataWithDependencies($manager, $class, $metadataCollection, $processedClasses);
}

$schemaTool = new SchemaTool($manager);
@$schemaTool->dropSchema($cl);
@$schemaTool->createSchema($cl);

@$schemaTool->dropDatabase();
@$schemaTool->createSchema($metadataCollection);
}

/**
* @param array<ClassMetadata> $metadataCollection
* @param array<string, bool> $processedClasses
*
* @param-out array<ClassMetadata> $metadataCollection
* @param-out array<string, bool> $processedClasses
*/
private function addMetadataWithDependencies(EntityManagerInterface $manager, string $class, array &$metadataCollection, array &$processedClasses): void
{
if (isset($processedClasses[$class])) {
return;
}

$metadata = $manager->getMetadataFactory()->getMetadataFor($class);
$metadataCollection[] = $metadata;
$processedClasses[$class] = true;

foreach ($metadata->getAssociationMappings() as $associationMapping) {
$this->addMetadataWithDependencies($manager, $associationMapping->targetEntity, $metadataCollection, $processedClasses);
}
}

private function isMongoDB(): bool
{
return 'mongodb' === static::getContainer()->getParameter('kernel.environment');
}

private function isPostgres(): bool
{
return 'postgres' === static::getContainer()->getParameter('kernel.environment');
}

private function getManager(): EntityManagerInterface|DocumentManager
{
return static::getContainer()->get($this->isMongoDB() ? 'doctrine_mongodb' : 'doctrine')->getManager();
Expand Down
Loading