Passed
Branch master (e8fd46)
by Alexey
02:50
created

ObjectInflectorTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 6
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
use Venta\Container\ObjectInflector;
5
use Venta\Contracts\Container\ArgumentResolver;
6
7
class ObjectInflectorTest extends TestCase
8
{
9
10
    /**
11
     * @test
12
     */
13
    public function canApplyInflections()
14
    {
15
        // Mocking argument resolver dependency.
16
        $resolver = Mockery::mock(ArgumentResolver::class);
17
18
        // Callback to be returned by ArgumentResolver::resolveArguments() method.
19
        // Expects $arguments array to contain 'value' key to merge.
20
        // Will return array of arguments for TestClass::setValue() method call.
21
        $callback = function (array $arguments) {
22
            return array_values(array_merge(['value' => 'to be replaced'], $arguments));
23
        };
24
25
        // Reflecting method to call on inflection.
26
        $reflection = new ReflectionMethod(TestClass::class, 'setValue');
27
28
        // Defining expectations.
29
        $resolver->shouldReceive('reflectCallable')
30
                 ->with([TestClass::class, 'setValue'])
31
                 ->andReturn($reflection)
32
                 ->once();
33
        $resolver->shouldReceive('resolveArguments')
34
                 ->with($reflection)
35
                 ->andReturn($callback)
36
                 ->once();
37
38
        // Creating inflector, setting resolver, adding inflection.
39
        $inflector = new ObjectInflector($resolver);
40
        $inflector->addInflection(TestClass::class, 'setValue', ['value' => 'value']);
41
42
        // Creating test object.
43
        $test = new TestClass(new stdClass());
44
45
        // At first value is empty.
46
        $this->assertNull($test->getValue());
47
48
        // Applying inflections.
49
        $inflector->applyInflections($test);
50
51
        // Now value was changed via TestClass::setValue() call.
52
        $this->assertSame('value', $test->getValue());
53
54
        // After first run inflection callable must be saved for better performance.
55
        $test2 = new TestClass(new stdClass());
56
        $inflector->applyInflections($test2);
57
        $this->assertSame($test->getValue(), $test2->getValue());
58
59
        // Inflection must be called only on TestClass instances.
60
        $inflector->applyInflections(
61
            Mockery::mock(stdClass::class)->shouldNotReceive('setValue')->getMock()
62
        );
63
64
        Mockery::close();
65
    }
66
67
}
68