-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationTest.php
More file actions
199 lines (163 loc) · 5.74 KB
/
ApplicationTest.php
File metadata and controls
199 lines (163 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace Keboola\GoogleAnalyticsExtractor;
use Generator;
use JsonException;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Process;
class ApplicationTest extends TestCase
{
private array $config;
private string $dataDir;
private string $configDir;
public function setUp(): void
{
$this->dataDir = '/tmp/data-test';
$this->configDir = __DIR__ . '/../../../tests/data';
$this->config = $this->getConfig();
$filesystem = new Filesystem();
$filesystem->remove($this->dataDir . '/out/tables');
$filesystem->mkdir($this->dataDir . '/out/tables');
}
private function getConfig(string $suffix = ''): array
{
$config = json_decode(
(string) file_get_contents($this->configDir . '/config' . $suffix . '.json'),
true,
);
$config['authorization']['oauth_api']['credentials'] = [
'appKey' => getenv('CLIENT_ID'),
'#appSecret' => getenv('CLIENT_SECRET'),
'#data' => json_encode([
'access_token' => getenv('ACCESS_TOKEN'),
'refresh_token' => getenv('REFRESH_TOKEN'),
]),
];
return $config;
}
public function testAppRunDailyWalk(): void
{
$this->config = $this->getConfig('_antisampling');
$this->runProcess();
$dailyWalk = $this->getManifestFiles('dailyWalk');
Assert::assertEquals(1, count($dailyWalk));
foreach ($dailyWalk as $file) {
/** @var $file SplFileInfo */
$this->assertManifestContainsColumns($file->getPathname(), [
'id',
'idProfile',
'date',
'sourceMedium',
'landingPagePath',
'pageviews',
]);
}
}
public function testAppRunAdaptive(): void
{
$this->config = $this->getConfig('_antisampling_adaptive');
$this->runProcess();
$adaptive = $this->getManifestFiles('adaptive');
Assert::assertEquals(1, count($adaptive));
foreach ($adaptive as $file) {
/** @var $file SplFileInfo */
$this->assertManifestContainsColumns($file->getPathname(), [
'id',
'idProfile',
'date',
'sourceMedium',
'landingPagePath',
'pageviews',
]);
}
}
public function testAppRunMCF(): void
{
$this->config = $this->getConfig('_mcf');
$this->runProcess();
$funnelFiles = $this->getManifestFiles('funnel');
Assert::assertEquals(1, count($funnelFiles));
foreach ($funnelFiles as $file) {
/** @var $file SplFileInfo */
$this->assertManifestContainsColumns($file->getPathname(), [
'id',
'idProfile',
'mcf:conversionDate',
'mcf:sourcePath',
'mcf:mediumPath',
'mcf:sourceMedium',
'mcf:totalConversions',
'mcf:totalConversionValue',
'mcf:assistedConversions',
]);
}
}
public function testAppProfilesProperties(): void
{
$this->config = $this->getConfig('_empty');
$this->config['action'] = 'getProfilesProperties';
$result = json_decode($this->runProcess()->getOutput(), true);
Assert::assertArrayHasKey('profiles', $result);
Assert::assertArrayHasKey('properties', $result);
}
public function testAppRunProperties(): void
{
$this->config = $this->getConfig('_properties');
$this->runProcess();
$properties = $this->getOutputFiles('properties');
$propertiesManifests = $this->getManifestFiles('properties');
$users = $this->getOutputFiles('users');
$usersManifests = $this->getManifestFiles('users');
Assert::assertEquals(1, count($properties));
Assert::assertEquals(1, count($propertiesManifests));
Assert::assertEquals(1, count($users));
Assert::assertEquals(1, count($usersManifests));
}
private function runProcess(): Process
{
$fs = new Filesystem();
$fs->remove($this->dataDir);
$fs->mkdir($this->dataDir);
$fs->mkdir($this->dataDir . '/out/tables');
file_put_contents($this->dataDir . '/config.json', json_encode($this->config));
$process = new Process(['php', 'src/run.php']);
$process->setEnv([
'KBC_DATADIR' => $this->dataDir,
]);
$process->setTimeout(900);
$process->run();
return $process;
}
private function getOutputFiles(string $queryName): Finder
{
$finder = new Finder();
return $finder->files()
->in($this->dataDir . '/out/tables')
->name('/^' . $queryName . '.*\.csv$/i')
;
}
private function getManifestFiles(string $queryName): Finder
{
$finder = new Finder();
return $finder->files()
->in($this->dataDir . '/out/tables')
->name('/^' . $queryName . '.*\.csv.manifest$/i')
;
}
private function assertManifestContainsColumns(string $pathname, array $expected): void
{
$manifest = (array) json_decode(file_get_contents($pathname), true, 512, JSON_THROW_ON_ERROR);
Assert::assertArrayHasKey('columns', $manifest);
Assert::assertEquals($expected, $manifest['columns']);
}
public function appRunDataProvider(): Generator
{
yield 'configRow' => [
'',
];
}
}