Completed
Push — master ( 391b65...49cdab )
by David
14s
created

RegistryTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 57
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ get() 0 4 1
A hp$0 ➔ has() 0 4 1
A getContainer() 0 14 1
A testFromContainer() 0 9 1
A testInstantiate() 0 10 1
A testNotFound() 0 6 1
A testGetAuthorization() 0 11 1
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);
0 ignored issues
show
Documentation introduced by
$authorizationService is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a null|object<TheCodingMac...zationServiceInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
60
        $this->assertSame($authorizationService, $registry->getAuthorizationService());
61
62
        $registry = new Registry($this->getContainer());
63
64
        $this->assertNull($registry->getAuthorizationService());
65
    }
66
}
67