summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/QueryBuilder/DSL/AbstractDSLTest.php
blob: 3e44f4633c33c7068b960e81b33c27c9cb97e6cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Elastica\Test\QueryBuilder\DSL;

use Elastica\Exception\NotImplementedException;
use Elastica\QueryBuilder\DSL;
use Elastica\Test\Base as BaseTest;

abstract class AbstractDSLTest extends BaseTest
{
    /**
     * @param DSL    $dsl
     * @param string $methodName
     * @param string $className
     * @param array  $arguments
     */
    protected function _assertImplemented(DSL $dsl, $methodName, $className, $arguments)
    {
        // Check method existence
        $this->assertTrue(method_exists($dsl, $methodName));

        // Check returned value
        $return = call_user_func_array(array($dsl, $methodName), $arguments);
        $this->assertTrue(class_exists($className), 'Class not exists but NotImplementedException is not thrown');
        $this->assertInstanceOf($className, $return);

        // Check method signature
        $class = new \ReflectionClass($className);
        $method = new \ReflectionMethod(get_class($dsl), $methodName);
        if (!$class->hasMethod('__construct')) {
            $this->assertEmpty($method->getParameters(), 'Constructor is not defined, but method has some parameters');
        } else {
            $this->_assertParametersEquals($class->getMethod('__construct')->getParameters(), $method->getParameters());
        }
    }

    /**
     * @param DSL    $dsl
     * @param string $name
     */
    protected function _assertNotImplemented(DSL $dsl, $methodName, $arguments)
    {
        try {
            call_user_func(array($dsl, $methodName), $arguments);
            $this->fail('NotImplementedException is not thrown');
        } catch (NotImplementedException $ex) {
            // expected
        }
    }

    /**
     * @param \ReflectionParameter[] $left
     * @param \ReflectionParameter[] $right
     */
    protected function _assertParametersEquals($left, $right)
    {
        $this->assertEquals(count($left), count($right), 'Parameters count mismatch');

        for ($i = 0; $i < count($left); $i++) {
            $this->assertEquals($left[$i]->getName(), $right[$i]->getName(), 'Parameters names mismatch');
            $this->assertEquals($left[$i]->isOptional(), $right[$i]->isOptional(), 'Parameters optionality mismatch');
            $this->assertEquals($this->_getHintName($left[$i]), $this->_getHintName($right[$i]), 'Parameters typehints mismatch');
            $this->assertEquals($this->_getDefaultValue($left[$i]), $this->_getDefaultValue($right[$i]), 'Default values mismatch');
        }
    }

    /**
     * @param \ReflectionParameter $param
     *
     * @return string|null
     */
    protected function _getDefaultValue(\ReflectionParameter $param)
    {
        if ($param->isOptional()) {
            return $param->getDefaultValue();
        }
    }

    /**
     * @param \ReflectionParameter $param
     *
     * @return string|null
     */
    protected function _getHintName(\ReflectionParameter $param)
    {
        if (version_compare(phpversion(), '5.4', '>=') && $param->isCallable()) {
            return 'callable';
        }

        if ($param->isArray()) {
            return 'array';
        }

        if ($class = $param->getClass()) {
            return $class->getName();
        }
    }
}