Skip to content

Commit b656fd3

Browse files
committed
Improving code coverage
1 parent 652cfca commit b656fd3

File tree

7 files changed

+54
-3
lines changed

7 files changed

+54
-3
lines changed

docs/annotations_reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ query / mutation / field (according to the `@Logged` and `@Right` annotations).
123123

124124
Attribute | Compulsory | Type | Definition
125125
---------------|------------|------|--------
126-
*default* | *yes* | mixed | The value to return if the user is not authorized.
126+
value | *yes* | mixed | The value to return if the user is not authorized.
127127

128128
## @HideIfUnauthorized annotation
129129

docs/authentication_authorization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class UserController
101101
#[Query]
102102
#[Logged]
103103
#[Right("CAN_VIEW_USER_LIST")]
104-
#[FailWith(null)]
104+
#[FailWith(value: null)]
105105
public function users(int $limit, int $offset): array
106106
{
107107
// ...

src/Annotations/FailWith.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use BadMethodCallException;
99

1010
use function array_key_exists;
11+
use function is_array;
1112

1213
/**
1314
* @Annotation
@@ -37,7 +38,7 @@ public function __construct($values = [], $value = '__fail__with__magic__key__')
3738
{
3839
if ($value !== '__fail__with__magic__key__') {
3940
$this->value = $value;
40-
} elseif (array_key_exists('value', $values)) {
41+
} elseif (is_array($values) && array_key_exists('value', $values)) {
4142
$this->value = $values['value'];
4243
} else {
4344
throw new BadMethodCallException('The @FailWith annotation must be passed a defaultValue. For instance: "@FailWith(null)"');

tests/Annotations/DecorateTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BadMethodCallException;
66
use PHPUnit\Framework\TestCase;
7+
use ReflectionMethod;
78

89
class DecorateTest extends TestCase
910
{
@@ -14,4 +15,18 @@ public function testException(): void
1415
$this->expectExceptionMessage('The @Decorate annotation must be passed an input type. For instance: "@Decorate("MyInputType")"');
1516
new Decorate([]);
1617
}
18+
19+
/**
20+
* @requires PHP >= 8.0
21+
*/
22+
public function testPhp8Annotation(): void
23+
{
24+
$method = new ReflectionMethod(__CLASS__, 'method1');
25+
$attribute = $method->getAttributes()[0]->newInstance();
26+
$this->assertSame('foobar', $attribute->getInputTypeName());
27+
}
28+
29+
#[Decorate("foobar")]
30+
public function method1(): void {
31+
}
1732
}

tests/Annotations/FailWithTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BadMethodCallException;
66
use PHPUnit\Framework\TestCase;
7+
use ReflectionMethod;
78

89
class FailWithTest extends TestCase
910
{
@@ -14,4 +15,19 @@ public function testException(): void
1415
$this->expectExceptionMessage('The @FailWith annotation must be passed a defaultValue. For instance: "@FailWith(null)"');
1516
new FailWith([]);
1617
}
18+
19+
/**
20+
* @requires PHP >= 8.0
21+
*/
22+
public function testPhp8Annotation(): void
23+
{
24+
$method = new ReflectionMethod(__CLASS__, 'method1');
25+
$failWith = $method->getAttributes()[0]->newInstance();
26+
$this->assertSame(null, $failWith->getValue());
27+
}
28+
29+
#[FailWith(value: null)]
30+
public function method1(): void {
31+
}
32+
1733
}

tests/Annotations/SecurityTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use BadMethodCallException;
66
use PHPUnit\Framework\TestCase;
7+
use stdClass;
8+
use TypeError;
79

810
class SecurityTest extends TestCase
911
{
@@ -21,4 +23,11 @@ public function testIncompatibleParams(): void
2123
$this->expectExceptionMessage('A @Security annotation that has "failWith" attribute set cannot have a message or a statusCode attribute.');
2224
new Security(['expression'=>'foo', 'failWith'=>null, 'statusCode'=>500]);
2325
}
26+
27+
public function testBadParams2(): void
28+
{
29+
$this->expectException(TypeError::class);
30+
$this->expectExceptionMessage('"TheCodingMachine\GraphQLite\Annotations\Security::__construct": Argument $data is expected to be a string or array, got "object".');
31+
new Security(new stdClass());
32+
}
2433
}

tests/Annotations/UseInputTypeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BadMethodCallException;
66
use PHPUnit\Framework\TestCase;
7+
use ReflectionMethod;
78

89
class UseInputTypeTest extends TestCase
910
{
@@ -21,4 +22,13 @@ public function testException2(): void
2122
$this->expectExceptionMessage('The @UseInputType annotation must be passed a target and an input type. For instance: "@UseInputType(for="$input", inputType="MyInputType")" in PHP 7+ or #[UseInputType("MyInputType")] in PHP 8+');
2223
(new UseInputType(['inputType' => 'foo']))->getTarget();
2324
}
25+
26+
/**
27+
* @requires PHP >= 8.0
28+
*/
29+
public function testPhp8Annotation(): void
30+
{
31+
$attribute = new UseInputType('foo');
32+
$this->assertSame('foo', $attribute->getInputType());
33+
}
2434
}

0 commit comments

Comments
 (0)