Passed
Branch develop (84afed)
by Paulius
02:35
created

testDoNotMapFieldWhichIsNotPresentInResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Dokobit\Gateway\Tests;
3
4
use Dokobit\Gateway\ResponseMapper;
5
6
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Dokobit\Gateway\Tests\TestCase. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
class ResponseMapperTest extends TestCase
9
{
10
    private $resultMock;
11
12
    protected function setUp(): void
13
    {
14
        $this->resultMock = $this
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

14
        $this->resultMock = /** @scrutinizer ignore-deprecated */ $this

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
15
            ->getMockBuilder('Dokobit\Gateway\Result\ResultInterface')
16
            ->setMethods(['getFields', 'setFieldName1', 'setField2', 'setField1'])
17
            ->disableOriginalConstructor()
18
            ->getMock()
19
        ;
20
    }
21
22
    public function testMap()
23
    {
24
        $mapper = new ResponseMapper();
25
26
        $this->resultMock->method('getFields')
27
            ->willReturn([
28
                'field_name_1',
29
                'field2',
30
            ])
31
        ;
32
        $this->resultMock
33
            ->expects($this->once())
34
            ->method('setFieldName1')
35
            ->with($this->equalTo('value1'))
36
        ;
37
        $this->resultMock
38
            ->expects($this->once())
39
            ->method('setField2')
40
            ->with($this->equalTo('value2'))
41
        ;
42
43
        $result = $mapper->map(
44
            ['field_name_1' => 'value1', 'field2' => 'value2'],
45
            $this->resultMock
46
        );
47
        $this->assertInstanceOf('Dokobit\Gateway\Result\ResultInterface', $result);
48
    }
49
50
    public function testDoNotMapFieldWhichIsNotPresentInResponse()
51
    {
52
        $mapper = new ResponseMapper();
53
54
        $this->resultMock->method('getFields')
55
            ->willReturn([
56
                'field1',
57
            ])
58
        ;
59
        $this->resultMock
60
            ->expects($this->exactly(0))
61
            ->method('setField1')
62
            ->with($this->equalTo('value1'))
63
        ;
64
65
        $result = $mapper->map(['field2' => 'value2'], $this->resultMock);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
66
    }
67
}
68