testConstructorWithBadArguments()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
/**
4
 * PHP: Nelson Martell Library file
5
 *
6
 * Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io)
7
 *
8
 * Licensed under The MIT License (MIT)
9
 * For full copyright and license information, please see the LICENSE
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright 2016-2021 Nelson Martell
13
 * @link      http://nelson6e65.github.io/php_nml/
14
 * @since     0.6.0
15
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
16
 * */
17
18
declare(strict_types=1);
19
20
namespace NelsonMartell\Test\Helpers;
21
22
use Throwable;
23
use ReflectionClass;
24
use ReflectionException;
25
use BadMethodCallException;
26
use UnexpectedValueException;
27
use PHPUnit\Framework\TestCase;
28
29
/**
30
 * Provides test methods and helpers to test class constructors.
31
 *
32
 * @author Nelson Martell <[email protected]>
33
 * @since 0.6.0
34
 * */
35
trait ConstructorMethodTester
36
{
37
    /**
38
     * Gets the name of class target of this test-class.
39
     *
40
     * @return string
41
     */
42
    abstract public function getTargetClassName(): string;
43
44
45
46
    /**
47
     *
48
     * @var ReflectionClass
49
     */
50
    protected $targetClassReflection = null;
51
52
    /**
53
     *
54
     * @return ReflectionClass
55
     * @throws BadMethodCallException
56
     */
57
    public function getTargetClassReflection(): ReflectionClass
58
    {
59
        if ($this->targetClassReflection === null) {
60
            try {
61
                $this->targetClassReflection = new ReflectionClass($this->getTargetClassName());
62
            } catch (ReflectionException $e) {
63
                throw new BadMethodCallException('``getTargetClassName()`` is not returning a valid class name.', 1, $e);
64
            }
65
        }
66
67
        return $this->targetClassReflection;
68
    }
69
70
    /**
71
     * Gets (dinamically) an instance of target class using its constructor with the (optional) arguments.
72
     * It uses the ``getTargetClassName`` return value to determinate the name of target class.
73
     *
74
     * @param mixed  $args
75
     *
76
     * @return mixed Instance of target class.
77
     * @throws UnexpectedValueException
78
     * @throws BadMethodCallException
79
     */
80
    public function getTargetClassInstance(...$args)
81
    {
82
        return $this->getTargetClassReflection()->newInstanceArgs($args);
83
    }
84
85
    /**
86
     * @testdox Creates new instances
87
     * @dataProvider goodConstructorArgumentsProvider
88
     *
89
     * @param mixed  $args Constructor arguments
90
     */
91
    public function testConstructor(...$args): void
92
    {
93
        $this->getTargetClassInstance(...$args);
94
95
        /** @var TestCase $this */
96
        $this->assertTrue(true);
97
    }
98
99
    /**
100
     * @testdox Informs when error occurs on creating new instances
101
     * @dataProvider badConstructorArgumentsProvider
102
     *
103
     * @param string $exception Exception name
104
     * @param mixed  $args Constructor arguments
105
     */
106
    public function testConstructorWithBadArguments(string $exception, ...$args): void
107
    {
108
        /** @var TestCase $this */
109
        if (!is_subclass_of($exception, Throwable::class)) {
110
            $this->fail('dataProvider argument error: first argument must to be a Throwable name');
111
        }
112
113
        $this->expectException($exception);
114
115
        /** @var self $this */
116
        $this->getTargetClassInstance(...$args);
117
    }
118
119
    /**
120
     * Must provide valid argument for constructor.
121
     *
122
     * @return array
123
     */
124
    abstract public function goodConstructorArgumentsProvider(): array;
125
126
    /**
127
     * Must provide a exception class name and invalid arguments for constructor.
128
     *
129
     * @return array
130
     */
131
    abstract public function badConstructorArgumentsProvider(): array;
132
}
133