ConfigurationTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 137
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testGetSetHydratedClassName() 0 6 1
A testCreateFactory() 0 4 1
A testGetSetAutoGenerateProxies() 0 10 1
A testGetSetProxiesNamespace() 0 11 1
A testSetGetClassNameInflector() 0 10 1
A testSetGetGeneratorStrategy() 0 10 1
A testSetGetProxiesTargetDir() 0 7 1
A testSetGetGeneratedClassAutoloader() 0 10 1
A testSetGetHydratorGenerator() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GeneratedHydratorTest;
6
7
use CodeGenerationUtils\Autoloader\AutoloaderInterface;
8
use CodeGenerationUtils\GeneratorStrategy\GeneratorStrategyInterface;
9
use CodeGenerationUtils\Inflector\ClassNameInflectorInterface;
10
use GeneratedHydrator\ClassGenerator\HydratorGenerator;
11
use GeneratedHydrator\Configuration;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
use function is_dir;
15
16
/**
17
 * Tests for {@see \GeneratedHydrator\Configuration}
18
 */
19
class ConfigurationTest extends TestCase
20
{
21
    /** @var Configuration */
22
    protected $configuration;
23
24
    /**
25
     * {@inheritDoc}
26
     *
27
     * @covers \GeneratedHydrator\Configuration::__construct
28
     */
29
    public function setUp() : void
30
    {
31
        $this->configuration = new Configuration('test');
32
    }
33
34
    /**
35
     * @covers \GeneratedHydrator\Configuration::setHydratedClassName
36
     * @covers \GeneratedHydrator\Configuration::getHydratedClassName
37
     */
38
    public function testGetSetHydratedClassName() : void
39
    {
40
        self::assertSame('test', $this->configuration->getHydratedClassName());
41
        $this->configuration->setHydratedClassName('bar');
42
        self::assertSame('bar', $this->configuration->getHydratedClassName());
43
    }
44
45
    /**
46
     * @covers \GeneratedHydrator\Configuration::createFactory
47
     */
48
    public function testCreateFactory() : void
49
    {
50
        self::assertInstanceOf('GeneratedHydrator\Factory\HydratorFactory', $this->configuration->createFactory());
51
    }
52
53
    /**
54
     * @covers \GeneratedHydrator\Configuration::doesAutoGenerateProxies
55
     * @covers \GeneratedHydrator\Configuration::setAutoGenerateProxies
56
     */
57
    public function testGetSetAutoGenerateProxies() : void
58
    {
59
        self::assertTrue($this->configuration->doesAutoGenerateProxies(), 'Default setting check for BC');
60
61
        $this->configuration->setAutoGenerateProxies(false);
62
        self::assertFalse($this->configuration->doesAutoGenerateProxies());
63
64
        $this->configuration->setAutoGenerateProxies(true);
65
        self::assertTrue($this->configuration->doesAutoGenerateProxies());
66
    }
67
68
    /**
69
     * @covers \GeneratedHydrator\Configuration::getGeneratedClassesNamespace
70
     * @covers \GeneratedHydrator\Configuration::setGeneratedClassesNamespace
71
     */
72
    public function testGetSetProxiesNamespace() : void
73
    {
74
        self::assertSame(
75
            'GeneratedHydratorGeneratedClass',
76
            $this->configuration->getGeneratedClassesNamespace(),
77
            'Default setting check for BC'
78
        );
79
80
        $this->configuration->setGeneratedClassesNamespace('foo');
81
        self::assertSame('foo', $this->configuration->getGeneratedClassesNamespace());
82
    }
83
84
    /**
85
     * @covers \GeneratedHydrator\Configuration::getClassNameInflector
86
     * @covers \GeneratedHydrator\Configuration::setClassNameInflector
87
     */
88
    public function testSetGetClassNameInflector() : void
89
    {
90
        self::assertInstanceOf(ClassNameInflectorInterface::class, $this->configuration->getClassNameInflector());
91
92
        /** @var ClassNameInflectorInterface|MockObject $inflector */
93
        $inflector = $this->createMock(ClassNameInflectorInterface::class);
94
95
        $this->configuration->setClassNameInflector($inflector);
0 ignored issues
show
Bug introduced by
It seems like $inflector defined by $this->createMock(\CodeG...lectorInterface::class) on line 93 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, GeneratedHydrator\Config...setClassNameInflector() does only seem to accept object<CodeGenerationUti...NameInflectorInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96
        self::assertSame($inflector, $this->configuration->getClassNameInflector());
97
    }
98
99
    /**
100
     * @covers \GeneratedHydrator\Configuration::getGeneratorStrategy
101
     * @covers \GeneratedHydrator\Configuration::setGeneratorStrategy
102
     */
103
    public function testSetGetGeneratorStrategy() : void
104
    {
105
        self::assertInstanceOf(GeneratorStrategyInterface::class, $this->configuration->getGeneratorStrategy());
106
107
        /** @var GeneratorStrategyInterface|MockObject $strategy */
108
        $strategy = $this->createMock(GeneratorStrategyInterface::class);
109
110
        $this->configuration->setGeneratorStrategy($strategy);
0 ignored issues
show
Bug introduced by
It seems like $strategy defined by $this->createMock(\CodeG...rategyInterface::class) on line 108 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, GeneratedHydrator\Config...:setGeneratorStrategy() does only seem to accept object<CodeGenerationUti...ratorStrategyInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
111
        self::assertSame($strategy, $this->configuration->getGeneratorStrategy());
112
    }
113
114
    /**
115
     * @covers \GeneratedHydrator\Configuration::getGeneratedClassesTargetDir
116
     * @covers \GeneratedHydrator\Configuration::setGeneratedClassesTargetDir
117
     */
118
    public function testSetGetProxiesTargetDir() : void
119
    {
120
        self::assertTrue(is_dir($this->configuration->getGeneratedClassesTargetDir()));
121
122
        $this->configuration->setGeneratedClassesTargetDir(__DIR__);
123
        self::assertSame(__DIR__, $this->configuration->getGeneratedClassesTargetDir());
124
    }
125
126
    /**
127
     * @covers \GeneratedHydrator\Configuration::getGeneratedClassAutoloader
128
     * @covers \GeneratedHydrator\Configuration::setGeneratedClassAutoloader
129
     */
130
    public function testSetGetGeneratedClassAutoloader() : void
131
    {
132
        self::assertInstanceOf(AutoloaderInterface::class, $this->configuration->getGeneratedClassAutoloader());
133
134
        /** @var AutoloaderInterface|MockObject $autoloader */
135
        $autoloader = $this->createMock(AutoloaderInterface::class);
136
137
        $this->configuration->setGeneratedClassAutoloader($autoloader);
0 ignored issues
show
Bug introduced by
It seems like $autoloader defined by $this->createMock(\CodeG...loaderInterface::class) on line 135 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, GeneratedHydrator\Config...eratedClassAutoloader() does only seem to accept object<CodeGenerationUti...er\AutoloaderInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
138
        self::assertSame($autoloader, $this->configuration->getGeneratedClassAutoloader());
139
    }
140
141
    /**
142
     * @covers \GeneratedHydrator\Configuration::getHydratorGenerator
143
     * @covers \GeneratedHydrator\Configuration::setHydratorGenerator
144
     */
145
    public function testSetGetHydratorGenerator() : void
146
    {
147
        self::assertInstanceOf(HydratorGenerator::class, $this->configuration->getHydratorGenerator());
148
149
        /** @var HydratorGenerator $generator*/
150
        $generator = $this->createMock(HydratorGenerator::class);
151
152
        $this->configuration->setHydratorGenerator($generator);
0 ignored issues
show
Documentation introduced by
$generator is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<GeneratedHydrator...ator\HydratorGenerator>.

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...
153
        self::assertSame($generator, $this->configuration->getHydratorGenerator());
154
    }
155
}
156