|
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'))); |
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|