Skip to content

Commit 0861943

Browse files
committed
Add test case covering parallel usage of attributes and annotations
1 parent 9e8a232 commit 0861943

File tree

2 files changed

+115
-9
lines changed

2 files changed

+115
-9
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hateoas\Tests\Fixtures\Attribute;
6+
7+
use Hateoas\Configuration\Annotation as Hateoas;
8+
use Hateoas\Configuration\Relation;
9+
use JMS\Serializer\Annotation as Serializer;
10+
11+
/**
12+
* @Hateoas\Relation(
13+
* "self",
14+
* href = "http://adrienbrault.fr",
15+
* exclusion = @Hateoas\Exclusion(
16+
* groups = {"Default", "simple"},
17+
* excludeIf = "expr(object.firstName !== 'Adrien' || object.lastName !== 'Brault')"
18+
* )
19+
* )
20+
* @Hateoas\Relation(
21+
* "broken-computer",
22+
*
23+
* embedded = "expr(object.getWindowsComputer())"
24+
* )
25+
*/
26+
#[Hateoas\Relation(
27+
'self',
28+
href: 'http://adrienbrault.fr',
29+
exclusion: new Hateoas\Exclusion(
30+
groups: ['Default', 'simple'],
31+
excludeIf: "expr(object.firstName !== 'Adrien' || object.lastName !== 'Brault')",
32+
)
33+
)]
34+
#[Hateoas\Relation(
35+
'computer',
36+
href: 'http://www.apple.com/macbook-pro/',
37+
exclusion: new Hateoas\Exclusion(groups: ['Default', 'simple']),
38+
embedded: new Hateoas\Embedded(
39+
'expr(object.getMacbookPro())',
40+
type: 'Hateoas\Tests\Fixtures\Computer',
41+
exclusion: new Hateoas\Exclusion(groups: ['Default']),
42+
),
43+
)]
44+
#[Hateoas\Relation(
45+
'broken-computer',
46+
embedded: 'expr(object.getWindowsComputer())',
47+
)]
48+
#[Hateoas\Relation(
49+
'smartphone',
50+
embedded: 'expr(object.getiOSSmartphone())',
51+
)]
52+
#[Hateoas\Relation(
53+
'smartphone',
54+
embedded: 'expr(object.getAndroidSmartphone())',
55+
)]
56+
#[Hateoas\RelationProvider(name: 'Hateoas\Tests\Fixtures\Attribute\AdrienBrault::getRelations')]
57+
class AdrienBraultAttributesAndAnnotations
58+
{
59+
#[Serializer\Groups(['Default', 'simple'])]
60+
public $firstName = 'Adrien';
61+
62+
#[Serializer\Groups(['Default', 'simple'])]
63+
public $lastName = 'Brault';
64+
65+
public function getMacbookPro()
66+
{
67+
return new Computer('MacBook Pro');
68+
}
69+
70+
public function getWindowsComputer()
71+
{
72+
return new Computer('Windows Computer');
73+
}
74+
75+
public function getiOSSmartphone()
76+
{
77+
return new Smartphone('iPhone 6');
78+
}
79+
80+
public function getAndroidSmartphone()
81+
{
82+
return new Smartphone('Nexus 5');
83+
}
84+
85+
public static function getRelations()
86+
{
87+
return [
88+
new Relation('dynamic-relation', 'awesome!!!', ['wowowow']),
89+
];
90+
}
91+
}

tests/Hateoas/Tests/HateoasBuilderTest.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,13 @@ public function testBuild()
2929
$this->assertInstanceOf(SerializerInterface::class, $hateoas);
3030
}
3131

32-
public function testSerializeAdrienBraultWithExclusion()
32+
/**
33+
* @dataProvider getTestSerializeAdrienBraultWithExclusionData
34+
*/
35+
public function testSerializeAdrienBraultWithExclusion($adrienBrault, $fakeAdrienBrault)
3336
{
3437
$hateoas = HateoasBuilder::buildHateoas();
3538

36-
if (class_exists(AnnotationReader::class)) {
37-
$adrienBrault = new AdrienBrault();
38-
$fakeAdrienBrault = new AdrienBrault();
39-
} else {
40-
$adrienBrault = new Attribute\AdrienBrault();
41-
$fakeAdrienBrault = new Attribute\AdrienBrault();
42-
}
43-
4439
$fakeAdrienBrault->firstName = 'John';
4540
$fakeAdrienBrault->lastName = 'Smith';
4641

@@ -76,6 +71,26 @@ public function testSerializeAdrienBraultWithExclusion()
7671
);
7772
}
7873

74+
private static function getTestSerializeAdrienBraultWithExclusionData(): iterable
75+
{
76+
yield [
77+
new Attribute\AdrienBrault(),
78+
new Attribute\AdrienBrault(),
79+
];
80+
81+
if (class_exists(AnnotationReader::class)) {
82+
yield [
83+
new AdrienBrault(),
84+
new AdrienBrault(),
85+
];
86+
87+
yield [
88+
new Attribute\AdrienBraultAttributesAndAnnotations(),
89+
new Attribute\AdrienBraultAttributesAndAnnotations(),
90+
];
91+
}
92+
}
93+
7994
public function testAlternativeUrlGenerator()
8095
{
8196
$brokenUrlGenerator = new CallableUrlGenerator(function ($name, $parameters) {

0 commit comments

Comments
 (0)