From 88c9286e368debcc947244ad9a946ca60111c6d5 Mon Sep 17 00:00:00 2001 From: Nemanja Ognjanovic Date: Wed, 23 Jul 2014 08:10:21 +0200 Subject: [PATCH] trying to be compatible with new changes in phpunit mocking of native File objects, refs #28 --- apps/backend/Module.php | 2 +- apps/backend/models/Package.php | 28 ++++----- .../controllers/PackageControllerTest.php | 43 +++++++------ composer.json | 1 + composer.lock | 61 +++++++++++++++++-- 5 files changed, 90 insertions(+), 45 deletions(-) diff --git a/apps/backend/Module.php b/apps/backend/Module.php index 3c6bb49..0c54ae0 100644 --- a/apps/backend/Module.php +++ b/apps/backend/Module.php @@ -26,7 +26,7 @@ public function registerAutoloaders() 'Robinson\Backend\Validator' => __DIR__ . '/validators/', 'Robinson\Backend\Tag' => __DIR__ . '/tags/', 'Robinson\Backend\Filter' => __DIR__ . '/filters/', - 'Robinson\Frontend\Filter' => __DIR__ . '/filters/', + 'Robinson\Frontend\Filter' => __DIR__ . '/../frontend/filters/', ) ); diff --git a/apps/backend/models/Package.php b/apps/backend/models/Package.php index fa1c656..2d232f3 100644 --- a/apps/backend/models/Package.php +++ b/apps/backend/models/Package.php @@ -653,30 +653,24 @@ public function updateTags(array $tagsData) */ protected function removeObsoleteLeftovers() { - $dir = $this->getDI()->get( - 'DirectoryIterator', - array( - $this->getDI()->get('config')->application->packagePdfPath . '/' . $this->getPackageId() - ) - ); - while ($dir->valid()) { - /** @var \DirectoryIterator $file */ - $file = $dir->current(); - - if ($file->isDot()) { - $dir->next(); - continue; - } - + /** @var \Symfony\Component\Finder\Finder $finder */ + $finder = $this->getDI()->get('Symfony\Component\Finder\Finder'); + $finder->files()->in($this->getDI()->get('config')->application->packagePdfPath . '/' . $this->getPackageId()); + $finder->ignoreDotFiles(true); + $iterator = $finder->getIterator(); + while ($iterator->valid()) { + /** @var \Symfony\Component\Finder\SplFileInfo $file */ + $file = $iterator->current(); // original pdf file? if ($file->getFilename() === $this->getPdf()) { - $dir->next(); + $iterator->next(); continue; } $filesystem = $this->getDI()->get('Symfony\Component\Filesystem\Filesystem'); $filesystem->remove($file->getPathname()); - $dir->next(); + $iterator->next(); + continue; } } } diff --git a/apps/backend/tests/controllers/PackageControllerTest.php b/apps/backend/tests/controllers/PackageControllerTest.php index 89204a3..1f2cb16 100644 --- a/apps/backend/tests/controllers/PackageControllerTest.php +++ b/apps/backend/tests/controllers/PackageControllerTest.php @@ -488,38 +488,37 @@ public function testUpdatePackageWithUpdatedPdfShouldWorkAsExpectedAndClearObsol $this->getDI()->set('Imagick', $this->mockWorkingImagick()); - // mock directory iterator - $dirIterator = $this->getMockBuilder('DirectoryIterator') + $file = $this->getMockBuilder('Symfony\Component\Finder\SplFileInfo') ->disableOriginalConstructor() - ->setMethods(array('valid', 'current')) - ->getMock(); - - $fileIterator = $this->getMockBuilder('DirectoryIterator') - ->disableOriginalConstructor() - ->setMethods(array('isDot', 'getPathname', 'getFilename')) + ->setMethods(array('getPathname', 'getFilename')) ->getMock(); - $fileIterator->expects($this->any()) - ->method('isDot') - ->will($this->returnValue(false)); - - // 1 times, on remove - $fileIterator->expects($this->exactly(1)) + $file->expects($this->exactly(1)) ->method('getPathname') ->will($this->onConsecutiveCalls('packagepdftest.pdf', 'packagepdftest.html')); - - - $fileIterator->expects($this->exactly(2)) + $file->expects($this->exactly(2)) ->method('getFilename') ->will($this->onConsecutiveCalls('packagepdftest.pdf', 'packagepdftest.html')); - $dirIterator->expects($this->any()) + $iterator = $this->getMockBuilder('Symfony\Component\Finder\Iterator\PathFilterIterator') + ->setMethods(array('valid', 'current')) + ->setConstructorArgs(array(new \ArrayIterator(array()), array(), array())) + ->getMock(); + $iterator->expects($this->any()) + ->method('current') + ->will($this->returnValue($file)); + $iterator->expects($this->any()) ->method('valid') ->will($this->onConsecutiveCalls(true, true, false)); - $dirIterator->expects($this->any()) - ->method('current') - ->will($this->returnValue($fileIterator)); - $this->getDI()->set('DirectoryIterator', $dirIterator); + + $finder = $this->getMockBuilder('Symfony\Component\Finder\Finder') + ->setMethods(array('getIterator')) + ->getMock(); + $finder->expects($this->once()) + ->method('getIterator') + ->will($this->returnValue($iterator)); + + $this->getDI()->set('Symfony\Component\Finder\Finder', $finder); // mock filesystem component $filesystem = $this->getMockBuilder('Symfony\Component\Filesystem\Filesystem') diff --git a/composer.json b/composer.json index d58451f..599478b 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "ext-imagick": "*", "phalcon/incubator": "1.3.0.x-dev#7e5660c293a3a11dea5ed594f5d73d996117d5ff", "symfony/filesystem": "2.5.*@dev", + "symfony/finder": "2.5.*@dev", "judev/php-htmltruncator": "1.0.0", "zendframework/zend-resources": "2.2.5", "zendframework/zend-mail": "2.2.5", diff --git a/composer.lock b/composer.lock index f211416..14aad82 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "b1dffdcdb7f6949f88c4968b4279c631", + "hash": "322ff74afe3a869f0c0ba0730873418b", "packages": [ { "name": "electrolinux/php-html5lib", @@ -254,6 +254,55 @@ "homepage": "http://symfony.com", "time": "2014-07-09 09:05:48" }, + { + "name": "symfony/finder", + "version": "2.5.x-dev", + "target-dir": "Symfony/Component/Finder", + "source": { + "type": "git", + "url": "https://github.com/symfony/Finder.git", + "reference": "576d8f69feec477067e91b6bd0367c113e76a1a0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Finder/zipball/576d8f69feec477067e91b6bd0367c113e76a1a0", + "reference": "576d8f69feec477067e91b6bd0367c113e76a1a0", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.5-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\Finder\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + }, + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + } + ], + "description": "Symfony Finder Component", + "homepage": "http://symfony.com", + "time": "2014-07-15 14:15:12" + }, { "name": "zendframework/zendframework", "version": "2.2.5", @@ -528,12 +577,12 @@ "source": { "type": "git", "url": "https://github.com/mikey179/vfsStream.git", - "reference": "d1eb2debe1d1036179458b8fce1c61dd2bcdf0fe" + "reference": "c00e8433f907fb32162c782a92e648df5db07a59" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mikey179/vfsStream/zipball/d1eb2debe1d1036179458b8fce1c61dd2bcdf0fe", - "reference": "d1eb2debe1d1036179458b8fce1c61dd2bcdf0fe", + "url": "https://api.github.com/repos/mikey179/vfsStream/zipball/c00e8433f907fb32162c782a92e648df5db07a59", + "reference": "c00e8433f907fb32162c782a92e648df5db07a59", "shasum": "" }, "require": { @@ -558,7 +607,7 @@ "BSD" ], "homepage": "http://vfs.bovigo.org/", - "time": "2014-06-04 18:19:53" + "time": "2014-07-21 20:34:35" }, { "name": "pdepend/pdepend", @@ -1398,10 +1447,12 @@ "imagine/imagine": 20, "phalcon/incubator": 20, "symfony/filesystem": 20, + "symfony/finder": 20, "mikey179/vfsstream": 20, "squizlabs/php_codesniffer": 20, "robmorgan/phinx": 20 }, + "prefer-stable": false, "platform": { "ext-phalcon": ">=1.3,<2", "ext-intl": "*",