Completed
Push — refactor-04-parser-tests ( dc4950...bd4663 )
by John
06:06
created

FieldParserDateOffsetTest::testGetText()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.9411
c 0
b 0
f 0
cc 1
eloc 37
nc 1
nop 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 Graze\CiffRenderer\Test\SimpleXmlElementChainMocker;
9
use Graze\CiffRenderer\Parser\FieldParser\DateFormatter\DateFormatterInterface;
10
use Graze\CiffRenderer\Parser\FieldParser\DateFormatter\DateFormatterFactory;
11
12
class FieldParserDateOffsetTest extends AbstractFieldParserTest
13
{
14
    /**
15
     * @var FieldParserDateOffset
16
     */
17
    private $parser;
18
19
    /**
20
     * @var \DateTimeInterface
21
     */
22
    private $dateTimeNow;
23
24
    /**
25
     * @var DateFormatterFactory
26
     */
27
    private $dateFormatterFactory;
28
29
    /**
30
     * @return FieldParserDateOffset
31
     */
32 View Code Duplication
    protected function getParser()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        if (!$this->parser) {
35
            $this->dateTimeNow = m::mock(\DateTimeImmutable::class);
36
            $this->dateFormatterFactory = m::mock(DateFormatterFactory::class);
37
            $this->parser = new FieldParserDateOffset($this->dateTimeNow, $this->dateFormatterFactory);
38
        }
39
40
        return $this->parser;
41
    }
42
43
    public function testGetText()
44
    {
45
        $offsetFieldName = 'i am offset field name';
46
        SimpleXmlElementChainMocker::addChain(
47
            ['xmlField', 'Data', 'Object', 'OffsetDate'],
48
            ['SrcOffset' => $offsetFieldName]
49
        );
50
51
        $offsetDays = 2;
52
        $offsetField = SimpleXmlElementChainMocker::addChain(['offsetField', 'DefaultOffset', 'Day'], $offsetDays);
53
        $this->xmlHeader
54
            ->shouldReceive('xpath')
55
            ->with('//DateOffset[@Name="'. $offsetFieldName .'"]')
56
            ->andReturn([$offsetField])
57
            ->once()
58
            ->getMock();
59
60
        $dateIntervalExpected = function (\DateInterval $dateInterval) use ($offsetDays) {
61
            return $dateInterval->d == $offsetDays;
62
        };
63
        $dateOffset = m::mock(\DateTime::class);
64
        $this->dateTimeNow
65
            ->shouldReceive('add')
66
            ->with(m::on($dateIntervalExpected))
67
            ->andReturn($dateOffset)
68
            ->once()
69
            ->getMock();
70
71
        $dateFormat = 'i am date format';
72
        SimpleXmlElementChainMocker::addChain(['xmlField', 'Data', 'Object', 'Default'], $dateFormat);
73
74
        $text = 'i am text';
75
        $dateFormatter = m::mock(DateFormatterInterface::class)
76
            ->shouldReceive('format')
77
            ->with($dateOffset, $dateFormat)
78
            ->andReturn($text)
79
            ->once()
80
            ->getMock();
81
        $this->dateFormatterFactory
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Graze\CiffRendere...r\DateFormatterFactory>.

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...
82
            ->shouldReceive('getFormatter')
83
            ->with($dateFormat)
84
            ->andReturn($dateFormatter)
85
            ->getMock();
86
87
        $this->assertEquals($text, $this->getParser()->getText());
88
    }
89
}
90