VariadicValueResolverTest::testResolve()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Tests\Controller\ArgumentResolver;
12
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
15
use Symfony\Component\HttpFoundation\ParameterBag;
16
use Yarhon\RouteGuardBundle\Controller\ArgumentResolver\VariadicValueResolver;
17
use Yarhon\RouteGuardBundle\Controller\ArgumentResolver\ArgumentResolverContext;
18
use Yarhon\RouteGuardBundle\Exception\RuntimeException;
19
20
/**
21
 * @author Yaroslav Honcharuk <[email protected]>
22
 */
23
class VariadicValueResolverTest extends TestCase
24
{
25
    private $attributes;
26
27
    private $context;
28
29
    private $argument;
30
31
    public function setUp()
32
    {
33
        $this->attributes = $this->createMock(ParameterBag::class);
34
35
        $this->context = $this->createMock(ArgumentResolverContext::class);
36
37
        $this->context->method('getAttributes')
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

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

37
        $this->context->/** @scrutinizer ignore-call */ 
38
                        method('getAttributes')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
            ->willReturn($this->attributes);
39
40
        $this->argument = $this->createMock(ArgumentMetadata::class);
41
    }
42
43
    public function testSupportsNonVariadic()
44
    {
45
        $resolver = new VariadicValueResolver();
46
47
        $this->argument->method('isVariadic')
48
            ->willReturn(false);
49
50
        $this->assertFalse($resolver->supports($this->context, $this->argument));
51
    }
52
53
    public function testSupportsNotHasAttribute()
54
    {
55
        $resolver = new VariadicValueResolver();
56
57
        $this->argument->method('isVariadic')
58
            ->willReturn(true);
59
60
        $this->argument->method('getName')
61
            ->willReturn('arg');
62
63
        $this->attributes->expects($this->once())
64
            ->method('has')
65
            ->with('arg')
66
            ->willReturn(false);
67
68
        $this->assertFalse($resolver->supports($this->context, $this->argument));
69
    }
70
71
    public function testSupportsHasAttribute()
72
    {
73
        $resolver = new VariadicValueResolver();
74
75
        $this->argument->method('isVariadic')
76
            ->willReturn(true);
77
78
        $this->argument->method('getName')
79
            ->willReturn('arg');
80
81
        $this->attributes->expects($this->once())
82
            ->method('has')
83
            ->with('arg')
84
            ->willReturn(true);
85
86
        $this->assertTrue($resolver->supports($this->context, $this->argument));
87
    }
88
89
    public function testResolve()
90
    {
91
        $resolver = new VariadicValueResolver();
92
93
        $this->argument->method('getName')
94
            ->willReturn('arg');
95
96
        $this->attributes->expects($this->once())
97
            ->method('get')
98
            ->with('arg')
99
            ->willReturn([5]);
100
101
        $this->assertSame([5], $resolver->resolve($this->context, $this->argument));
102
    }
103
104
    public function testResolveException()
105
    {
106
        $resolver = new VariadicValueResolver();
107
108
        $this->argument->method('getName')
109
            ->willReturn('arg');
110
111
        $this->attributes->expects($this->once())
112
            ->method('get')
113
            ->with('arg')
114
            ->willReturn(5);
115
116
        $this->expectException(RuntimeException::class);
117
        $this->expectExceptionMessage('The action argument "...$arg" is required to be an array, the request attribute "arg" contains a type of "integer" instead.');
118
119
        $resolver->resolve($this->context, $this->argument);
120
    }
121
}
122