Skip to content

Commit

Permalink
add recoursive directory iterator (#23)
Browse files Browse the repository at this point in the history
* fix code style - add brackets to new
* add nested iterator for SelectFilesToProceedTask
  • Loading branch information
marvin255 authored Aug 3, 2020
1 parent 693180f commit e981497
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $finder = PhpCsFixer\Finder::create()->in(__DIR__);

$rules = [
'@Symfony' => true,
'new_with_braces' => false,
'new_with_braces' => true,
'phpdoc_inline_tag' => false,
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
Expand Down
4 changes: 2 additions & 2 deletions src/FiasInformer/SoapFiasInformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getCompleteInfo(): InformerResponse
{
$response = $this->getSoapClient()->__call('GetLastDownloadFileInfo', []);

$res = new InformerResponseBase;
$res = new InformerResponseBase();
$res->setVersion((int) $response->GetLastDownloadFileInfoResult->VersionId);
$res->setUrl($response->GetLastDownloadFileInfoResult->FiasCompleteXmlUrl);

Expand All @@ -56,7 +56,7 @@ public function getDeltaInfo(int $version): InformerResponse
$response = $this->getSoapClient()->__call('GetAllDownloadFileInfo', []);
$versions = $this->sortResponseByVersion($response->GetAllDownloadFileInfoResult->DownloadFileInfo);

$res = new InformerResponseBase;
$res = new InformerResponseBase();
foreach ($versions as $serviceVersion) {
if ((int) $serviceVersion['VersionId'] <= $version) {
continue;
Expand Down
11 changes: 8 additions & 3 deletions src/Pipeline/Task/SelectFilesToProceedTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Liquetsoft\Fias\Component\Pipeline\State\State;
use Psr\Log\LogLevel;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;

class SelectFilesToProceedTask implements Task, LoggableTask
Expand Down Expand Up @@ -96,19 +97,23 @@ protected function getFilesForProceedFromFolder(SplFileInfo $filesFolder): array
$filesToInsert = [];
$filesToDelete = [];

$iterator = new RecursiveDirectoryIterator(
$directoryIterator = new RecursiveDirectoryIterator(
$filesFolder->getRealPath(),
RecursiveDirectoryIterator::SKIP_DOTS
);
$iterator = new RecursiveIteratorIterator($directoryIterator);

foreach ($iterator as $fileInfo) {
if ($this->isFileAllowedToInsert($fileInfo)) {
$filesToInsert[] = $fileInfo->getRealPath();
$filesToInsert[] = (string) $fileInfo->getRealPath();
} elseif ($this->isFileAllowedToDelete($fileInfo)) {
$filesToDelete[] = $fileInfo->getRealPath();
$filesToDelete[] = (string) $fileInfo->getRealPath();
}
}

sort($filesToInsert, SORT_STRING);
sort($filesToDelete, SORT_STRING);

return [$filesToInsert, $filesToDelete];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Unpacker/ZipUnpacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function unpack(SplFileInfo $source, SplFileInfo $destination): void
private function runUnZip(SplFileInfo $source, SplFileInfo $destination): void
{
$filePath = $source->getPathName();
$zip = new ZipArchive;
$zip = new ZipArchive();
if ($zip->open($filePath) === true) {
$zip->extractTo($destination->getPathName());
$zip->close();
Expand Down
2 changes: 1 addition & 1 deletion src/XmlReader/BaseXmlReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ protected function resetReader(): PhpXmlReader
}

$this->unsetReader();
$this->reader = new PhpXmlReader;
$this->reader = new PhpXmlReader();

if ($this->reader->open($this->file->getPathname(), 'UTF-8', LIBXML_COMPACT | LIBXML_NONET | LIBXML_NOBLANKS) === false) {
throw new RuntimeException(
Expand Down
2 changes: 1 addition & 1 deletion tests/src/EntityManager/BaseEntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testGetDescriptorByObject()
$entityName1 => $class1,
]);

$this->assertSame($descriptor, $manager->getDescriptorByObject(new \stdClass));
$this->assertSame($descriptor, $manager->getDescriptorByObject(new \stdClass()));
$this->assertNull($manager->getDescriptorByObject('TestEmpty'));
}

Expand Down
8 changes: 4 additions & 4 deletions tests/src/FiasInformer/InformerResponseBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetSetVersion()
{
$version = $this->createFakeData()->numberBetween(1, 10000);

$res = new InformerResponseBase;
$res = new InformerResponseBase();
$res->setVersion($version);

$this->assertSame($version, $res->getVersion());
Expand All @@ -34,7 +34,7 @@ public function testGetSetUrl()
{
$url = $this->createFakeData()->url;

$res = new InformerResponseBase;
$res = new InformerResponseBase();
$res->setUrl($url);

$this->assertSame($url, $res->getUrl());
Expand All @@ -45,7 +45,7 @@ public function testGetSetUrl()
*/
public function testHasResult()
{
$res = new InformerResponseBase;
$res = new InformerResponseBase();

$this->assertFalse($res->hasResult());

Expand All @@ -61,7 +61,7 @@ public function testHasResult()
*/
public function testSetUrlWrongFormatException()
{
$res = new InformerResponseBase;
$res = new InformerResponseBase();

$this->expectException(InvalidArgumentException::class);
$res->setUrl($this->createFakeData()->word);
Expand Down
10 changes: 5 additions & 5 deletions tests/src/FiasInformer/SoapFiasInformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class SoapFiasInformerTest extends BaseCase
*/
public function testGetCompleteInfo()
{
$soapResponse = new stdClass;
$soapResponse->GetLastDownloadFileInfoResult = new stdClass;
$soapResponse = new stdClass();
$soapResponse->GetLastDownloadFileInfoResult = new stdClass();
$soapResponse->GetLastDownloadFileInfoResult->FiasCompleteXmlUrl = $this->createFakeData()->url;
$soapResponse->GetLastDownloadFileInfoResult->VersionId = $this->createFakeData()->randomNumber;

Expand Down Expand Up @@ -50,16 +50,16 @@ public function testGetCompleteInfo()
*/
public function testGetDeltaInfo()
{
$soapResponse = new stdClass;
$soapResponse->GetAllDownloadFileInfoResult = new stdClass;
$soapResponse = new stdClass();
$soapResponse->GetAllDownloadFileInfoResult = new stdClass();
$soapResponse->GetAllDownloadFileInfoResult->DownloadFileInfo = [];

$totalDeltas = 10;
$currentDelta = $this->createFakeData()->numberBetween(1, $totalDeltas - 1);
$nextDelta = $currentDelta + 1;
$nextUrl = null;
for ($i = 1; $i <= $totalDeltas; ++$i) {
$delta = new stdClass;
$delta = new stdClass();
$delta->VersionId = $i;
$delta->FiasDeltaXmlUrl = $this->createFakeData()->url;
$soapResponse->GetAllDownloadFileInfoResult->DownloadFileInfo[] = $delta;
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Pipeline/Pipe/ArrayPipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function testRunException()
$task1 = $this->getMockBuilder(Task::class)->getMock();

$task2 = $this->getMockBuilder(Task::class)->getMock();
$task2->method('run')->will($this->throwException(new InvalidArgumentException));
$task2->method('run')->will($this->throwException(new InvalidArgumentException()));

$pipe = new ArrayPipe([$task1, $task2], $cleanUp);

Expand Down
10 changes: 5 additions & 5 deletions tests/src/Pipeline/State/ArrayStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testSetAndGetParameter()
$parameterName = $this->createFakeData()->word;
$parameterValue = $this->createFakeData()->word;

$state = new ArrayState;
$state = new ArrayState();
$state->setParameter($parameterName, $parameterValue);

$this->assertSame($parameterValue, $state->getParameter($parameterName));
Expand All @@ -35,7 +35,7 @@ public function testSetAndLockParameter()
$parameterName = $this->createFakeData()->word;
$parameterValue = $this->createFakeData()->word;

$state = new ArrayState;
$state = new ArrayState();
$state->setAndLockParameter($parameterName, $parameterValue);

$this->assertSame($parameterValue, $state->getParameter($parameterName));
Expand All @@ -50,7 +50,7 @@ public function testSetParameterLockedException()
$parameterName = $this->createFakeData()->word;
$parameterValue = $this->createFakeData()->word;

$state = new ArrayState;
$state = new ArrayState();
$state->setAndLockParameter($parameterName, $parameterValue);

$this->expectException(InvalidArgumentException::class);
Expand All @@ -62,9 +62,9 @@ public function testSetParameterLockedException()
*/
public function testComplete()
{
$state = new ArrayState;
$state = new ArrayState();

$stateCompleted = new ArrayState;
$stateCompleted = new ArrayState();
$stateCompleted->complete();

$this->assertFalse($state->isCompleted());
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Pipeline/Task/CleanupTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testRun()
return $return;
}));

$task = new CleanupTask;
$task = new CleanupTask();
$task->run($state);

$this->assertFalse(file_exists($downloadToPath), 'Downloaded file removed');
Expand All @@ -60,7 +60,7 @@ public function testRunEmptyFiles()
return $name === Task::DOWNLOAD_TO_FILE_PARAM ? $downloadTo : null;
}));

$task = new CleanupTask;
$task = new CleanupTask();
$task->run($state);

$this->assertFalse(file_exists($downloadToPath), 'Downloaded file removed');
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Pipeline/Task/DataDeleteTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function testRun()
$insertedData[] = $object->getActstatid();
}));

$state = new ArrayState;
$state = new ArrayState();
$state->setParameter(Task::FILES_TO_DELETE_PARAM, [__DIR__ . '/_fixtures/data.xml']);

$task = new DataDeleteTask($entityManager, new BaseXmlReader, $storage, new FiasSerializer);
$task = new DataDeleteTask($entityManager, new BaseXmlReader(), $storage, new FiasSerializer());
$task->run($state);

$this->assertSame([321], $insertedData);
Expand Down
10 changes: 5 additions & 5 deletions tests/src/Pipeline/Task/DataInsertTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public function testRun()
$insertedData[] = $object->getActstatid();
}));

$state = new ArrayState;
$state = new ArrayState();
$state->setParameter(Task::FILES_TO_INSERT_PARAM, [__DIR__ . '/_fixtures/data.xml']);

$task = new DataInsertTask($entityManager, new BaseXmlReader, $storage, new FiasSerializer);
$task = new DataInsertTask($entityManager, new BaseXmlReader(), $storage, new FiasSerializer());
$task->run($state);

$this->assertSame([321], $insertedData);
Expand Down Expand Up @@ -83,13 +83,13 @@ public function testRunDeserializeException()
$insertedData[] = $object->getActstatid();
}));

$state = new ArrayState;
$state = new ArrayState();
$state->setParameter(Task::FILES_TO_INSERT_PARAM, [__DIR__ . '/_fixtures/data.xml']);

$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
$serializer->method('deserialize')->will($this->throwException(new InvalidArgumentException));
$serializer->method('deserialize')->will($this->throwException(new InvalidArgumentException()));

$task = new DataInsertTask($entityManager, new BaseXmlReader, $storage, $serializer);
$task = new DataInsertTask($entityManager, new BaseXmlReader(), $storage, $serializer);

$this->expectException(TaskException::class);
$task->run($state);
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Pipeline/Task/DataUpsertTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function testRun()
$insertedData[] = $object->getActstatid();
}));

$state = new ArrayState;
$state = new ArrayState();
$state->setParameter(Task::FILES_TO_INSERT_PARAM, [__DIR__ . '/_fixtures/data.xml']);

$task = new DataUpsertTask($entityManager, new BaseXmlReader, $storage, new FiasSerializer);
$task = new DataUpsertTask($entityManager, new BaseXmlReader(), $storage, new FiasSerializer());
$task->run($state);

$this->assertSame([321], $insertedData);
Expand Down
69 changes: 47 additions & 22 deletions tests/src/Pipeline/Task/SelectFilesToProceedTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Liquetsoft\Fias\Component\Pipeline\Task\Task;
use Liquetsoft\Fias\Component\Tests\BaseCase;
use SplFileInfo;
use stdClass;

/**
* Тест для задачи, которая выбирает файлы из папки для загрузки в базу на основе данных из EntityManager.
Expand All @@ -24,7 +25,7 @@ class SelectFilesToProceedTaskTest extends BaseCase
public function testRunEmptyUnpackToException()
{
$entityManager = $this->getMockBuilder(EntityManager::class)->getMock();
$state = new ArrayState;
$state = new ArrayState();

$task = new SelectFilesToProceedTask($entityManager);

Expand All @@ -39,7 +40,7 @@ public function testRunNonExitedUnpackToException()
{
$entityManager = $this->getMockBuilder(EntityManager::class)->getMock();

$state = new ArrayState;
$state = new ArrayState();
$state->setParameter(
Task::EXTRACT_TO_FOLDER_PARAM,
new SplFileInfo(__DIR__ . '/test')
Expand All @@ -61,36 +62,60 @@ public function testRun()
$descriptor = $this->getMockBuilder(EntityDescriptor::class)->getMock();

$entityManager = $this->getMockBuilder(EntityManager::class)->getMock();
$entityManager->method('getDescriptorByInsertFile')->will($this->returnCallback(function ($file) use ($descriptor) {
return $file === 'SelectFilesToProceedTaskTest_insert.xml' ? $descriptor : null;
}));
$entityManager->method('getDescriptorByDeleteFile')->will($this->returnCallback(function ($file) use ($descriptor) {
return $file === 'SelectFilesToProceedTaskTest_delete.xml' ? $descriptor : null;
}));
$entityManager->method('getClassByDescriptor')->will($this->returnCallback(function ($testDescriptor) use ($descriptor) {
return $testDescriptor === $descriptor ? SelectFilesToProceedTaskObject::class : null;
}));

$state = new ArrayState;
$entityManager->method('getDescriptorByInsertFile')
->will(
$this->returnCallback(
function ($file) use ($descriptor) {
$files = [
'SelectFilesToProceedTaskTest_insert.xml',
'SelectFilesToProceedTaskTest_nested_insert.xml',
];

return in_array($file, $files, true) ? $descriptor : null;
}
)
);
$entityManager->method('getDescriptorByDeleteFile')
->will(
$this->returnCallback(
function ($file) use ($descriptor) {
$files = [
'SelectFilesToProceedTaskTest_delete.xml',
'SelectFilesToProceedTaskTest_nested_delete.xml',
];

return in_array($file, $files, true) ? $descriptor : null;
}
)
);
$entityManager->method('getClassByDescriptor')
->will(
$this->returnCallback(
function ($testDescriptor) use ($descriptor) {
return $testDescriptor === $descriptor ? stdClass::class : null;
}
)
);

$state = new ArrayState();
$state->setParameter(Task::EXTRACT_TO_FOLDER_PARAM, new SplFileInfo($fixturesFolder));

$task = new SelectFilesToProceedTask($entityManager);
$task->run($state);

$this->assertSame(
[$fixturesFolder . '/SelectFilesToProceedTaskTest_insert.xml'],
[
$fixturesFolder . '/SelectFilesToProceedTaskTest_insert.xml',
$fixturesFolder . '/nested/SelectFilesToProceedTaskTest_nested_insert.xml',
],
$state->getParameter(Task::FILES_TO_INSERT_PARAM)
);
$this->assertSame(
[$fixturesFolder . '/SelectFilesToProceedTaskTest_delete.xml'],
[
$fixturesFolder . '/SelectFilesToProceedTaskTest_delete.xml',
$fixturesFolder . '/nested/SelectFilesToProceedTaskTest_nested_delete.xml',
],
$state->getParameter(Task::FILES_TO_DELETE_PARAM)
);
}
}

/**
* Мок для проверки задачи, которая выбирает файлы для обработки.
*/
class SelectFilesToProceedTaskObject
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><ActualStatuses></ActualStatuses>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><ActualStatuses></ActualStatuses>
Loading

0 comments on commit e981497

Please sign in to comment.