1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Registry; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType; |
8
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface; |
9
|
|
|
|
10
|
|
|
class RegistryTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
private function getContainer(): ContainerInterface |
13
|
|
|
{ |
14
|
|
|
return new class implements ContainerInterface { |
15
|
|
|
public function get($id) |
16
|
|
|
{ |
17
|
|
|
return 'foo'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function has($id) |
21
|
|
|
{ |
22
|
|
|
return $id === 'foo'; |
23
|
|
|
} |
24
|
|
|
}; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testFromContainer() |
28
|
|
|
{ |
29
|
|
|
$registry = new Registry($this->getContainer()); |
30
|
|
|
|
31
|
|
|
$this->assertTrue($registry->has('foo')); |
32
|
|
|
$this->assertFalse($registry->has('bar')); |
33
|
|
|
|
34
|
|
|
$this->assertSame('foo', $registry->get('foo')); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testInstantiate() |
38
|
|
|
{ |
39
|
|
|
$registry = new Registry($this->getContainer()); |
40
|
|
|
|
41
|
|
|
$this->assertTrue($registry->has(TestType::class)); |
42
|
|
|
$type = $registry->get(TestType::class); |
43
|
|
|
$this->assertInstanceOf(TestType::class, $type); |
44
|
|
|
$this->assertSame($type, $registry->get(TestType::class)); |
45
|
|
|
$this->assertTrue($registry->has(TestType::class)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testNotFound() |
49
|
|
|
{ |
50
|
|
|
$registry = new Registry($this->getContainer()); |
51
|
|
|
$this->expectException(NotFoundException::class); |
52
|
|
|
$registry->get('notfound'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testGetAuthorization() |
56
|
|
|
{ |
57
|
|
|
$authorizationService = $this->createMock(AuthorizationServiceInterface::class); |
58
|
|
|
$registry = new Registry($this->getContainer(), $authorizationService); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->assertSame($authorizationService, $registry->getAuthorizationService()); |
61
|
|
|
|
62
|
|
|
$registry = new Registry($this->getContainer()); |
63
|
|
|
|
64
|
|
|
$this->assertNull($registry->getAuthorizationService()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: