1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
4
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
5
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
6
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
7
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
8
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MpaCustomDoctrineHydratorTest\Stdlib\Strategy; |
12
|
|
|
|
13
|
|
|
use MpaCustomDoctrineHydrator\Stdlib\Hydrator\Strategy\FormatConversionStrategy; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
class FormatConversionStrategyTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
protected $dateConfig; |
19
|
|
|
|
20
|
|
|
public function setup() |
21
|
|
|
{ |
22
|
|
|
$this->dateConfig = [ |
23
|
|
|
'date_format' => 'd/m/Y', |
24
|
|
|
'time_format' => 'H:i:s', |
25
|
|
|
'datetime_format' => 'd/m/Y H:i:s', |
26
|
|
|
'date_placeholder' => 'jj/mm/aaaa', |
27
|
|
|
'time_placeholder' => 'hh:mm:ss', |
28
|
|
|
'datetime_placeholder' => 'jj/mm/aaaa hh:mm:ss', |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testFormatConversionStrategyCanExtractAndFormatDate() |
33
|
|
|
{ |
34
|
|
|
$strategy = new FormatConversionStrategy($this->dateConfig['date_format']); |
35
|
|
|
$today = new \DateTime('2014-05-01'); |
36
|
|
|
|
37
|
|
|
$this->assertEquals('01/05/2014', $strategy->extract($today)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testFormatConversionStrategyCanExtractAndFormatDateAndTime() |
41
|
|
|
{ |
42
|
|
|
$strategy = new FormatConversionStrategy($this->dateConfig['datetime_format']); |
43
|
|
|
$today = new \DateTime('2014-05-01 05:11:24'); |
44
|
|
|
|
45
|
|
|
$this->assertEquals('01/05/2014 05:11:24', $strategy->extract($today)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testFormatConversionStrategyCanExtractAndFormatTime() |
49
|
|
|
{ |
50
|
|
|
$strategy = new FormatConversionStrategy($this->dateConfig['time_format']); |
51
|
|
|
$today = new \DateTime('05:11:24'); |
52
|
|
|
|
53
|
|
|
$this->assertEquals('05:11:24', $strategy->extract($today)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testFormatConversionReturnsANullValueIfNullPassedToConstructor() |
57
|
|
|
{ |
58
|
|
|
$strategy = new FormatConversionStrategy($this->dateConfig['date_format']); |
59
|
|
|
$today = null; |
60
|
|
|
|
61
|
|
|
$this->assertNull($strategy->extract($today)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @expectedException \InvalidArgumentException |
66
|
|
|
*/ |
67
|
|
|
public function testFormatConversionStrategyThrowsExceptionAtExtraction() |
68
|
|
|
{ |
69
|
|
|
$strategy = new FormatConversionStrategy($this->dateConfig['date_format']); |
70
|
|
|
$today = 'chewbacca'; |
71
|
|
|
$strategy->extract($today); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testCanHydrate() |
75
|
|
|
{ |
76
|
|
|
$strategy = new FormatConversionStrategy($this->dateConfig['date_format']); |
77
|
|
|
|
78
|
|
|
$this->assertEquals('test', $strategy->hydrate('test')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @expectedException \InvalidArgumentException |
83
|
|
|
*/ |
84
|
|
|
public function testStrategyThrowsExceptionIfStringNotGiven() |
85
|
|
|
{ |
86
|
|
|
$strategy = new FormatConversionStrategy($this->dateConfig); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$this->assertEquals('test', $strategy->hydrate('test')); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: