Completed
Pull Request — 1.4.x (#106)
by Grégoire
05:25 queued 03:08
created

ObjectManagerDecoratorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\Persistence;
4
5
use Doctrine\Persistence\ObjectManager;
6
use Doctrine\Persistence\ObjectManagerDecorator;
7
use PHPUnit\Framework\TestCase;
8
use PHPUnit_Framework_MockObject_MockObject;
9
use ReflectionClass;
10
use function array_fill;
11
use function call_user_func_array;
12
use function in_array;
13
14
class NullObjectManagerDecorator extends ObjectManagerDecorator
15
{
16
    public function __construct(ObjectManager $wrapped)
17
    {
18
        $this->wrapped = $wrapped;
19
    }
20
}
21
22
class ObjectManagerDecoratorTest extends TestCase
23
{
24
    /** @var PHPUnit_Framework_MockObject_MockObject|ObjectManager */
25
    private $wrapped;
26
27
    /** @var NullObjectManagerDecorator */
28
    private $decorated;
29
30
    public function setUp()
31
    {
32
        $this->wrapped   = $this->createMock(ObjectManager::class);
33
        $this->decorated = new NullObjectManagerDecorator($this->wrapped);
34
    }
35
36
    public function getMethodParameters()
37
    {
38
        $class       = new ReflectionClass(ObjectManager::class);
39
        $voidMethods = [
40
            'persist',
41
            'remove',
42
            'clear',
43
            'detach',
44
            'refresh',
45
            'flush',
46
            'initializeObject',
47
        ];
48
49
        $methods = [];
50
        foreach ($class->getMethods() as $method) {
51
            $isVoidMethod = in_array($method->getName(), $voidMethods, true);
52
            if ($method->getNumberOfRequiredParameters() === 0) {
53
                $methods[] = [$method->getName(), [], $isVoidMethod];
54
            } elseif ($method->getNumberOfRequiredParameters() > 0) {
55
                $methods[] = [$method->getName(), array_fill(0, $method->getNumberOfRequiredParameters(), 'req') ?: [], $isVoidMethod];
56
            }
57
            if ($method->getNumberOfParameters() === $method->getNumberOfRequiredParameters()) {
58
                continue;
59
            }
60
61
            $methods[] = [$method->getName(), array_fill(0, $method->getNumberOfParameters(), 'all') ?: [], $isVoidMethod];
62
        }
63
64
        return $methods;
65
    }
66
67
    /**
68
     * @param string  $method
69
     * @param mixed[] $parameters
70
     * @param bool    $isVoidMethod
71
     *
72
     * @dataProvider getMethodParameters
73
     */
74
    public function testAllMethodCallsAreDelegatedToTheWrappedInstance($method, array $parameters, $isVoidMethod)
75
    {
76
        $returnedValue = $isVoidMethod ? null : 'INNER VALUE FROM ' . $method;
77
        $stub          = $this->wrapped
78
            ->expects($this->once())
79
            ->method($method)
80
            ->will($this->returnValue($returnedValue));
81
82
        call_user_func_array([$stub, 'with'], $parameters);
83
84
        self::assertSame($returnedValue, call_user_func_array([$this->decorated, $method], $parameters));
85
    }
86
}
87