Completed
Push — master ( fa2c24...0b15ad )
by Dmitri
131:14 queued 125:30
created

DateNormalizerTest::makeMock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Devmachine\Bundle\FormBundle\Tests\Form;
4
5
use Devmachine\Bundle\FormBundle\Form\DateNormalizer;
6
7
class DateNormalizerTest extends \PHPUnit_Framework_TestCase
8
{
9
    /** @var DateNormalizer */
10
    private $normalizer;
11
12
    public function setUp()
13
    {
14
        $this->normalizer = new DateNormalizer();
15
    }
16
17
    /**
18
     * @test
19
     */
20
    public function it_leaves_non_datetime_type_intact()
21
    {
22
        $dummy = new \stdClass();
23
        $this->assertSame($dummy, $this->normalizer->normalizeDate($dummy, $this->makeMock('Symfony\Component\Form\FormInterface')));
0 ignored issues
show
Bug introduced by
$dummy of type stdClass is incompatible with the type string|DateTime expected by parameter $date of Devmachine\Bundle\FormBu...alizer::normalizeDate(). ( Ignorable by Annotation )

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

23
        $this->assertSame($dummy, $this->normalizer->normalizeDate(/** @scrutinizer ignore-type */ $dummy, $this->makeMock('Symfony\Component\Form\FormInterface')));
Loading history...
24
    }
25
26
    /**
27
     * @test
28
     */
29
    public function it_normalizes_date()
30
    {
31
        $date              = new \DateTime();
32
        $datePreNormalized = new \DateTime();
33
        $dateNormalized    = new \DateTime();
34
35
        $transformer1 = $this->makeMock('Symfony\Component\Form\DataTransformerInterface');
36
        $transformer2 = $this->makeMock('Symfony\Component\Form\DataTransformerInterface');
37
38
        $transformer1
39
            ->expects($this->once())
40
            ->method('transform')
41
            ->with($this->identicalTo($date))
0 ignored issues
show
Bug introduced by
$this->identicalTo($date) of type PHPUnit_Framework_Constraint_IsIdentical is incompatible with the type array expected by parameter $arguments of PHPUnit_Framework_MockOb...nvocationMocker::with(). ( Ignorable by Annotation )

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

41
            ->with(/** @scrutinizer ignore-type */ $this->identicalTo($date))
Loading history...
42
            ->willReturn($datePreNormalized)
43
        ;
44
        $transformer2
45
            ->expects($this->once())
46
            ->method('transform')
47
            ->with($this->identicalTo($datePreNormalized))
48
            ->willReturn($dateNormalized)
49
        ;
50
51
        $result = $this->normalizer->normalizeDate($date, $this->getForm([$transformer1, $transformer2]));
52
        $this->assertSame($dateNormalized, $result);
53
    }
54
55
    /**
56
     * @param array $transformers
57
     *
58
     * @return \Symfony\Component\Form\FormInterface
59
     */
60
    private function getForm(array $transformers)
61
    {
62
        $config = $this->makeMock('Symfony\Component\Form\FormConfigInterface');
63
        $config
64
            ->expects($this->once())
65
            ->method('getViewTransformers')
66
            ->willReturn($transformers)
67
        ;
68
69
        $form = $this->makeMock('Symfony\Component\Form\FormInterface');
70
        $form
71
            ->expects($this->once())
72
            ->method('getConfig')
73
            ->willReturn($config)
74
        ;
75
76
        return $form;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $form returns the type PHPUnit_Framework_MockObject_MockObject which is incompatible with the documented return type Symfony\Component\Form\FormInterface.
Loading history...
77
    }
78
79
    private function makeMock($originalClassName)
80
    {
81
        if (method_exists($this, 'createMock')) {
82
            return $this->createMock($originalClassName);
83
        }
84
85
        return $this->getMock($originalClassName);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit_Framework_TestCase::getMock() has been deprecated: Method deprecated since Release 5.4.0 ( Ignorable by Annotation )

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

85
        return /** @scrutinizer ignore-deprecated */ $this->getMock($originalClassName);

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...
86
    }
87
}
88