Passed
Push — main ( d1c8bb...2cd0ba )
by Daniel
12:40
created

AbstractTestCase::getRequestFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DanBettles\Defence\Tests;
6
7
use DanBettles\Defence\Tests\TestsFactory\RequestFactory;
8
use InvalidArgumentException;
9
use PHPUnit\Framework\TestCase;
10
use ReflectionClass;
11
use Traversable;
12
13
use function is_array;
14
15
use const false;
16
use const true;
17
18
abstract class AbstractTestCase extends TestCase
19
{
20
    protected function getRequestFactory(): RequestFactory
21
    {
22
        return new RequestFactory();
23
    }
24
25
    /**
26
     * @phpstan-param class-string $expectedClassName
27
     * @phpstan-param class-string|object $objectOrClassName
28
     */
29
    protected function assertSubclassOf(string $expectedClassName, $objectOrClassName): void
30
    {
31
        $reflectionClass = new ReflectionClass($objectOrClassName);
32
33
        $this->assertTrue($reflectionClass->isSubclassOf($expectedClassName));
34
    }
35
36
    /**
37
     * @param mixed $collection
38
     * @throws InvalidArgumentException If the collection is neither an array nor traversable
39
     */
40
    protected function assertContainsInstanceOf(string $expected, $collection): void
41
    {
42
        if (!is_array($collection) && !($collection instanceof Traversable)) {
43
            throw new InvalidArgumentException('The collection is neither an array nor traversable');
44
        }
45
46
        $containsInstanceOf = false;
47
48
        foreach ($collection as $element) {
49
            if ($element instanceof $expected) {
50
                $containsInstanceOf = true;
51
52
                break;
53
            }
54
        }
55
56
        $this->assertTrue($containsInstanceOf, "The collection does not contain an instance of `{$expected}`");
57
    }
58
}
59