GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d5d186...e6385d )
by Cees-Jan
15:28 queued 05:27
created

AbstractResourceTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 0
loc 132
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
getClass() 0 1 ?
getNamespace() 0 1 ?
A provideProperties() 0 4 1
A providePropertiesIncompatible() 0 4 1
B providePropertiesGenerator() 0 31 6
A generateTypeValues() 0 23 3
A getDocBlock() 0 8 2
A testProperties() 0 15 1
A testPropertiesIncompatible() 0 14 1
A testInterface() 0 9 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ApiClients\Tools\ResourceTestUtilities;
5
6
use ApiClients\Foundation\Resource\ResourceInterface;
7
use Doctrine\Common\Inflector\Inflector;
8
use Generator;
9
use phpDocumentor\Reflection\DocBlock;
10
use phpDocumentor\Reflection\DocBlockFactory;
11
use ReflectionClass;
12
use ReflectionProperty;
13
use TypeError;
14
15
abstract class AbstractResourceTest extends TestCase
16
{
17
    abstract function getClass(): string;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
    abstract function getNamespace(): string;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
20
    public function provideProperties(): Generator
21
    {
22
        yield from $this->providePropertiesGenerator('compatible');
23
    }
24
25
    public function providePropertiesIncompatible(): Generator
26
    {
27
        yield from $this->providePropertiesGenerator('incompatible');
28
    }
29
30
    public function providePropertiesGenerator(string $typeMethod): Generator
31
    {
32
        $class = new ReflectionClass($this->getClass());
33
34
        $jsonTemplate = [];
35
        foreach ($class->getProperties() as $property) {
36
            $jsonTemplate[$property->getName()] = '';
37
        }
38
39
        foreach ($class->getProperties() as $property) {
40
            $method = Inflector::camelize($property->getName());
41
            $docBlock = $this->getDocBlock($property->getDocComment());
42
43
            $varTag = $docBlock->getTagsByName('var');
44
            if (count($varTag) !== 1) {
45
                continue;
46
            }
47
48
            $varTag = $varTag[0];
49
            if ($varTag->getType() === '') {
50
                continue;
51
            }
52
53
            if (!Types::has($varTag->getType())) {
54
                continue;
55
            }
56
57
            $type = Types::get($varTag->getType());
58
            yield from $this->generateTypeValues($type, $property, $method, $typeMethod, $jsonTemplate);
59
        }
60
    }
61
62
    protected function generateTypeValues(
63
        Type $type,
64
        ReflectionProperty $property,
65
        string $method,
66
        string $typeMethod,
67
        array $jsonTemplate
68
    ): Generator
69
    {
70
        $json = $jsonTemplate;
71
        foreach ($type->$typeMethod() as $typeClass) {
72
            $methodType = Types::get(constant($typeClass . '::SCALAR'));
73
            foreach ($methodType->generate(2500) as $value) {
74
                $json[$property->getName()] = $value;
75
                yield [
76
                    $property->getName(), // Name of the property to assign data to
77
                    $method,              // Method to call verifying that data
78
                    $type,                // The different types of data assiciated with this field
79
                    $json,                // JSON to use during testing
80
                    $value,               // Value to check against
81
                ];
82
            }
83
        }
84
    }
85
86
    /**
87
     * @param $docBlockContents
88
     * @return DocBlock
89
     */
90
    protected function getDocBlock(string $docBlockContents): DocBlock
91
    {
92
        if (class_exists('phpDocumentor\Reflection\DocBlockFactory')) {
93
            return DocBlockFactory::createInstance()->create($docBlockContents);
94
        }
95
96
        return new DocBlock($docBlockContents);
97
    }
98
99
    /**
100
     * @dataProvider provideProperties
101
     */
102
    public function testProperties(string $property, string $method, Type $type, array $json, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $property is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        $class = $this->getClass();
105
        $resource = $this->hydrate(
106
            str_replace(
107
                $this->getNamespace(),
108
                $this->getNamespace() . '\\Async',
109
                $class
110
            ),
111
            $json,
112
            'Async'
113
        );
114
        $this->assertSame($value, $resource->{$method}());
115
        $this->assertInternalType($type->scalar(), $resource->{$method}());
116
    }
117
118
    /**
119
     * @dataProvider providePropertiesIncompatible
120
     * @expectedException TypeError
121
     */
122
    public function testPropertiesIncompatible(string $property, string $method, Type $type, array $json, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $property is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
    {
124
        $class = $this->getClass();
125
        $resource = $this->hydrate(
126
            str_replace(
127
                $this->getNamespace(),
128
                $this->getNamespace() . '\\Async',
129
                $class
130
            ),
131
            $json,
132
            'Async'
133
        );
134
        $this->assertSame($value, $resource->{$method}());
135
    }
136
137
    public function testInterface()
138
    {
139
        $this->assertTrue(
140
            is_subclass_of(
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \ApiClients\Foundation\R...esourceInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
141
                $this->getClass(),
142
                ResourceInterface::class
143
            )
144
        );
145
    }
146
}
147