Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

AbstractDSLTest::_getDefaultValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Elastica\Test\QueryBuilder\DSL;
4
5
use Elastica\Exception\NotImplementedException;
6
use Elastica\QueryBuilder\DSL;
7
use Elastica\Test\Base as BaseTest;
8
9
abstract class AbstractDSLTest extends BaseTest
10
{
11
    protected function _assertImplemented(DSL $dsl, string $methodName, string $className, array $arguments): void
12
    {
13
        // Check method existence
14
        $this->assertTrue(\method_exists($dsl, $methodName));
15
16
        // Check returned value
17
        $return = \call_user_func_array([$dsl, $methodName], $arguments);
18
        $this->assertTrue(\class_exists($className), 'Class not exists but NotImplementedException is not thrown');
19
        $this->assertInstanceOf($className, $return);
20
21
        // Check method signature
22
        $class = new \ReflectionClass($className);
23
        $method = new \ReflectionMethod(\get_class($dsl), $methodName);
24
        if (!$class->hasMethod('__construct')) {
25
            $this->assertEmpty($method->getParameters(), 'Constructor is not defined, but method has some parameters');
26
        } else {
27
            $this->_assertParametersEquals($class->getMethod('__construct')->getParameters(), $method->getParameters());
28
        }
29
    }
30
31
    protected function _assertNotImplemented(DSL $dsl, string $methodName, array $arguments): void
32
    {
33
        try {
34
            \call_user_func([$dsl, $methodName], $arguments);
35
            $this->fail('NotImplementedException is not thrown');
36
        } catch (NotImplementedException $ex) {
37
            // expected
38
        }
39
    }
40
41
    /**
42
     * @param \ReflectionParameter[] $left
43
     * @param \ReflectionParameter[] $right
44
     */
45
    protected function _assertParametersEquals(array $left, array $right): void
46
    {
47
        $countLeft = \count($left);
48
        $this->assertCount($countLeft, $right, 'Parameters count mismatch');
49
50
        for ($i = 0; $i < $countLeft; ++$i) {
51
            $this->assertEquals($left[$i]->getName(), $right[$i]->getName(), 'Parameters names mismatch');
0 ignored issues
show
Bug introduced by
Consider using $left[$i]->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
52
            $this->assertEquals($left[$i]->isOptional(), $right[$i]->isOptional(), 'Parameters optionality mismatch');
53
            $this->assertEquals($this->_getHintName($left[$i]), $this->_getHintName($right[$i]), 'Parameters typehints mismatch');
54
            $this->assertEquals($this->_getDefaultValue($left[$i]), $this->_getDefaultValue($right[$i]), 'Default values mismatch');
55
        }
56
    }
57
58
    /**
59
     * @return string|null
60
     */
61
    protected function _getDefaultValue(\ReflectionParameter $param)
62
    {
63
        if ($param->isOptional()) {
64
            return $param->getDefaultValue();
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * @return string|null
72
     */
73
    protected function _getHintName(\ReflectionParameter $param)
74
    {
75
        if ($param->isCallable()) {
76
            return 'callable';
77
        }
78
79
        if ($param->isArray()) {
80
            return 'array';
81
        }
82
83
        if ($class = $param->getClass()) {
84
            return $class->getName();
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
85
        }
86
87
        return null;
88
    }
89
}
90