Completed
Push — master ( fd3130...674c45 )
by John
03:39
created

FieldParserDateOffsetTest::getParser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\CiffRenderer\Test\Parser\FieldParser;
4
5
use Mockery as m;
6
use Graze\CiffRenderer\Test\AbstractFieldParserTest;
7
use Graze\CiffRenderer\Parser\FieldParser\FieldParserDateOffset;
8
use \SimpleXMLElement;
9
use \DateTimeImmutable;
10
use Graze\CiffRenderer\Parser\FieldParser\DateFormatter\DateFormatterInterface;
11
use Graze\CiffRenderer\Parser\FieldParser\DateFormatter\DateFormatterFactory;
12
13
class FieldParserDateOffsetTest extends AbstractFieldParserTest
14
{
15
    /**
16
     * @var FieldParserDateOffset
17
     */
18
    private $parser;
19
20
    /**
21
     * @var DateTimeImmutable
22
     */
23
    private $dateTimeNow;
24
25
    /**
26
     * @var DateFormatterFactory
27
     */
28
    private $dateFormatterFactory;
29
30
    /**
31
     * @return SimpleXMLElement
32
     */
33
    protected function getXmlField()
34
    {
35
        return new SimpleXMLElement(
36
            '<Field Name="i am field name">
37
                <FldType>OffsetDateText</FldType>
38
                <Displayed>1</Displayed>
39
                <X>4200</X>
40
                <Y>250</Y>
41
                <W>3775</W>
42
                <H>450</H>
43
                <Ln>1</Ln>
44
                <CalcData><![CDATA[31/07/18]]></CalcData>
45
                <Data>
46
                    <Object Static="0" Parse="1">
47
                        <DataType>3</DataType>
48
                        <Default><![CDATA[i am date format]]></Default>
49
                        <OffsetDate SrcOffset="lifetime"/>
50
                    </Object>
51
                </Data>
52
                <Text>
53
                    <Font>
54
                        <Pitch>10</Pitch>
55
                    </Font>
56
                </Text>
57
            </Field>'
58
        );
59
    }
60
61
    /**
62
     * @return FieldParserDateOffset
63
     */
64
    protected function getParser()
65
    {
66
        if (!$this->parser) {
67
            $this->dateTimeNow = m::mock(DateTimeImmutable::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Mockery::mock(DateTimeImmutable::class) of type Mockery\MockInterface is incompatible with the declared type DateTimeImmutable of property $dateTimeNow.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
            $this->dateFormatterFactory = m::mock(DateFormatterFactory::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like Mockery::mock(Graze\Ciff...ormatterFactory::class) of type Mockery\MockInterface is incompatible with the declared type Graze\CiffRenderer\Parse...er\DateFormatterFactory of property $dateFormatterFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
            $this->parser = new FieldParserDateOffset($this->dateTimeNow, $this->dateFormatterFactory);
0 ignored issues
show
Bug introduced by
$this->dateTimeNow of type Mockery\MockInterface is incompatible with the type DateTimeImmutable expected by parameter $dateTimeNow of Graze\CiffRenderer\Parse...teOffset::__construct(). ( Ignorable by Annotation )

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

69
            $this->parser = new FieldParserDateOffset(/** @scrutinizer ignore-type */ $this->dateTimeNow, $this->dateFormatterFactory);
Loading history...
Bug introduced by
$this->dateFormatterFactory of type Mockery\MockInterface is incompatible with the type Graze\CiffRenderer\Parse...er\DateFormatterFactory expected by parameter $dateFormatterFactory of Graze\CiffRenderer\Parse...teOffset::__construct(). ( Ignorable by Annotation )

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

69
            $this->parser = new FieldParserDateOffset($this->dateTimeNow, /** @scrutinizer ignore-type */ $this->dateFormatterFactory);
Loading history...
70
        }
71
72
        return $this->parser;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parser returns the type Graze\CiffRenderer\Parse...r\FieldParserDateOffset which is incompatible with the return type mandated by Graze\CiffRenderer\Test\...ParserTest::getParser() of Graze\CiffRenderer\Test\...er\FieldParserInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
73
    }
74
75
    public function testGetText()
76
    {
77
        $offsetFieldName = 'i am offset field name';
0 ignored issues
show
Unused Code introduced by
The assignment to $offsetFieldName is dead and can be removed.
Loading history...
78
        $offsetDays = 130;
79
        $offset = new \DateInterval('P' . $offsetDays . 'D');
80
81
        $dateOffset = m::mock(\DateTime::class);
82
        $this->dateTimeNow
83
            ->shouldReceive('add')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on DateTimeImmutable. ( Ignorable by Annotation )

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

83
            ->/** @scrutinizer ignore-call */ 
84
              shouldReceive('add')

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...
84
            ->with(equalTo($offset))
85
            ->andReturn($dateOffset)
86
            ->once()
87
            ->getMock();
88
89
        $dateFormat = 'i am date format';
90
91
        $text = 'i am text';
92
        $dateFormatter = m::mock(DateFormatterInterface::class)
93
            ->shouldReceive('format')
0 ignored issues
show
Unused Code introduced by
The call to Mockery\MockInterface::shouldReceive() has too many arguments starting with 'format'. ( Ignorable by Annotation )

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

93
            ->/** @scrutinizer ignore-call */ shouldReceive('format')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
94
            ->with($dateOffset, $dateFormat)
95
            ->andReturn($text)
96
            ->once()
97
            ->getMock();
98
99
        $this->dateFormatterFactory
100
            ->shouldReceive('getFormatter')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on Graze\CiffRenderer\Parse...er\DateFormatterFactory. ( Ignorable by Annotation )

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

100
            ->/** @scrutinizer ignore-call */ 
101
              shouldReceive('getFormatter')

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...
101
            ->with($dateFormat)
102
            ->andReturn($dateFormatter)
103
            ->getMock();
104
105
        $this->assertEquals($text, $this->getParser()->getText());
106
    }
107
}
108