forked from sofascore/purgatory-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssociationResolverTestCase.php
More file actions
185 lines (157 loc) · 7.04 KB
/
Copy pathAssociationResolverTestCase.php
File metadata and controls
185 lines (157 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
namespace Sofascore\PurgatoryBundle\Tests\Cache\PropertyResolver;
use Doctrine\ORM\Mapping\ClassMetadata;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Sofascore\PurgatoryBundle\Attribute\PurgeOn;
use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\PropertyValues;
use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\RawValues;
use Sofascore\PurgatoryBundle\Attribute\Target\ForProperties;
use Sofascore\PurgatoryBundle\Cache\PropertyResolver\AssociationResolver;
use Sofascore\PurgatoryBundle\Cache\RouteMetadata\RouteMetadata;
use Sofascore\PurgatoryBundle\Cache\Subscription\PurgeSubscription;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\PropertyInfo\PropertyReadInfo;
use Symfony\Component\PropertyInfo\PropertyReadInfoExtractorInterface;
use Symfony\Component\Routing\Route;
abstract class AssociationResolverTestCase extends TestCase
{
abstract protected function createAssociationMapping(array $associationMappingConfig): mixed;
#[DataProvider('associationProvider')]
public function testResolveAssociations(
array $associationMapping,
bool $isGetAssociationMappedByTargetFieldCalled,
bool $isAssociationInverseSide,
): void {
$extractor = $this->createMock(PropertyReadInfoExtractorInterface::class);
$extractor->method('getReadInfo')
->with('BarEntity', 'barProperty')
->willReturn(
new PropertyReadInfo(
type: PropertyReadInfo::TYPE_METHOD,
name: 'getFoo',
visibility: PropertyReadInfo::VISIBILITY_PUBLIC,
static: false,
byRef: false,
),
);
$resolver = new AssociationResolver($extractor);
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->method('hasAssociation')
->with('fooProperty')
->willReturn(true);
$classMetadata->method('isAssociationInverseSide')
->with('fooProperty')
->willReturn($isAssociationInverseSide);
$classMetadata->method('getAssociationMapping')
->with('fooProperty')
->willReturn($this->createAssociationMapping($associationMapping));
if ($isGetAssociationMappedByTargetFieldCalled) {
$classMetadata->expects(self::once())
->method('getAssociationMappedByTargetField')
->with('fooProperty')
->willReturn('barProperty');
} else {
$classMetadata->expects(self::never())
->method('getAssociationMappedByTargetField');
}
$classMetadata->method('getAssociationTargetClass')
->with('fooProperty')
->willReturn('BarEntity');
$purgeSubscription = $resolver->resolveSubscription(
routeMetadata: new RouteMetadata(
routeName: 'route_foo',
route: new Route('/foo/{param1}/{param2}'),
purgeOn: new PurgeOn(
class: 'FooEntity',
target: new ForProperties(['fooProperty']),
if: new Expression('obj.isActive() === true'),
),
reflectionMethod: $this->createMock(\ReflectionMethod::class),
),
classMetadata: $classMetadata,
routeParams: [
'param1' => new PropertyValues('bazProperty'),
'param2' => new RawValues('const'),
],
target: 'fooProperty',
);
/** @var PurgeSubscription[] $subscription */
$subscription = [...$purgeSubscription];
self::assertTrue($purgeSubscription->getReturn());
self::assertContainsOnlyInstancesOf(PurgeSubscription::class, $subscription);
self::assertCount(1, $subscription);
self::assertNull($subscription[0]->property);
self::assertSame('BarEntity', $subscription[0]->class);
self::assertEquals(
new PropertyValues(Kernel::MAJOR_VERSION > 5 ? 'barProperty?.bazProperty' : 'barProperty.bazProperty'),
$subscription[0]->routeParams['param1'],
);
self::assertEquals(new RawValues('const'), $subscription[0]->routeParams['param2']);
self::assertSame('obj.getFoo() !== null && (obj.getFoo().isActive() === true)', (string) $subscription[0]->if);
}
abstract public static function associationProvider(): iterable;
public function testFieldNotAssociation(): void
{
$resolver = new AssociationResolver(
$this->createMock(PropertyReadInfoExtractorInterface::class),
);
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->method('hasAssociation')
->with('fooProperty')
->willReturn(false);
$purgeSubscriptions = $resolver->resolveSubscription(
routeMetadata: new RouteMetadata(
routeName: 'route_foo',
route: new Route('/foo'),
purgeOn: new PurgeOn(
class: 'FooEntity',
target: new ForProperties(['fooProperty']),
),
reflectionMethod: $this->createMock(\ReflectionMethod::class),
),
classMetadata: $classMetadata,
routeParams: [],
target: 'fooProperty',
);
$subscriptions = [...$purgeSubscriptions];
self::assertFalse($purgeSubscriptions->getReturn());
self::assertCount(0, $subscriptions);
}
#[DataProvider('invalidAssociationProvider')]
public function testInvalidAssociationType(array $associationMapping): void
{
$resolver = new AssociationResolver(
$this->createMock(PropertyReadInfoExtractorInterface::class),
);
$classMetadata = $this->createMock(ClassMetadata::class);
$classMetadata->expects(self::once())
->method('hasAssociation')
->with('fooProperty')
->willReturn(true);
$classMetadata->expects(self::once())
->method('getAssociationMapping')
->with('fooProperty')
->willReturn($this->createAssociationMapping($associationMapping));
$purgeSubscriptions = $resolver->resolveSubscription(
routeMetadata: new RouteMetadata(
routeName: 'route_foo',
route: new Route('/foo'),
purgeOn: new PurgeOn(
class: 'FooEntity',
target: new ForProperties(['fooProperty']),
),
reflectionMethod: $this->createMock(\ReflectionMethod::class),
),
classMetadata: $classMetadata,
routeParams: [],
target: 'fooProperty',
);
$subscriptions = [...$purgeSubscriptions];
self::assertFalse($purgeSubscriptions->getReturn());
self::assertCount(0, $subscriptions);
}
abstract public static function invalidAssociationProvider(): iterable;
}