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 ( f51b4f...d5d186 )
by Cees-Jan
10:52
created

AbstractResourceTest::generateTypeValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
nc 3
cc 3
eloc 12
nop 4
crap 12
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
14
abstract class AbstractResourceTest extends TestCase
15
{
16
    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...
17
    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...
18
19
    public function provideProperties(): Generator
20
    {
21
        yield from $this->providePropertiesGenerator('compatible');
22
    }
23
24
    public function providePropertiesIncompatible(): Generator
25
    {
26
        yield from $this->providePropertiesGenerator('incompatible');
27
    }
28
29
    public function providePropertiesGenerator(string $typeMethod): Generator
30
    {
31
        foreach (Types::types() as $t) {
32
            var_export($t);
33
        }
34
        $class = new ReflectionClass($this->getClass());
35
36
        $jsonTemplate = [];
37
        foreach ($class->getProperties() as $property) {
38
            $jsonTemplate[$property->getName()] = '';
39
        }
40
41
        foreach ($class->getProperties() as $property) {
42
            $method = Inflector::camelize($property->getName());
0 ignored issues
show
Unused Code introduced by
$method is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
            $docBlock = $this->getDocBlock($property->getDocComment());
44
45
            $varTag = $docBlock->getTagsByName('var');
46
            if (count($varTag) !== 1) {
47
                continue;
48
            }
49
50
            $varTag = $varTag[0];
51
            if ($varTag->getType() === '') {
52
                continue;
53
            }
54
55
            if (!Types::has($varTag->getType())) {
56
                continue;
57
            }
58
59
            $type = Types::get($varTag->getType());
60
            yield from $this->generateTypeValues($type, $property, $typeMethod, $jsonTemplate);
61
        }
62
    }
63
64
    protected function generateTypeValues(Type $type, ReflectionProperty $property, string $method, array $jsonTemplate): Generator
65
    {
66
        $json = $jsonTemplate;
67
        foreach ($type->$method as $typeClass) {
68
            $methodType = Types::get(constant($typeClass . '::SCALAR'));
69
            foreach ($methodType->generate(25) as $value) {
70
                $json[$property->getName()] = $value;
71
                yield [
72
                    $property->getName(), // Name of the property to assign data to
73
                    $method,              // Method to call verifying that data
74
                    $type,                // The different types of data assiciated with this field
75
                    $json,                // JSON to use during testing
76
                    $value,               // Value to check against
77
                ];
78
            }
79
        }
80
    }
81
82
    /**
83
     * @param $docBlockContents
84
     * @return DocBlock
85
     */
86
    protected function getDocBlock(string $docBlockContents): DocBlock
87
    {
88
        if (class_exists('phpDocumentor\Reflection\DocBlockFactory')) {
89
            return DocBlockFactory::createInstance()->create($docBlockContents);
90
        }
91
92
        return new DocBlock($docBlockContents);
93
    }
94
95
    /**
96
     * @dataProvider provideProperties
97
     */
98
    public function testProperties(string $property, string $method, Type $type, array $json, mixed $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...
99
    {
100
        $class = $this->getClass();
101
        $resource = $this->hydrate(
102
            str_replace(
103
                $this->getNamespace(),
104
                $this->getNamespace() . '\\Async',
105
                $class
106
            ),
107
            $json,
108
            'Async'
109
        );
110
        $this->assertSame($value, $resource->{$method}());
111
        $this->assertInternalType($type->scalar(), $resource->{$method}());
112
    }
113
114
    /**
115
     * @dataProvider providePropertiesIncompatible
116
     */
117
    public function testPropertiesIncompatible(string $property, string $method, Type $type, array $json, mixed $value)
118
    {
119
        var_export([$property, $method, $type, $json, $value]);
120
        $class = $this->getClass();
121
        $resource = $this->hydrate(
122
            str_replace(
123
                $this->getNamespace(),
124
                $this->getNamespace() . '\\Async',
125
                $class
126
            ),
127
            $json,
128
            'Async'
129
        );
130
        $this->assertSame($value, $resource->{$method}());
131
    }
132
133
    public function testInterface()
134
    {
135
        $this->assertTrue(
136
            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...
137
                $this->getClass(),
138
                ResourceInterface::class
139
            )
140
        );
141
    }
142
}
143