|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the DbImporter package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace DbImporter\Tests; |
|
12
|
|
|
|
|
13
|
|
|
use DbImporter\Importer; |
|
14
|
|
|
use DbImporter\Tests\Entity\Photo; |
|
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
16
|
|
|
use Doctrine\DBAL\Configuration; |
|
17
|
|
|
use Doctrine\DBAL\Connection; |
|
18
|
|
|
use Doctrine\DBAL\DriverManager; |
|
19
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
20
|
|
|
use Faker\Factory; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
|
|
23
|
|
|
abstract class BaseTestCase extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param Importer $importer |
|
27
|
|
|
* @param Connection $connection |
|
28
|
|
|
* @param $tableName |
|
29
|
|
|
* @param array $keys |
|
30
|
|
|
* @param array|null $uniqueKeys |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function executeImportQueryAndPerformTests( |
|
33
|
|
|
Importer $importer, |
|
34
|
|
|
Connection $connection, |
|
35
|
|
|
$tableName, |
|
36
|
|
|
array $keys, |
|
37
|
|
|
array $uniqueKeys = null |
|
38
|
|
|
) { |
|
39
|
|
|
$this->createSchema($connection, $tableName, $keys, $uniqueKeys); |
|
40
|
|
|
$this->assertInstanceOf(Importer::class, $importer); |
|
41
|
|
|
$this->assertTrue($importer->execute()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Connection $connection |
|
46
|
|
|
* @param $tableName |
|
47
|
|
|
* @param array $keys |
|
48
|
|
|
* @param array|null $uniqueKeys |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function createSchema( |
|
51
|
|
|
Connection $connection, |
|
52
|
|
|
$tableName, |
|
53
|
|
|
array $keys, |
|
54
|
|
|
array $uniqueKeys = null |
|
55
|
|
|
) { |
|
56
|
|
|
$schema = new Schema(); |
|
57
|
|
|
|
|
58
|
|
|
if (false === $this->checkIfTableExists($connection, $tableName)) { |
|
59
|
|
|
$table = $schema->createTable($tableName); |
|
60
|
|
|
|
|
61
|
|
|
foreach ($keys as $key => $type) { |
|
62
|
|
|
$table->addColumn($key, $type); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($uniqueKeys) { |
|
66
|
|
|
$table->setPrimaryKey($uniqueKeys); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$platform = $connection->getDatabasePlatform(); |
|
70
|
|
|
$queries = $schema->toSql($platform); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($queries as $query) { |
|
73
|
|
|
$connection->executeQuery($query); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $table |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function checkIfTableExists(Connection $connection, $table) |
|
83
|
|
|
{ |
|
84
|
|
|
try { |
|
85
|
|
|
$query = 'SELECT count(*) as c FROM ' . $table; |
|
86
|
|
|
$connection->executeQuery($query); |
|
87
|
|
|
} catch (\Exception $e) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $limit |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function createPhotosArray($limit) |
|
99
|
|
|
{ |
|
100
|
|
|
$array = []; |
|
101
|
|
|
$faker = Factory::create(); |
|
102
|
|
|
|
|
103
|
|
|
for ($i = 1; $i <= $limit; $i++) { |
|
104
|
|
|
$array[] = [ |
|
105
|
|
|
'id' => $i, |
|
106
|
|
|
'albumId' => ($i+1), |
|
107
|
|
|
'title' => $faker->name, |
|
108
|
|
|
'url' => $faker->url, |
|
109
|
|
|
'thumbnailUrl' => $faker->imageUrl() |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $array; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param $limit |
|
118
|
|
|
* @return ArrayCollection |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function createPhotosCollection($limit) |
|
121
|
|
|
{ |
|
122
|
|
|
$array = []; |
|
123
|
|
|
$faker = Factory::create(); |
|
124
|
|
|
|
|
125
|
|
|
for ($i = 1; $i <= $limit; $i++) { |
|
126
|
|
|
$array[] = new Photo( |
|
127
|
|
|
$i, |
|
128
|
|
|
($i+1), |
|
129
|
|
|
$faker->name, |
|
130
|
|
|
$faker->url, |
|
131
|
|
|
$faker->imageUrl() |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return new ArrayCollection($array); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param $limit |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
public function createPhotosStdClassArray($limit) |
|
143
|
|
|
{ |
|
144
|
|
|
$url = 'https://jsonplaceholder.typicode.com/photos'; |
|
145
|
|
|
|
|
146
|
|
|
return array_slice(json_decode(file_get_contents($url)), 0, $limit); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param $url |
|
151
|
|
|
* @return Connection |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function getConnection($url) |
|
154
|
|
|
{ |
|
155
|
|
|
return DriverManager::getConnection( |
|
156
|
|
|
[ |
|
157
|
|
|
'url' => $url, |
|
158
|
|
|
], |
|
159
|
|
|
new Configuration() |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|