Skip to content

Commit a7594ed

Browse files
committed
fixed tests
1 parent 85642f4 commit a7594ed

File tree

4 files changed

+218
-99
lines changed

4 files changed

+218
-99
lines changed

Command/ImportCommand.php

Lines changed: 8 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Mathielen\ImportEngine\Import\Run\ImportRunner;
1010
use Mathielen\ImportEngine\ValueObject\ImportRequest;
1111
use Mathielen\ImportEngine\ValueObject\ImportRun;
12+
use Mathielen\ImportEngineBundle\Utils;
1213
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
1314
use Symfony\Component\Console\Helper\ProgressBar;
1415
use Symfony\Component\Console\Helper\Table;
@@ -68,7 +69,7 @@ protected function import(OutputInterface $output, $importerId, $sourceProviderI
6869
{
6970
$output->writeln("Commencing ".($isDryrun?'<comment>dry-run</comment> ':'')."import using importer ".(empty($importerId)?'<comment>unknown</comment>':"<info>$importerId</info>")." with source provider <info>$sourceProviderId</info> and source id <info>$sourceId</info>");
7071

71-
$sourceId = $this->parseSourceId($sourceId);
72+
$sourceId = Utils::parseSourceId($sourceId);
7273
$progress = new ProgressBar($output);
7374

7475
//set limit
@@ -81,7 +82,7 @@ protected function import(OutputInterface $output, $importerId, $sourceProviderI
8182
/** @var ImportBuilder $importBuilder */
8283
$importBuilder = $this->getContainer()->get('mathielen_importengine.import.builder');
8384

84-
$importRequest = new ImportRequest($sourceId, $sourceProviderId, $importerId, self::whoAmI().'@CLI');
85+
$importRequest = new ImportRequest($sourceId, $sourceProviderId, $importerId, Utils::whoAmI().'@CLI');
8586

8687
$import = $importBuilder->build($importRequest);
8788

@@ -90,7 +91,7 @@ protected function import(OutputInterface $output, $importerId, $sourceProviderI
9091
$importRun->setContext($context);
9192

9293
//status callback
93-
$this->getContainer()->get('event_dispatcher')->addListener('data-import.read', function (ImportItemEvent $event) use ($output, &$progress) {
94+
$this->getContainer()->get('event_dispatcher')->addListener(ImportItemEvent::AFTER_READ, function (ImportItemEvent $event) use ($output, &$progress) {
9495
/** @var ImportRun $importRun */
9596
$importRun = $event->getContext();
9697
$stats = $importRun->getStatistics();
@@ -132,6 +133,9 @@ protected function import(OutputInterface $output, $importerId, $sourceProviderI
132133

133134
protected function writeValidationViolations(array $violations, Table $table)
134135
{
136+
if (empty($violations)) {
137+
return;
138+
}
135139
$violations = $violations['source'] + $violations['target'];
136140

137141
$table
@@ -152,7 +156,7 @@ protected function writeValidationViolations(array $violations, Table $table)
152156

153157
$i = 0;
154158
foreach ($tree as $violation=>$lines) {
155-
$table->addRow([$violation, join(', ', self::numbersToRangeText($lines))]);
159+
$table->addRow([$violation, join(', ', Utils::numbersToRangeText($lines))]);
156160
++$i;
157161

158162
if ($i === self::MAX_VIOLATION_ERRORS) {
@@ -182,97 +186,4 @@ protected function writeStatistics(array $statistics, Table $table)
182186
$table->render();
183187
}
184188

185-
private function parseSourceId($sourceId)
186-
{
187-
if (is_file($sourceId)) {
188-
return $sourceId;
189-
} elseif (preg_match('/^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+/', $sourceId)) {
190-
$parsedSourceId = parse_url($sourceId);
191-
if (isset($parsedSourceId['query'])) {
192-
parse_str($parsedSourceId['query'], $parsedSourceId['query']);
193-
}
194-
$pathTokens = explode('.', $parsedSourceId['path']);
195-
$method = array_pop($pathTokens);
196-
$service = join('.', $pathTokens);
197-
198-
return array(
199-
'service' => $service,
200-
'method' => $method,
201-
'arguments' => isset($parsedSourceId['query'])?$parsedSourceId['query']:null
202-
);
203-
}
204-
205-
return $sourceId;
206-
}
207-
208-
/**
209-
* @return bool
210-
*/
211-
public static function isWindows()
212-
{
213-
return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
214-
}
215-
216-
/**
217-
* @return bool
218-
*/
219-
public static function isCli()
220-
{
221-
return php_sapi_name() == "cli";
222-
}
223-
224-
/**
225-
* @return string
226-
*/
227-
public static function whoAmI()
228-
{
229-
if (self::isWindows()) {
230-
$user = getenv("username");
231-
} else {
232-
$processUser = posix_getpwuid(posix_geteuid());
233-
$user = $processUser['name'];
234-
}
235-
236-
return $user;
237-
}
238-
239-
public static function numbersToRangeText(array $numbers)
240-
{
241-
if (empty($numbers)) {
242-
return [];
243-
}
244-
245-
$ranges = [];
246-
sort($numbers);
247-
248-
$currentRange = [];
249-
foreach ($numbers as $number) {
250-
if (empty($currentRange) || current($currentRange) === $number-1) {
251-
$currentRange[] = $number;
252-
end($currentRange);
253-
} else {
254-
$lastItem = current($currentRange);
255-
256-
if (count($currentRange) === 1) {
257-
$ranges[] = $lastItem;
258-
} else {
259-
$firstItem = reset($currentRange);
260-
$ranges[] = $firstItem . '-' . $lastItem;
261-
}
262-
263-
$currentRange = [];
264-
}
265-
}
266-
267-
$lastItem = current($currentRange);
268-
if (count($currentRange) === 1) {
269-
$ranges[] = $lastItem;
270-
} else {
271-
$firstItem = reset($currentRange);
272-
$ranges[] = $firstItem . '-' . $lastItem;
273-
}
274-
275-
return $ranges;
276-
}
277-
278189
}

Tests/Command/ImportCommandTest.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
namespace Mathielen\ImportEngineBundle\Tests\Command;
33

4-
use Infrastructure\Utils;
4+
use Mathielen\ImportEngine\Event\ImportConfigureEvent;
5+
use Mathielen\ImportEngineBundle\Utils;
56
use Mathielen\ImportEngine\Import\Import;
67
use Mathielen\ImportEngine\Importer\Importer;
78
use Mathielen\ImportEngine\ValueObject\ImportConfiguration;
@@ -38,6 +39,63 @@ protected function setUp()
3839
$this->command->setContainer($this->container);
3940
}
4041

42+
public function testRunLimit()
43+
{
44+
$this->container->get('mathielen_importengine.import.builder')
45+
->expects($this->once())
46+
->method('build')
47+
->will($this->returnValue(
48+
new Import(
49+
new Importer(
50+
$this->getMock('Mathielen\ImportEngine\Storage\StorageInterface')
51+
),
52+
$this->getMock('Mathielen\ImportEngine\Storage\StorageInterface'),
53+
new ImportRun(new ImportConfiguration())
54+
)
55+
));
56+
57+
$this->container->get('event_dispatcher')
58+
->expects($this->exactly(2))
59+
->method('addListener')
60+
->withConsecutive(
61+
array(ImportConfigureEvent::AFTER_BUILD . '.abc', $this->anything())
62+
);
63+
64+
$this->container->get('mathielen_importengine.import.runner')
65+
->expects($this->once())
66+
->method('run');
67+
68+
$input = new ArrayInput(array('--limit'=>1, '--importer'=>'abc', 'source_id'), $this->command->getDefinition());
69+
$output = new TestOutput();
70+
71+
$this->command->run($input, $output);
72+
}
73+
74+
public function testRunDryrun()
75+
{
76+
$this->container->get('mathielen_importengine.import.builder')
77+
->expects($this->once())
78+
->method('build')
79+
->will($this->returnValue(
80+
new Import(
81+
new Importer(
82+
$this->getMock('Mathielen\ImportEngine\Storage\StorageInterface')
83+
),
84+
$this->getMock('Mathielen\ImportEngine\Storage\StorageInterface'),
85+
new ImportRun(new ImportConfiguration())
86+
)
87+
));
88+
89+
$this->container->get('mathielen_importengine.import.runner')
90+
->expects($this->once())
91+
->method('dryrun');
92+
93+
$input = new ArrayInput(array('--dryrun'=>true, '--importer'=>'abc', 'source_id'), $this->command->getDefinition());
94+
$output = new TestOutput();
95+
96+
$this->command->run($input, $output);
97+
}
98+
4199
/**
42100
* @dataProvider getRunData
43101
*/
@@ -57,6 +115,10 @@ public function testRun(array $input, $parsedSourceId)
57115
)
58116
));
59117

118+
$this->container->get('mathielen_importengine.import.runner')
119+
->expects($this->once())
120+
->method('run');
121+
60122
$input = new ArrayInput($input, $this->command->getDefinition());
61123
$output = new TestOutput();
62124

@@ -66,7 +128,7 @@ public function testRun(array $input, $parsedSourceId)
66128
public function getRunData()
67129
{
68130
return array(
69-
array(array('source_id'=>'source_id', '--context'=>'key=value&otherkey=othervalue'), 'source_id'),
131+
array(array('source_id'=>'source_id'), 'source_id'),
70132
array(array('source_id'=>'service.method?arg1=abc'), array('service'=>'service', 'method'=>'method', 'arguments'=>array('arg1'=>'abc')))
71133
);
72134
}

Tests/UtilsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace Mathielen\ImportEngineBundle\Tests;
3+
4+
use Mathielen\ImportEngineBundle\Utils;
5+
6+
class UtilsTest extends \PHPUnit_Framework_TestCase
7+
{
8+
9+
public function testIsCli()
10+
{
11+
$this->assertTrue(Utils::isCli());
12+
}
13+
14+
public function testWhoAmI()
15+
{
16+
$processUser = posix_getpwuid(posix_geteuid());
17+
$actualUser = $processUser['name'];
18+
19+
$this->assertEquals($actualUser, Utils::whoAmI());
20+
}
21+
22+
/**
23+
* @dataProvider getNumbersToRangeTextData
24+
*/
25+
public function testNumbersToRangeText(array $range, $expected)
26+
{
27+
$this->assertEquals($expected, Utils::numbersToRangeText($range));
28+
}
29+
30+
public function getNumbersToRangeTextData()
31+
{
32+
return [
33+
[[], []],
34+
[[1], ['1']],
35+
[[1,2,3], ['1-3']],
36+
[[1,2,4,5], ['1-2', '4-5']],
37+
[[1,3,5,7], ['1', '3', '5', '7']]
38+
];
39+
}
40+
41+
}

Utils.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
namespace Mathielen\ImportEngineBundle;
3+
4+
class Utils
5+
{
6+
7+
/**
8+
* @return array
9+
*/
10+
public static function parseSourceId($sourceId)
11+
{
12+
if (is_file($sourceId)) {
13+
return $sourceId;
14+
} elseif (preg_match('/^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+/', $sourceId)) {
15+
$parsedSourceId = parse_url($sourceId);
16+
if (isset($parsedSourceId['query'])) {
17+
parse_str($parsedSourceId['query'], $parsedSourceId['query']);
18+
}
19+
$pathTokens = explode('.', $parsedSourceId['path']);
20+
$method = array_pop($pathTokens);
21+
$service = join('.', $pathTokens);
22+
23+
return array(
24+
'service' => $service,
25+
'method' => $method,
26+
'arguments' => isset($parsedSourceId['query'])?$parsedSourceId['query']:null
27+
);
28+
}
29+
30+
return $sourceId;
31+
}
32+
33+
/**
34+
* @return bool
35+
*/
36+
public static function isWindows()
37+
{
38+
return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
39+
}
40+
41+
/**
42+
* @return bool
43+
*/
44+
public static function isCli()
45+
{
46+
return php_sapi_name() == "cli";
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public static function whoAmI()
53+
{
54+
if (self::isWindows()) {
55+
$user = getenv("username");
56+
} else {
57+
$processUser = posix_getpwuid(posix_geteuid());
58+
$user = $processUser['name'];
59+
}
60+
61+
return $user;
62+
}
63+
64+
/**
65+
* @return array
66+
*/
67+
public static function numbersToRangeText(array $numbers)
68+
{
69+
if (empty($numbers)) {
70+
return [];
71+
}
72+
73+
$ranges = [];
74+
sort($numbers);
75+
76+
$currentRange = [];
77+
foreach ($numbers as $number) {
78+
if (!empty($currentRange) && current($currentRange) !== $number-1) {
79+
self::addRangeText($ranges, $currentRange);
80+
81+
$currentRange = [];
82+
}
83+
84+
$currentRange[] = $number;
85+
end($currentRange);
86+
}
87+
88+
self::addRangeText($ranges, $currentRange);
89+
90+
return $ranges;
91+
}
92+
93+
private static function addRangeText(array &$ranges, array $currentRange)
94+
{
95+
$lastItem = current($currentRange);
96+
97+
if (count($currentRange) === 1) {
98+
$ranges[] = $lastItem;
99+
} else {
100+
$firstItem = reset($currentRange);
101+
$ranges[] = $firstItem . '-' . $lastItem;
102+
}
103+
}
104+
105+
}

0 commit comments

Comments
 (0)