Failed Conditions
Push — master ( dbaf44...bfd3f9 )
by Jakub
03:46
created

TestDoubles::createTestDouble()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
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\MockObject;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Zalas\PHPUnit\Doubles\Extractor\Property;
9
use Zalas\PHPUnit\Doubles\PhpDocumentor\ReflectionExtractor;
10
11
trait TestDoubles
12
{
13
    /**
14
     * @before
15
     */
16 4
    protected function initialiseTestDoubles(): void
17
    {
18 4
        foreach ($this->getTestDoubleProperties() as $property) {
19 4
            $this->{$property->getName()} = $this->createTestDouble($property);
20
        }
21
    }
22
23 4
    private function createTestDouble(Property $property)
24
    {
25 4
        if ($property->hasType(ObjectProphecy::class)) {
26
            return $this->createTestDoubleWithProphecy($property->getTypesFiltered(function (string $type) {
27 2
                return ObjectProphecy::class !== $type;
28 2
            }));
29
        }
30 2
        if ($property->hasType(MockObject::class)) {
31
            return $this->createTestDoubleWithPhpunit($property->getTypesFiltered(function (string $type) {
32 2
                return MockObject::class !== $type;
33 2
            }));
34
        }
35
    }
36
37 2
    private function createTestDoubleWithProphecy(array $types): ObjectProphecy
38
    {
39 2
        $prophecy = $this->prophesize(\array_shift($types));
0 ignored issues
show
Bug introduced by
It seems like prophesize() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        /** @scrutinizer ignore-call */ 
40
        $prophecy = $this->prophesize(\array_shift($types));
Loading history...
40
41 2
        foreach ($types as $type) {
42 2
            if (\interface_exists($type)) {
43 2
                $prophecy->willImplement($type);
44
            } else {
45 2
                $prophecy->willExtend($type);
46
            }
47
        }
48
49 2
        return $prophecy;
50
    }
51
52 2
    private function createTestDoubleWithPhpunit(array $types): MockObject
53
    {
54 2
        return $this->getMockBuilder(1 === \count($types) ? \array_pop($types) : $types)
0 ignored issues
show
Bug introduced by
It seems like getMockBuilder() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        return $this->/** @scrutinizer ignore-call */ getMockBuilder(1 === \count($types) ? \array_pop($types) : $types)
Loading history...
55 2
            ->disableOriginalConstructor()
56 2
            ->disableOriginalClone()
57 2
            ->disableArgumentCloning()
58 2
            ->disableProxyingToOriginalMethods()
59 2
            ->disallowMockingUnknownTypes()
60 2
            ->getMock();
61
    }
62
63 4
    private function getTestDoubleProperties(): array
64
    {
65
        return (new ReflectionExtractor())->extract($this, function (Property $property) {
66 4
            return $property->hasType(ObjectProphecy::class) || $property->hasType(MockObject::class);
67 4
        });
68
    }
69
}
70