Skip to content

Commit

Permalink
Add test case covering parallel usage of attributes and annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
W0rma committed Oct 31, 2024
1 parent 9e8a232 commit 0861943
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Hateoas\Tests\Fixtures\Attribute;

use Hateoas\Configuration\Annotation as Hateoas;
use Hateoas\Configuration\Relation;
use JMS\Serializer\Annotation as Serializer;

/**
* @Hateoas\Relation(
* "self",
* href = "http://adrienbrault.fr",
* exclusion = @Hateoas\Exclusion(
* groups = {"Default", "simple"},
* excludeIf = "expr(object.firstName !== 'Adrien' || object.lastName !== 'Brault')"
* )
* )
* @Hateoas\Relation(
* "broken-computer",
*
* embedded = "expr(object.getWindowsComputer())"
* )
*/
#[Hateoas\Relation(
'self',
href: 'http://adrienbrault.fr',
exclusion: new Hateoas\Exclusion(
groups: ['Default', 'simple'],
excludeIf: "expr(object.firstName !== 'Adrien' || object.lastName !== 'Brault')",
)
)]
#[Hateoas\Relation(
'computer',
href: 'http://www.apple.com/macbook-pro/',
exclusion: new Hateoas\Exclusion(groups: ['Default', 'simple']),
embedded: new Hateoas\Embedded(
'expr(object.getMacbookPro())',
type: 'Hateoas\Tests\Fixtures\Computer',
exclusion: new Hateoas\Exclusion(groups: ['Default']),
),
)]
#[Hateoas\Relation(
'broken-computer',
embedded: 'expr(object.getWindowsComputer())',
)]
#[Hateoas\Relation(
'smartphone',
embedded: 'expr(object.getiOSSmartphone())',
)]
#[Hateoas\Relation(
'smartphone',
embedded: 'expr(object.getAndroidSmartphone())',
)]
#[Hateoas\RelationProvider(name: 'Hateoas\Tests\Fixtures\Attribute\AdrienBrault::getRelations')]
class AdrienBraultAttributesAndAnnotations
{
#[Serializer\Groups(['Default', 'simple'])]
public $firstName = 'Adrien';

#[Serializer\Groups(['Default', 'simple'])]
public $lastName = 'Brault';

public function getMacbookPro()
{
return new Computer('MacBook Pro');
}

public function getWindowsComputer()
{
return new Computer('Windows Computer');
}

public function getiOSSmartphone()
{
return new Smartphone('iPhone 6');
}

public function getAndroidSmartphone()
{
return new Smartphone('Nexus 5');
}

public static function getRelations()
{
return [
new Relation('dynamic-relation', 'awesome!!!', ['wowowow']),
];
}
}
33 changes: 24 additions & 9 deletions tests/Hateoas/Tests/HateoasBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,13 @@ public function testBuild()
$this->assertInstanceOf(SerializerInterface::class, $hateoas);
}

public function testSerializeAdrienBraultWithExclusion()
/**
* @dataProvider getTestSerializeAdrienBraultWithExclusionData
*/
public function testSerializeAdrienBraultWithExclusion($adrienBrault, $fakeAdrienBrault)
{
$hateoas = HateoasBuilder::buildHateoas();

if (class_exists(AnnotationReader::class)) {
$adrienBrault = new AdrienBrault();
$fakeAdrienBrault = new AdrienBrault();
} else {
$adrienBrault = new Attribute\AdrienBrault();
$fakeAdrienBrault = new Attribute\AdrienBrault();
}

$fakeAdrienBrault->firstName = 'John';
$fakeAdrienBrault->lastName = 'Smith';

Expand Down Expand Up @@ -76,6 +71,26 @@ public function testSerializeAdrienBraultWithExclusion()
);
}

private static function getTestSerializeAdrienBraultWithExclusionData(): iterable
{
yield [
new Attribute\AdrienBrault(),
new Attribute\AdrienBrault(),
];

if (class_exists(AnnotationReader::class)) {
yield [
new AdrienBrault(),
new AdrienBrault(),
];

yield [
new Attribute\AdrienBraultAttributesAndAnnotations(),
new Attribute\AdrienBraultAttributesAndAnnotations(),
];
}
}

public function testAlternativeUrlGenerator()
{
$brokenUrlGenerator = new CallableUrlGenerator(function ($name, $parameters) {
Expand Down

0 comments on commit 0861943

Please sign in to comment.