Completed
Push — master ( 7a7569...aac949 )
by Angelo
14s
created

src/Moka/Tests/MokaMockingStrategyTestCase.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Tests;
5
6
use Moka\Exception\InvalidArgumentException;
7
use Moka\Factory\StubFactory;
8
use Moka\Strategy\MockingStrategyInterface;
9
use Moka\Stub\StubSet;
10
use PHPUnit\Framework\TestCase;
11
use Tests\AbstractTestClass;
12
use Tests\BarTestClass;
13
use Tests\FooTestClass;
14
use Tests\TestInterface;
15
16
abstract class MokaMockingStrategyTestCase extends TestCase
17
{
18
    /**
19
     * @var MockingStrategyInterface
20
     */
21
    protected $strategy;
22
23
    /**
24
     * @var object
25
     */
26
    protected $mock;
27
28
    /**
29
     * @var StubSet
30
     */
31
    protected $stubs;
32
33
    /**
34
     * @var string
35
     */
36
    private $className;
37
38
    /**
39
     * @var array
40
     */
41
    private $methodsWithValues = [];
42
43
    final public function testGetMockTypeSuccess()
44
    {
45
        $this->assertInternalType('string', $this->strategy->getMockType());
46
    }
47
48
    final public function testBuildClassSuccess()
49
    {
50
        $this->assertInstanceOf($this->strategy->getMockType(), $this->mock);
51
    }
52
53
    final public function testBuildInterfaceSuccess()
54
    {
55
        $mock = $this->strategy->build(TestInterface::class);
56
57
        $this->assertInstanceOf($this->strategy->getMockType(), $mock);
58
    }
59
60
    final public function testBuildAbstractClassSuccess()
61
    {
62
        $mock = $this->strategy->build(AbstractTestClass::class);
63
64
        $this->assertInstanceOf($this->strategy->getMockType(), $mock);
65
    }
66
67
    final public function testDecorateFailure()
68
    {
69
        $this->expectException(InvalidArgumentException::class);
70
71
        $this->strategy->decorate(new \stdClass(), $this->stubs);
72
    }
73
74
    final public function testDecorateWrongTypeHintFailure()
75
    {
76
        $this->strategy->decorate($this->mock, StubFactory::fromArray([
0 ignored issues
show
It seems like \Moka\Factory\StubFactor...getSelf' => mt_rand())) targeting Moka\Factory\StubFactory::fromArray() can also be of type array<integer,object<Moka\Stub\Stub>>; however, Moka\Strategy\MockingStrategyInterface::decorate() does only seem to accept object<Moka\Stub\StubSet>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
77
            'getSelf' => mt_rand()
78
        ]));
79
80
        $this->expectException(\TypeError::class);
81
        $this->strategy->get($this->mock)->getSelf();
82
    }
83
84
    final public function testDecorateSingleCallSuccess()
85
    {
86
        $this->assertSame($this->methodsWithValues['isTrue'], $this->strategy->get($this->mock)->isTrue());
87
88
        $this->expectException(\Exception::class);
89
        $this->expectExceptionMessage($this->methodsWithValues['throwException']->getMessage());
90
        $this->strategy->get($this->mock)->throwException();
91
    }
92
93
    final public function testDecorateMultipleCallsSuccess()
94
    {
95
        $this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
96
        $this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
97
    }
98
99
    final public function testDecorateOverriddenCallsFailure()
100
    {
101
        $this->strategy->decorate($this->mock, StubFactory::fromArray([
0 ignored issues
show
It seems like \Moka\Factory\StubFactor...ception' => mt_rand())) targeting Moka\Factory\StubFactory::fromArray() can also be of type array<integer,object<Moka\Stub\Stub>>; however, Moka\Strategy\MockingStrategyInterface::decorate() does only seem to accept object<Moka\Stub\StubSet>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
102
            'getInt' => mt_rand(),
103
            'throwException' => mt_rand()
104
        ]));
105
106
        $this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
107
        $this->assertSame($this->methodsWithValues['getInt'], $this->strategy->get($this->mock)->getInt());
108
109
        $this->expectException(\Exception::class);
110
        $this->expectExceptionMessage($this->methodsWithValues['throwException']->getMessage());
111
        $this->strategy->get($this->mock)->throwException();
112
    }
113
114
    final public function testDecorateCallWithArgumentSuccess()
115
    {
116
        $this->assertSame($this->methodsWithValues['withArgument'], $this->strategy->get($this->mock)->withArgument(mt_rand()));
117
    }
118
119
    final public function testDecorateCallWithMissingArgumentFailure()
120
    {
121
        $this->expectException(\Error::class);
122
123
        $this->strategy->get($this->mock)->withArgument();
124
    }
125
126
    final public function testDecorateCallWithWrongArgumentFailure()
127
    {
128
        $this->expectException(\TypeError::class);
129
130
        $this->strategy->get($this->mock)->withArgument('string');
131
    }
132
133
    final public function testGetSuccess()
134
    {
135
        $this->assertInstanceOf($this->className, $this->strategy->get($this->mock));
136
    }
137
138
    final public function testGetFailure()
139
    {
140
        $this->expectException(InvalidArgumentException::class);
141
142
        $this->strategy->get(new \stdClass());
143
    }
144
145
    protected function setUp()
146
    {
147
        $this->className = [
148
            FooTestClass::class,
149
            BarTestClass::class
150
        ][random_int(0, 1)];
151
152
        $this->methodsWithValues = [
153
            'isTrue' => (bool)random_int(0, 1),
154
            'getInt' => mt_rand(),
155
            'withArgument' => mt_rand(),
156
            'throwException' => new \Exception('' . mt_rand())
157
        ];
158
159
        $this->mock = $this->strategy->build($this->className);
160
161
        // Mocking a StubSet is way too difficult.
162
        $this->stubs = StubFactory::fromArray($this->methodsWithValues);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Moka\Factory\StubFactor...his->methodsWithValues) can also be of type array<integer,object<Moka\Stub\Stub>>. However, the property $stubs is declared as type object<Moka\Stub\StubSet>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
163
164
        $this->strategy->decorate($this->mock, $this->stubs);
0 ignored issues
show
It seems like $this->stubs can also be of type array<integer,object<Moka\Stub\Stub>>; however, Moka\Strategy\MockingStrategyInterface::decorate() does only seem to accept object<Moka\Stub\StubSet>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
165
    }
166
167
    final protected function setStrategy(MockingStrategyInterface $strategy)
168
    {
169
        $this->strategy = $strategy;
170
    }
171
172
    final protected function getRandomFQCN()
173
    {
174
        return 'foo_' . mt_rand();
175
    }
176
}
177