File tree Expand file tree Collapse file tree 3 files changed +52
-10
lines changed Expand file tree Collapse file tree 3 files changed +52
-10
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Camillebaronnet \ETL \Tests \Transformer ;
4
+
5
+ use Camillebaronnet \ETL \Transformer \DateTime ;
6
+ use PHPUnit \Framework \TestCase ;
7
+
8
+ final class DateTimeTest extends TestCase
9
+ {
10
+ /**
11
+ * @var DateTime
12
+ */
13
+ private $ dateTime ;
14
+
15
+ /**
16
+ * @var array
17
+ */
18
+ private $ data = [
19
+ 'name ' => 'Bar ' ,
20
+ 'created_at ' => '20/06/1990 12h15 ' ,
21
+ 'updated_at ' => '21/06/2010 6h02 ' ,
22
+ ];
23
+
24
+
25
+ protected function setUp ()
26
+ {
27
+ $ this ->dateTime = new DateTime ();
28
+ }
29
+
30
+ public function testCanReformatTheDatesBySpecifyingInputFormatAndOutputFormat ()
31
+ {
32
+ $ this ->dateTime ->from = 'd/m/Y H\hi ' ;
33
+ $ this ->dateTime ->to = 'Y-m-d H:i:s ' ;
34
+ $ this ->dateTime ->fields = ['created_at ' , 'updated_at ' ];
35
+
36
+ $ result = $ this ->dateTime ->__invoke ($ this ->data );
37
+
38
+ $ this ->assertEquals ($ result , [
39
+ 'name ' => 'Bar ' ,
40
+ 'created_at ' => '1990-06-20 12:15:00 ' ,
41
+ 'updated_at ' => '2010-06-21 06:02:00 ' ,
42
+ ]);
43
+ }
44
+ }
Original file line number Diff line number Diff line change 5
5
class DateTime implements TransformInterface
6
6
{
7
7
public $ fields = [];
8
- public $ format = 'Y-m-d H:i:s ' ;
8
+ public $ from = 'Y-m-d H:i:s ' ;
9
+ public $ to ;
9
10
10
11
public function __invoke (array $ data ): array
11
12
{
12
13
foreach ($ this ->fields as $ fieldName ){
13
14
$ data [$ fieldName ] = \DateTime::createFromFormat (
14
- $ this ->format ,
15
+ $ this ->from ,
15
16
$ data [$ fieldName ]
16
17
);
18
+
19
+ if ($ this ->to ){
20
+ $ data [$ fieldName ] = $ data [$ fieldName ]->format ($ this ->to );
21
+ }
17
22
}
18
23
19
24
return $ data ;
Original file line number Diff line number Diff line change @@ -6,19 +6,12 @@ class Sleep implements TransformInterface
6
6
{
7
7
public $ seconds = 1 ;
8
8
9
- /**
10
- * The saved context, hydrated by the __invoke.
11
- *
12
- * @var array
13
- */
14
- protected $ context = [];
15
-
16
9
/**
17
10
* The class entry point.
18
11
*/
19
12
public function __invoke (array $ data ): array
20
13
{
21
- sleep ($ this ->seconds );
14
+ usleep ($ this ->seconds * 1000000 );
22
15
23
16
return $ data ;
24
17
}
You can’t perform that action at this time.
0 commit comments