Completed
Pull Request — master (#2)
by Jakub
07:47
created

TestDoubles::getTestDoubleProperties()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Zalas\PHPUnit\Doubles\TestCase;
5
6
use PHPUnit\Framework\MockObject\MockBuilder;
7
use PHPUnit\Framework\MockObject\MockObject;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Zalas\PHPUnit\Doubles\Injector\PropertyAccessInjector;
10
use Zalas\PHPUnit\Doubles\PhpDocumentor\ReflectionExtractor;
11
12
trait TestDoubles
13
{
14
    abstract public function getMockBuilder($className): MockBuilder;
15
16
    abstract protected function prophesize($classOrInterface = null): ObjectProphecy;
17
18
    /**
19
     * @before
20
     */
21 19
    protected function initialiseTestDoubles(): void
22
    {
23 19
        $doubler = new Doubler(
24 19
            new ReflectionExtractor(),
25 19
            new PropertyAccessInjector(__CLASS__),
26
            [
27
                ObjectProphecy::class => function (array $types) {
28 12
                    return $this->createTestDoubleWithProphecy($types);
29 19
                },
30
                MockObject::class => function (array $types) {
31 6
                    return $this->createTestDoubleWithPhpunit($types);
32 19
                },
33
            ]
34
        );
35 19
        $doubler->createDoubles($this);
36
    }
37
38 12
    private function createTestDoubleWithProphecy(array $types): ObjectProphecy
39
    {
40 12
        $prophecy = $this->prophesize(\array_shift($types));
41
42 12
        foreach ($types as $type) {
43 3
            if (\interface_exists($type)) {
44 3
                $prophecy->willImplement($type);
45
            } else {
46 3
                $prophecy->willExtend($type);
47
            }
48
        }
49
50 12
        return $prophecy;
51
    }
52
53 6
    private function createTestDoubleWithPhpunit(array $types): MockObject
54
    {
55 6
        $normalisedTypes = 1 === \count($types) ? \array_pop($types) : (!empty($types) ? $types : \stdClass::class);
56
57 6
        return $this->getMockBuilder($normalisedTypes)
58 6
            ->disableOriginalConstructor()
59 6
            ->disableOriginalClone()
60 6
            ->disableArgumentCloning()
61 6
            ->disableProxyingToOriginalMethods()
62 6
            ->disallowMockingUnknownTypes()
63 6
            ->getMock();
64
    }
65
}
66