Skip to content

Commit d65edca

Browse files
authored
Improve code coverage (#122)
1 parent d53166b commit d65edca

4 files changed

Lines changed: 143 additions & 57 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sofascore\PurgatoryBundle\Tests\Attribute\RouteParamValue;
6+
7+
use PHPUnit\Framework\Attributes\TestWith;
8+
use PHPUnit\Framework\TestCase;
9+
use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\EnumValues;
10+
use Sofascore\PurgatoryBundle\Exception\InvalidArgumentException;
11+
12+
final class EnumValuesTest extends TestCase
13+
{
14+
/**
15+
* @param class-string $class
16+
*/
17+
#[TestWith([\stdClass::class])]
18+
#[TestWith([SomeUnitEnum::class])]
19+
public function testExceptionIsThrownWhenClassIsNotABackedEnum(string $class): void
20+
{
21+
$this->expectException(InvalidArgumentException::class);
22+
$this->expectExceptionMessage('The argument must be a backed enum.');
23+
24+
new EnumValues($class);
25+
}
26+
}
27+
28+
enum SomeUnitEnum
29+
{
30+
case One;
31+
case Two;
32+
}

tests/Cache/Subscription/PurgeSubscriptionProviderTest.php

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -360,63 +360,6 @@ public function testWithMissingRouteParams(
360360
[...$purgeSubscriptionProvider->provide()];
361361
}
362362

363-
#[TestWith([
364-
'if' => 'invalidObj.getMethod()',
365-
'expectedMessage' => 'Invalid "if" expression provided for route "foo": "Variable "invalidObj" is not valid around position 1 for expression `invalidObj.getMethod()`."',
366-
])]
367-
#[TestWith([
368-
'if' => 'entity !== null',
369-
'expectedMessage' => 'Invalid "if" expression provided for route "foo": "Variable "entity" is not valid around position 1 for expression `entity !== null`."',
370-
])]
371-
#[TestWith([
372-
'if' => 'some_function(obj)',
373-
'expectedMessage' => 'Invalid "if" expression provided for route "foo": "The function "some_function" does not exist around position 1 for expression `some_function(obj)`."',
374-
])]
375-
#[TestWith([
376-
'if' => 'valid_function(author)',
377-
'expectedMessage' => 'Invalid "if" expression provided for route "foo": "Variable "author" is not valid around position 16 for expression `valid_function(author)`."',
378-
])]
379-
public function testExceptionIsThrownOnInvalidIfExpression(string $if, string $expectedMessage): void
380-
{
381-
$routeMetadataProvider = self::createStub(RouteMetadataProviderInterface::class);
382-
$routeMetadataProvider->method('provide')
383-
->willReturnCallback(function () use ($if): iterable {
384-
yield new RouteMetadata(
385-
routeName: 'foo',
386-
route: new Route('/{foo}'),
387-
purgeOn: new PurgeOn(
388-
class: 'FooEntity',
389-
if: $if,
390-
),
391-
reflectionMethod: null,
392-
);
393-
});
394-
395-
$purgeSubscriptionProvider = new PurgeSubscriptionProvider(
396-
subscriptionResolvers: [],
397-
routeMetadataProviders: [$routeMetadataProvider],
398-
managerRegistry: self::createStub(ManagerRegistry::class),
399-
targetResolverLocator: self::createStub(ContainerInterface::class),
400-
expressionLanguage: new ExpressionLanguage(
401-
providers: [
402-
new class implements ExpressionFunctionProviderInterface {
403-
public function getFunctions(): array
404-
{
405-
return [
406-
new ExpressionFunction('valid_function', function () {}, function () {}),
407-
];
408-
}
409-
},
410-
],
411-
),
412-
);
413-
414-
$this->expectException(InvalidIfExpressionException::class);
415-
$this->expectExceptionMessage($expectedMessage);
416-
417-
[...$purgeSubscriptionProvider->provide()];
418-
}
419-
420363
public static function provideRouteMetadataWithMissingPurgeRouteParams(): iterable
421364
{
422365
$route = new Route(
@@ -542,4 +485,61 @@ class: 'FooEntity',
542485
'expectedMissingRequiredParameters' => ['foo', 'baz'],
543486
];
544487
}
488+
489+
#[TestWith([
490+
'if' => 'invalidObj.getMethod()',
491+
'expectedMessage' => 'Invalid "if" expression provided for route "foo": "Variable "invalidObj" is not valid around position 1 for expression `invalidObj.getMethod()`."',
492+
])]
493+
#[TestWith([
494+
'if' => 'entity !== null',
495+
'expectedMessage' => 'Invalid "if" expression provided for route "foo": "Variable "entity" is not valid around position 1 for expression `entity !== null`."',
496+
])]
497+
#[TestWith([
498+
'if' => 'some_function(obj)',
499+
'expectedMessage' => 'Invalid "if" expression provided for route "foo": "The function "some_function" does not exist around position 1 for expression `some_function(obj)`."',
500+
])]
501+
#[TestWith([
502+
'if' => 'valid_function(author)',
503+
'expectedMessage' => 'Invalid "if" expression provided for route "foo": "Variable "author" is not valid around position 16 for expression `valid_function(author)`."',
504+
])]
505+
public function testExceptionIsThrownOnInvalidIfExpression(string $if, string $expectedMessage): void
506+
{
507+
$routeMetadataProvider = self::createStub(RouteMetadataProviderInterface::class);
508+
$routeMetadataProvider->method('provide')
509+
->willReturnCallback(function () use ($if): iterable {
510+
yield new RouteMetadata(
511+
routeName: 'foo',
512+
route: new Route('/{foo}'),
513+
purgeOn: new PurgeOn(
514+
class: 'FooEntity',
515+
if: $if,
516+
),
517+
reflectionMethod: null,
518+
);
519+
});
520+
521+
$purgeSubscriptionProvider = new PurgeSubscriptionProvider(
522+
subscriptionResolvers: [],
523+
routeMetadataProviders: [$routeMetadataProvider],
524+
managerRegistry: self::createStub(ManagerRegistry::class),
525+
targetResolverLocator: self::createStub(ContainerInterface::class),
526+
expressionLanguage: new ExpressionLanguage(
527+
providers: [
528+
new class implements ExpressionFunctionProviderInterface {
529+
public function getFunctions(): array
530+
{
531+
return [
532+
new ExpressionFunction('valid_function', function () {}, function () {}),
533+
];
534+
}
535+
},
536+
],
537+
),
538+
);
539+
540+
$this->expectException(InvalidIfExpressionException::class);
541+
$this->expectExceptionMessage($expectedMessage);
542+
543+
[...$purgeSubscriptionProvider->provide()];
544+
}
545545
}

tests/Cache/TargetResolver/ForGroupsResolverTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Sofascore\PurgatoryBundle\Attribute\Target\ForGroups;
1111
use Sofascore\PurgatoryBundle\Cache\RouteMetadata\RouteMetadata;
1212
use Sofascore\PurgatoryBundle\Cache\TargetResolver\ForGroupsResolver;
13+
use Sofascore\PurgatoryBundle\Exception\LogicException;
14+
use Sofascore\PurgatoryBundle\Exception\RuntimeException;
1315
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
1416
use Symfony\Component\Routing\Route;
1517

@@ -39,4 +41,35 @@ class: 'FooEntity',
3941

4042
self::assertSame(['property1', 'property2'], $resolved);
4143
}
44+
45+
public function testExceptionIsThrownWhenPropertiesCannotBeResolved(): void
46+
{
47+
$propertyListExtractor = self::createStub(PropertyListExtractorInterface::class);
48+
$propertyListExtractor->method('getProperties')->willReturn(null);
49+
50+
$resolver = new ForGroupsResolver($propertyListExtractor);
51+
52+
$routeMetadata = new RouteMetadata(
53+
routeName: 'route_foo',
54+
route: new Route('/foo'),
55+
purgeOn: new PurgeOn(
56+
class: 'FooEntity',
57+
target: $target = new ForGroups(['group1']),
58+
),
59+
reflectionMethod: self::createStub(\ReflectionMethod::class),
60+
);
61+
62+
$this->expectException(RuntimeException::class);
63+
$this->expectExceptionMessage('Could not resolve properties for groups "group1" in class "FooEntity".');
64+
65+
$resolver->resolve($target, $routeMetadata);
66+
}
67+
68+
public function testExceptionIsThrownWhenSerializerExtractorIsNotAvailable(): void
69+
{
70+
$this->expectException(LogicException::class);
71+
$this->expectExceptionMessage('You cannot use the "ForGroups" attribute because the Symfony Serializer component is not installed. Try running "composer require symfony/serializer".');
72+
73+
new ForGroupsResolver(null);
74+
}
4275
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sofascore\PurgatoryBundle\Tests\Purger\Messenger;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use Sofascore\PurgatoryBundle\Purger\Messenger\PurgeMessage;
10+
11+
#[CoversClass(PurgeMessage::class)]
12+
final class PurgeMessageTest extends TestCase
13+
{
14+
public function testExceptionIsThrownWhenArrayIsEmpty(): void
15+
{
16+
$this->expectException(\ValueError::class);
17+
$this->expectExceptionMessage('The list must contain at least one URL.');
18+
19+
new PurgeMessage([]);
20+
}
21+
}

0 commit comments

Comments
 (0)