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

FieldParserDateTest::testGetText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 18
rs 9.7998
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\FieldParserDate;
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 FieldParserDateTest extends AbstractFieldParserTest
14
{
15
    /**
16
     * @var FieldParserDate
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>DateText</FldType>
38
                <Displayed>1</Displayed>
39
                <X>4200</X>
40
                <Y>250</Y>
41
                <W>3775</W>
42
                <H>450</H>
43
                <CalcData><![CDATA[212]]></CalcData>
44
                <Data>
45
                    <Object Static="0" Parse="1">
46
                        <DataType>2</DataType>
47
                        <Default><![CDATA[i am date format]]></Default>
48
                    </Object>
49
                </Data>
50
                <Text>
51
                    <Font>
52
                        <Pitch>10</Pitch>
53
                    </Font>
54
                </Text>
55
            </Field>'
56
        );
57
58
        return $this->xmlField;
0 ignored issues
show
Unused Code introduced by
return $this->xmlField is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
59
    }
60
61
    /**
62
     * @return FieldParserDate
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 FieldParserDate($this->dateTimeNow, $this->dateFormatterFactory);
0 ignored issues
show
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...rserDate::__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 FieldParserDate($this->dateTimeNow, /** @scrutinizer ignore-type */ $this->dateFormatterFactory);
Loading history...
Bug introduced by
$this->dateTimeNow of type Mockery\MockInterface is incompatible with the type DateTimeImmutable expected by parameter $dateTimeNow of Graze\CiffRenderer\Parse...rserDate::__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 FieldParserDate(/** @scrutinizer ignore-type */ $this->dateTimeNow, $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...dParser\FieldParserDate 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
        $dateFormat = 'i am date format';
78
79
        $text = 'i am text';
80
        $dateFormatter = m::mock(DateFormatterInterface::class)
81
            ->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

81
            ->/** @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...
82
            ->with($this->dateTimeNow, $dateFormat)
83
            ->andReturn($text)
84
            ->once()
85
            ->getMock();
86
        $this->dateFormatterFactory
87
            ->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

87
            ->/** @scrutinizer ignore-call */ 
88
              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...
88
            ->with($dateFormat)
89
            ->andReturn($dateFormatter)
90
            ->getMock();
91
92
        $this->assertEquals($text, $this->getParser()->getText());
93
    }
94
}
95