-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMethodDeprecationTest.php
More file actions
128 lines (107 loc) · 4.15 KB
/
Copy pathMethodDeprecationTest.php
File metadata and controls
128 lines (107 loc) · 4.15 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
<?php
declare(strict_types=1);
namespace ApiTest\Unit\App\Attribute;
use Api\App\Attribute\MethodDeprecation;
use Api\App\Exception\SunsetException;
use Api\App\Middleware\DeprecationMiddleware;
use Laminas\Diactoros\Response\JsonResponse;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionClass;
class MethodDeprecationTest extends TestCase
{
public function testInvalidDateThrowsException(): void
{
$class = new class implements RequestHandlerInterface{
#[MethodDeprecation(
sunset: 'invalid-01-01',
link: 'test-link',
deprecationReason: 'test-deprecation-reason',
)]
public function test(): void
{
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new JsonResponse('test');
}
};
$reflectionClass = new ReflectionClass($class);
$attributes = $this->getAttributes($reflectionClass);
$this->expectException(SunsetException::class);
$attributes[0]->newInstance();
}
public function testValidDatePassesValidation(): void
{
$class = new class implements RequestHandlerInterface{
#[MethodDeprecation(
sunset: '2038-01-01',
link: 'test-link',
deprecationReason: 'test-deprecation-reason',
)]
public function test(): void
{
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new JsonResponse('test');
}
};
$reflectionClass = new ReflectionClass($class);
$attributes = $this->getAttributes($reflectionClass);
$attribute = $attributes[0]->newInstance();
$this->assertSame('2038-01-01', $attribute->sunset);
$this->assertSame('test-link', $attribute->link);
$this->assertSame('test-deprecation-reason', $attribute->deprecationReason);
}
public function testToArray(): void
{
$class = new class implements RequestHandlerInterface{
#[MethodDeprecation(
sunset: '2038-01-01',
link: 'test-link',
deprecationReason: 'test-deprecation-reason',
rel: 'test-rel',
type: 'test-type',
)]
public function test(): void
{
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new JsonResponse('test');
}
};
$reflectionClass = new ReflectionClass($class);
$attributes = $this->getAttributes($reflectionClass);
$this->assertNotEmpty($attributes);
$attribute = $attributes[0]->newInstance();
$array = $attribute->toArray();
$this->assertIsArray($array);
$this->assertNotEmpty($array);
$this->assertArrayHasKey('sunset', $array);
$this->assertArrayHasKey('link', $array);
$this->assertArrayHasKey('rel', $array);
$this->assertArrayHasKey('type', $array);
$this->assertArrayHasKey('deprecationReason', $array);
$this->assertArrayHasKey('deprecationType', $array);
$this->assertSame('2038-01-01', $array['sunset']);
$this->assertSame('test-rel', $array['rel']);
$this->assertSame('test-type', $array['type']);
$this->assertSame('test-link', $array['link']);
$this->assertSame('test-deprecation-reason', $array['deprecationReason']);
$this->assertSame(DeprecationMiddleware::METHOD_DEPRECATION_ATTRIBUTE, $array['deprecationType']);
}
/**
* @template T of RequestHandlerInterface
* @param ReflectionClass<T> $reflectionClass
* @return array<int, mixed>
*/
private function getAttributes(ReflectionClass $reflectionClass): array
{
$methods = $reflectionClass->getMethods();
return $methods[0]->getAttributes(MethodDeprecation::class);
}
}