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

TestDoubles   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 52
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialiseTestDoubles() 0 15 1
A createTestDoubleWithProphecy() 0 13 3
A createTestDoubleWithPhpunit() 0 11 3
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