|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ADiaz\AML\OpenList\utils; |
|
4
|
|
|
|
|
5
|
|
|
use ADiaz\AML\OpenList\models\Entity; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This file is part of the OpenList Parser utility. |
|
9
|
|
|
* |
|
10
|
|
|
* |
|
11
|
|
|
* @category PHP |
|
12
|
|
|
* |
|
13
|
|
|
* @author Alberto Diaz <[email protected]> |
|
14
|
|
|
* @copyright 2016 Alberto Diaz <[email protected]> |
|
15
|
|
|
* @license This source file is subject to the MIT license that is bundled |
|
16
|
|
|
* |
|
17
|
|
|
* @version Release: @package_version@ |
|
18
|
|
|
* |
|
19
|
|
|
* @link http://tytem.com |
|
20
|
|
|
*/ |
|
21
|
|
|
class ExporterTest extends \PHPUnit_Framework_TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
protected $exporter; |
|
24
|
|
|
protected $output_path; |
|
25
|
|
|
|
|
26
|
|
|
protected function setUp() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->output_path = __DIR__.'/../../output/'; |
|
29
|
|
|
$this->exporter = new Exporter($this->output_path); |
|
30
|
|
|
|
|
31
|
|
|
if (!file_exists($this->output_path)) { |
|
32
|
|
|
mkdir($this->output_path, 0777, true); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testSerialize() |
|
37
|
|
|
{ |
|
38
|
|
|
$entities = array(); |
|
39
|
|
|
$prefix = 'test-serialize'; |
|
40
|
|
|
|
|
41
|
|
|
$entities[] = new Entity(1001); |
|
42
|
|
|
$entities[] = new Entity(1002); |
|
43
|
|
|
|
|
44
|
|
|
$this->exporter->serialize($entities, $prefix); |
|
45
|
|
|
|
|
46
|
|
|
//filename |
|
47
|
|
|
$filename = $prefix.date('_Y-m-d').'.serialized'; |
|
48
|
|
|
|
|
49
|
|
|
// check the file |
|
50
|
|
|
$this->assertFileExists($this->output_path.$filename); |
|
51
|
|
|
|
|
52
|
|
|
// check if it is possible to unserialize |
|
53
|
|
|
$this->assertNotFalse(unserialize(file_get_contents($this->output_path.$filename))); |
|
54
|
|
|
|
|
55
|
|
|
//remove test file |
|
56
|
|
|
unlink($this->output_path.$filename); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testJson() |
|
60
|
|
|
{ |
|
61
|
|
|
$prefix = 'test-json'; |
|
62
|
|
|
|
|
63
|
|
|
$entities[] = new Entity(1003); |
|
|
|
|
|
|
64
|
|
|
$entities[] = new Entity(1004); |
|
65
|
|
|
|
|
66
|
|
|
$this->exporter->json($entities, $prefix); |
|
67
|
|
|
|
|
68
|
|
|
//filename |
|
69
|
|
|
$filename = $prefix.date('_Y-m-d').'.json'; |
|
70
|
|
|
|
|
71
|
|
|
//expected result |
|
72
|
|
|
$expectedResult = '[{"external_id":1003},{"external_id":1004}]'; |
|
73
|
|
|
|
|
74
|
|
|
// check content generated |
|
75
|
|
|
$this->assertEquals(file_get_contents($this->output_path.$filename), $expectedResult); |
|
76
|
|
|
|
|
77
|
|
|
//remove test file |
|
78
|
|
|
unlink($this->output_path.$filename); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.