diff --git a/.travis.yml b/.travis.yml index 73f4c148..1ef118c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,6 @@ matrix: - php: 7 - php: hhvm allow_failures: - - php: 7 - php: hhvm notifications: diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..59b683d8 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,22 @@ +# Changelog + +All notable changes to this project will be documented in this file, in reverse chronological order by release. + +## 3.0.0 - TBD + +### Added + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- [#15](https://github.com/zendframework/zend-filter/pull/15) updates the + component to use the v3 versions of zend-servicemanager and zend-crypt. diff --git a/composer.json b/composer.json index e6a56987..76fa14f4 100644 --- a/composer.json +++ b/composer.json @@ -18,10 +18,10 @@ }, "require-dev": { "zendframework/zend-config": "~2.5", - "zendframework/zend-crypt": "~2.5", + "zendframework/zend-crypt": "dev-develop as 2.7", "zendframework/zend-i18n": "~2.5", "zendframework/zend-loader": "~2.5", - "zendframework/zend-servicemanager": "~2.5", + "zendframework/zend-servicemanager": "dev-develop as 2.7", "zendframework/zend-uri": "~2.5", "fabpot/php-cs-fixer": "1.7.*", "phpunit/PHPUnit": "~4.0" diff --git a/src/FilterChain.php b/src/FilterChain.php index 293c7809..1358b6d2 100644 --- a/src/FilterChain.php +++ b/src/FilterChain.php @@ -11,6 +11,7 @@ use Countable; use Traversable; +use Zend\ServiceManager\ServiceManager; use Zend\Stdlib\PriorityQueue; class FilterChain extends AbstractFilter implements Countable @@ -108,7 +109,7 @@ public function count() public function getPluginManager() { if (!$this->plugins) { - $this->setPluginManager(new FilterPluginManager()); + $this->setPluginManager(new FilterPluginManager(new ServiceManager())); } return $this->plugins; } diff --git a/src/FilterPluginManager.php b/src/FilterPluginManager.php index 4644e062..f7a089b0 100644 --- a/src/FilterPluginManager.php +++ b/src/FilterPluginManager.php @@ -9,7 +9,13 @@ namespace Zend\Filter; +use Zend\I18n\Filter\Alnum; +use Zend\I18n\Filter\Alpha; +use Zend\I18n\Filter\NumberParse; +use Zend\I18n\View\Helper\NumberFormat; use Zend\ServiceManager\AbstractPluginManager; +use Zend\ServiceManager\Exception\InvalidServiceException; +use Zend\ServiceManager\Factory\InvokableFactory; /** * Plugin manager implementation for the filter chain. @@ -21,8 +27,189 @@ class FilterPluginManager extends AbstractPluginManager { protected $aliases = [ - 'Zend\Filter\Int' => 'Zend\Filter\ToInt', - 'Zend\Filter\Null' => 'Zend\Filter\ToNull', + + // For the future + 'Int' => ToInt::class, + 'Null' => ToNull::class, + + // I18n filters + 'alnum' => Alnum::class, + 'Alnum' => Alnum::class, + 'alpha' => Alpha::class, + 'Alpha' => Alpha::class, + 'numberformat' => NumberFormat::class, + 'numberFormat' => NumberFormat::class, + 'NumberFormat' => NumberFormat::class, + 'numberparse' => NumberParse::class, + 'numberParse' => NumberParse::class, + 'NumberParse' => NumberParse::class, + + // Standard filters + 'basename' => BaseName::class, + 'Basename' => BaseName::class, + 'blacklist' => Blacklist::class, + 'Blacklist' => Blacklist::class, + 'boolean' => Boolean::class, + 'Boolean' => Boolean::class, + 'callback' => Callback::class, + 'Callback' => Callback::class, + 'compress' => Compress::class, + 'Compress' => Compress::class, + 'compressbz2' => Compress\Bz2::class, + 'compressBz2' => Compress\Bz2::class, + 'CompressBz2' => Compress\Bz2::class, + 'compressgz' => Compress\Gz::class, + 'compressGz' => Compress\Gz::class, + 'CompressGz' => Compress\Gz::class, + 'compresslzf' => Compress\Lzf::class, + 'compressLzf' => Compress\Lzf::class, + 'CompressLzf' => Compress\Lzf::class, + 'compressrar' => Compress\Rar::class, + 'compressRar' => Compress\Rar::class, + 'CompressRar' => Compress\Rar::class, + 'compresssnappy' => Compress\Snappy::class, + 'compressSnappy' => Compress\Snappy::class, + 'CompressSnappy' => Compress\Snappy::class, + 'compresstar' => Compress\Tar::class, + 'compressTar' => Compress\Tar::class, + 'CompressTar' => Compress\Tar::class, + 'compresszip' => Compress\Zip::class, + 'compressZip' => Compress\Zip::class, + 'CompressZip' => Compress\Zip::class, + 'dataunitformatter' => DataUnitFormatter::class, + 'dataUnitFormatter' => DataUnitFormatter::class, + 'DataUnitFormatter' => DataUnitFormatter::class, + 'dateselect' => DateSelect::class, + 'dateSelect' => DateSelect::class, + 'DateSelect' => DateSelect::class, + 'datetimeformatter' => DateTimeFormatter::class, + 'datetimeFormatter' => DateTimeFormatter::class, + 'DatetimeFormatter' => DateTimeFormatter::class, + 'dateTimeFormatter' => DateTimeFormatter::class, + 'DateTimeFormatter' => DateTimeFormatter::class, + 'datetimeselect' => DateTimeSelect::class, + 'datetimeSelect' => DateTimeSelect::class, + 'DatetimeSelect' => DateTimeSelect::class, + 'dateTimeSelect' => DateTimeSelect::class, + 'DateTimeSelect' => DateTimeSelect::class, + 'decompress' => Decompress::class, + 'Decompress' => Decompress::class, + 'decrypt' => Decrypt::class, + 'Decrypt' => Decrypt::class, + 'digits' => Digits::class, + 'Digits' => Digits::class, + 'dir' => Dir::class, + 'Dir' => Dir::class, + 'encrypt' => Encrypt::class, + 'Encrypt' => Encrypt::class, + 'encryptblockcipher' => Encrypt\BlockCipher::class, + 'encryptBlockCipher' => Encrypt\BlockCipher::class, + 'EncryptBlockCipher' => Encrypt\BlockCipher::class, + 'encryptopenssl' => Encrypt\Openssl::class, + 'encryptOpenssl' => Encrypt\Openssl::class, + 'EncryptOpenssl' => Encrypt\Openssl::class, + 'filedecrypt' => File\Decrypt::class, + 'fileDecrypt' => File\Decrypt::class, + 'FileDecrypt' => File\Decrypt::class, + 'fileencrypt' => File\Encrypt::class, + 'fileEncrypt' => File\Encrypt::class, + 'FileEncrypt' => File\Encrypt::class, + 'filelowercase' => File\LowerCase::class, + 'fileLowercase' => File\LowerCase::class, + 'FileLowercase' => File\LowerCase::class, + 'fileLowerCase' => File\LowerCase::class, + 'FileLowerCase' => File\LowerCase::class, + 'filerename' => File\Rename::class, + 'fileRename' => File\Rename::class, + 'FileRename' => File\Rename::class, + 'filerenameupload' => File\RenameUpload::class, + 'fileRenameUpload' => File\RenameUpload::class, + 'FileRenameUpload' => File\RenameUpload::class, + 'fileuppercase' => File\UpperCase::class, + 'fileUppercase' => File\UpperCase::class, + 'FileUppercase' => File\UpperCase::class, + 'fileUpperCase' => File\UpperCase::class, + 'FileUpperCase' => File\UpperCase::class, + 'htmlentities' => HtmlEntities::class, + 'htmlEntities' => HtmlEntities::class, + 'HtmlEntities' => HtmlEntities::class, + 'inflector' => Inflector::class, + 'Inflector' => Inflector::class, + 'int' => ToInt::class, + 'Int' => ToInt::class, + 'monthselect' => MonthSelect::class, + 'monthSelect' => MonthSelect::class, + 'MonthSelect' => MonthSelect::class, + 'null' => ToNull::class, + 'Null' => ToNull::class, + 'pregreplace' => PregReplace::class, + 'pregReplace' => PregReplace::class, + 'PregReplace' => PregReplace::class, + 'realpath' => RealPath::class, + 'realPath' => RealPath::class, + 'RealPath' => RealPath::class, + 'stringtolower' => StringToLower::class, + 'stringToLower' => StringToLower::class, + 'StringToLower' => StringToLower::class, + 'stringtoupper' => StringToUpper::class, + 'stringToUpper' => StringToUpper::class, + 'StringToUpper' => StringToUpper::class, + 'stringtrim' => StringTrim::class, + 'stringTrim' => StringTrim::class, + 'StringTrim' => StringTrim::class, + 'stripnewlines' => StripNewlines::class, + 'stripNewlines' => StripNewlines::class, + 'StripNewlines' => StripNewlines::class, + 'striptags' => StripTags::class, + 'stripTags' => StripTags::class, + 'StripTags' => StripTags::class, + 'toint' => ToInt::class, + 'toInt' => ToInt::class, + 'ToInt' => ToInt::class, + 'tonull' => ToNull::class, + 'toNull' => ToNull::class, + 'ToNull' => ToNull::class, + 'urinormalize' => UriNormalize::class, + 'uriNormalize' => UriNormalize::class, + 'UriNormalize' => UriNormalize::class, + 'whitelist' => Whitelist::class, + 'Whitelist' => Whitelist::class, + 'wordcamelcasetodash' => Word\CamelCaseToDash::class, + 'wordCamelCaseToDash' => Word\CamelCaseToDash::class, + 'WordCamelCaseToDash' => Word\CamelCaseToDash::class, + 'wordcamelcasetoseparator' => Word\CamelCaseToSeparator::class, + 'wordCamelCaseToSeparator' => Word\CamelCaseToSeparator::class, + 'WordCamelCaseToSeparator' => Word\CamelCaseToSeparator::class, + 'wordcamelcasetounderscore' => Word\CamelCaseToUnderscore::class, + 'wordCamelCaseToUnderscore' => Word\CamelCaseToUnderscore::class, + 'WordCamelCaseToUnderscore' => Word\CamelCaseToUnderscore::class, + 'worddashtocamelcase' => Word\DashToCamelCase::class, + 'wordDashToCamelCase' => Word\DashToCamelCase::class, + 'WordDashToCamelCase' => Word\DashToCamelCase::class, + 'worddashtoseparator' => Word\DashToSeparator::class, + 'wordDashToSeparator' => Word\DashToSeparator::class, + 'WordDashToSeparator' => Word\DashToSeparator::class, + 'worddashtounderscore' => Word\DashToUnderscore::class, + 'wordDashToUnderscore' => Word\DashToUnderscore::class, + 'WordDashToUnderscore' => Word\DashToUnderscore::class, + 'wordseparatortocamelcase' => Word\SeparatorToCamelCase::class, + 'wordSeparatorToCamelCase' => Word\SeparatorToCamelCase::class, + 'WordSeparatorToCamelCase' => Word\SeparatorToCamelCase::class, + 'wordseparatortodash' => Word\SeparatorToDash::class, + 'wordSeparatorToDash' => Word\SeparatorToDash::class, + 'WordSeparatorToDash' => Word\SeparatorToDash::class, + 'wordunderscoretocamelcase' => Word\UnderscoreToCamelCase::class, + 'wordUnderscoreToCamelCase' => Word\UnderscoreToCamelCase::class, + 'WordUnderscoreToCamelCase' => Word\UnderscoreToCamelCase::class, + 'wordunderscoretostudlycase' => Word\UnderscoreToStudlyCase::class, + 'wordUnderscoreToStudlyCase' => Word\UnderscoreToStudlyCase::class, + 'WordUnderscoreToStudlyCase' => Word\UnderscoreToStudlyCase::class, + 'wordunderscoretodash' => Word\UnderscoreToDash::class, + 'wordUnderscoreToDash' => Word\UnderscoreToDash::class, + 'WordUnderscoreToDash' => Word\UnderscoreToDash::class, + 'wordunderscoretoseparator' => Word\UnderscoreToSeparator::class, + 'wordUnderscoreToSeparator' => Word\UnderscoreToSeparator::class, + 'WordUnderscoreToSeparator' => Word\UnderscoreToSeparator::class, ]; /** @@ -31,107 +218,94 @@ class FilterPluginManager extends AbstractPluginManager * @var array */ protected $factories = [ - 'wordseparatortoseparator' => 'Zend\Filter\Word\Service\SeparatorToSeparatorFactory', - ]; + 'wordseparatortoseparator' => Word\Service\SeparatorToSeparatorFactory::class, - /** - * Default set of filters - * - * @var array - */ - protected $invokableClasses = [ - 'alnum' => 'Zend\I18n\Filter\Alnum', - 'alpha' => 'Zend\I18n\Filter\Alpha', - 'basename' => 'Zend\Filter\BaseName', - 'blacklist' => 'Zend\Filter\Blacklist', - 'boolean' => 'Zend\Filter\Boolean', - 'callback' => 'Zend\Filter\Callback', - 'compress' => 'Zend\Filter\Compress', - 'compressbz2' => 'Zend\Filter\Compress\Bz2', - 'compressgz' => 'Zend\Filter\Compress\Gz', - 'compresslzf' => 'Zend\Filter\Compress\Lzf', - 'compressrar' => 'Zend\Filter\Compress\Rar', - 'compresssnappy' => 'Zend\Filter\Compress\Snappy', - 'compresstar' => 'Zend\Filter\Compress\Tar', - 'compresszip' => 'Zend\Filter\Compress\Zip', - 'dataunitformatter' => 'Zend\Filter\DataUnitFormatter', - 'dateselect' => 'Zend\Filter\DateSelect', - 'datetimeformatter' => 'Zend\Filter\DateTimeFormatter', - 'datetimeselect' => 'Zend\Filter\DateTimeSelect', - 'decompress' => 'Zend\Filter\Decompress', - 'decrypt' => 'Zend\Filter\Decrypt', - 'digits' => 'Zend\Filter\Digits', - 'dir' => 'Zend\Filter\Dir', - 'encrypt' => 'Zend\Filter\Encrypt', - 'encryptblockcipher' => 'Zend\Filter\Encrypt\BlockCipher', - 'encryptopenssl' => 'Zend\Filter\Encrypt\Openssl', - 'filedecrypt' => 'Zend\Filter\File\Decrypt', - 'fileencrypt' => 'Zend\Filter\File\Encrypt', - 'filelowercase' => 'Zend\Filter\File\LowerCase', - 'filerename' => 'Zend\Filter\File\Rename', - 'filerenameupload' => 'Zend\Filter\File\RenameUpload', - 'fileuppercase' => 'Zend\Filter\File\UpperCase', - 'htmlentities' => 'Zend\Filter\HtmlEntities', - 'inflector' => 'Zend\Filter\Inflector', - 'int' => 'Zend\Filter\ToInt', - 'monthselect' => 'Zend\Filter\MonthSelect', - 'null' => 'Zend\Filter\ToNull', - 'numberformat' => 'Zend\I18n\Filter\NumberFormat', - 'numberparse' => 'Zend\I18n\Filter\NumberParse', - 'pregreplace' => 'Zend\Filter\PregReplace', - 'realpath' => 'Zend\Filter\RealPath', - 'stringtolower' => 'Zend\Filter\StringToLower', - 'stringtoupper' => 'Zend\Filter\StringToUpper', - 'stringtrim' => 'Zend\Filter\StringTrim', - 'stripnewlines' => 'Zend\Filter\StripNewlines', - 'striptags' => 'Zend\Filter\StripTags', - 'toint' => 'Zend\Filter\ToInt', - 'tonull' => 'Zend\Filter\ToNull', - 'urinormalize' => 'Zend\Filter\UriNormalize', - 'whitelist' => 'Zend\Filter\Whitelist', - 'wordcamelcasetodash' => 'Zend\Filter\Word\CamelCaseToDash', - 'wordcamelcasetoseparator' => 'Zend\Filter\Word\CamelCaseToSeparator', - 'wordcamelcasetounderscore' => 'Zend\Filter\Word\CamelCaseToUnderscore', - 'worddashtocamelcase' => 'Zend\Filter\Word\DashToCamelCase', - 'worddashtoseparator' => 'Zend\Filter\Word\DashToSeparator', - 'worddashtounderscore' => 'Zend\Filter\Word\DashToUnderscore', - 'wordseparatortocamelcase' => 'Zend\Filter\Word\SeparatorToCamelCase', - 'wordseparatortodash' => 'Zend\Filter\Word\SeparatorToDash', - 'wordunderscoretocamelcase' => 'Zend\Filter\Word\UnderscoreToCamelCase', - 'wordunderscoretostudlycase' => 'Zend\Filter\Word\UnderscoreToStudlyCase', - 'wordunderscoretodash' => 'Zend\Filter\Word\UnderscoreToDash', - 'wordunderscoretoseparator' => 'Zend\Filter\Word\UnderscoreToSeparator', - ]; + // For the future + ToInt::class => InvokableFactory::class, + ToNull::class => InvokableFactory::class, - /** - * Whether or not to share by default; default to false - * - * @var bool - */ - protected $shareByDefault = false; + // I18n filters + Alnum::class => InvokableFactory::class, + Alpha::class => InvokableFactory::class, + NumberFormat::class => InvokableFactory::class, + NumberParse::class => InvokableFactory::class, + + // Standard filters + BaseName::class => InvokableFactory::class, + Blacklist::class => InvokableFactory::class, + Boolean::class => InvokableFactory::class, + Callback::class => InvokableFactory::class, + Compress::class => InvokableFactory::class, + Compress\Bz2::class => InvokableFactory::class, + Compress\Gz::class => InvokableFactory::class, + Compress\Lzf::class => InvokableFactory::class, + Compress\Rar::class => InvokableFactory::class, + Compress\Snappy::class => InvokableFactory::class, + Compress\Tar::class => InvokableFactory::class, + Compress\Zip::class => InvokableFactory::class, + DataUnitFormatter::class => InvokableFactory::class, + DateSelect::class => InvokableFactory::class, + DateTimeFormatter::class => InvokableFactory::class, + DateTimeSelect::class => InvokableFactory::class, + Decompress::class => InvokableFactory::class, + Decrypt::class => InvokableFactory::class, + Digits::class => InvokableFactory::class, + Dir::class => InvokableFactory::class, + Encrypt::class => InvokableFactory::class, + Encrypt\BlockCipher::class => InvokableFactory::class, + Encrypt\Openssl::class => InvokableFactory::class, + File\Decrypt::class => InvokableFactory::class, + File\Encrypt::class => InvokableFactory::class, + File\LowerCase::class => InvokableFactory::class, + File\Rename::class => InvokableFactory::class, + File\RenameUpload::class => InvokableFactory::class, + File\UpperCase::class => InvokableFactory::class, + HtmlEntities::class => InvokableFactory::class, + Inflector::class => InvokableFactory::class, + ToInt::class => InvokableFactory::class, + MonthSelect::class => InvokableFactory::class, + ToNull::class => InvokableFactory::class, + PregReplace::class => InvokableFactory::class, + RealPath::class => InvokableFactory::class, + StringToLower::class => InvokableFactory::class, + StringToUpper::class => InvokableFactory::class, + StringTrim::class => InvokableFactory::class, + StripNewlines::class => InvokableFactory::class, + StripTags::class => InvokableFactory::class, + ToInt::class => InvokableFactory::class, + ToNull::class => InvokableFactory::class, + UriNormalize::class => InvokableFactory::class, + Whitelist::class => InvokableFactory::class, + Word\CamelCaseToDash::class => InvokableFactory::class, + Word\CamelCaseToSeparator::class => InvokableFactory::class, + Word\CamelCaseToUnderscore::class => InvokableFactory::class, + Word\DashToCamelCase::class => InvokableFactory::class, + Word\DashToSeparator::class => InvokableFactory::class, + Word\DashToUnderscore::class => InvokableFactory::class, + Word\SeparatorToCamelCase::class => InvokableFactory::class, + Word\SeparatorToDash::class => InvokableFactory::class, + Word\UnderscoreToCamelCase::class => InvokableFactory::class, + Word\UnderscoreToStudlyCase::class => InvokableFactory::class, + Word\UnderscoreToDash::class => InvokableFactory::class, + Word\UnderscoreToSeparator::class => InvokableFactory::class, + ]; /** - * Validate the plugin - * - * Checks that the filter loaded is either a valid callback or an instance - * of FilterInterface. - * - * @param mixed $plugin - * @return void - * @throws Exception\RuntimeException if invalid + * {@inheritdoc} */ - public function validatePlugin($plugin) + public function validate($plugin) { if ($plugin instanceof FilterInterface) { // we're okay return; } + if (is_callable($plugin)) { // also okay return; } - throw new Exception\RuntimeException(sprintf( + throw new InvalidServiceException(sprintf( 'Plugin of type %s is invalid; must implement %s\FilterInterface or be callable', (is_object($plugin) ? get_class($plugin) : gettype($plugin)), __NAMESPACE__ diff --git a/src/Inflector.php b/src/Inflector.php index 6cba8317..1af7d118 100644 --- a/src/Inflector.php +++ b/src/Inflector.php @@ -10,6 +10,7 @@ namespace Zend\Filter; use Traversable; +use Zend\ServiceManager\ServiceManager; use Zend\Stdlib\ArrayUtils; /** @@ -86,7 +87,7 @@ public function __construct($options = null) public function getPluginManager() { if (!$this->pluginManager instanceof FilterPluginManager) { - $this->setPluginManager(new FilterPluginManager()); + $this->setPluginManager(new FilterPluginManager(new ServiceManager())); } return $this->pluginManager; diff --git a/src/StaticFilter.php b/src/StaticFilter.php index aafe3430..3b6fa574 100644 --- a/src/StaticFilter.php +++ b/src/StaticFilter.php @@ -9,6 +9,8 @@ namespace Zend\Filter; +use Zend\ServiceManager\ServiceManager; + class StaticFilter { /** @@ -24,10 +26,6 @@ class StaticFilter */ public static function setPluginManager(FilterPluginManager $manager = null) { - // Don't share by default to allow different arguments on subsequent calls - if ($manager instanceof FilterPluginManager) { - $manager->setShareByDefault(false); - } static::$plugins = $manager; } @@ -39,8 +37,9 @@ public static function setPluginManager(FilterPluginManager $manager = null) public static function getPluginManager() { if (null === static::$plugins) { - static::setPluginManager(new FilterPluginManager()); + static::setPluginManager(new FilterPluginManager(new ServiceManager())); } + return static::$plugins; } diff --git a/src/Word/Service/SeparatorToSeparatorFactory.php b/src/Word/Service/SeparatorToSeparatorFactory.php index 9d3c7220..0c4d4d84 100644 --- a/src/Word/Service/SeparatorToSeparatorFactory.php +++ b/src/Word/Service/SeparatorToSeparatorFactory.php @@ -2,52 +2,20 @@ namespace Zend\Filter\Word\Service; +use Interop\Container\ContainerInterface; use Zend\Filter\Word\SeparatorToSeparator; -use Zend\ServiceManager\FactoryInterface; -use Zend\ServiceManager\MutableCreationOptionsInterface; -use Zend\ServiceManager\ServiceLocatorInterface; +use Zend\ServiceManager\Factory\FactoryInterface; -class SeparatorToSeparatorFactory implements - FactoryInterface, - MutableCreationOptionsInterface +class SeparatorToSeparatorFactory implements FactoryInterface { - /** - * @var array - */ - protected $creationOptions = []; - - /** - * Set creation options - * - * @param array $creationOptions - * @return void - */ - public function setCreationOptions(array $creationOptions) - { - $this->creationOptions = $creationOptions; - } - - /** - * Get creation options - * - * @return array - */ - public function getCreationOptions() - { - return $this->creationOptions; - } - /** * {@inheritDoc} - * - * @return SeparatorToSeparator - * @throws ServiceNotCreatedException if Controllermanager service is not found in application service locator */ - public function createService(ServiceLocatorInterface $plugins) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { return new SeparatorToSeparator( - isset($this->creationOptions['search_separator']) ? $this->creationOptions['search_separator'] : ' ', - isset($this->creationOptions['replacement_separator']) ? $this->creationOptions['replacement_separator'] : '-' + isset($options['search_separator']) ? $options['search_separator'] : ' ', + isset($options['replacement_separator']) ? $options['replacement_separator'] : '-' ); } } diff --git a/test/BlacklistTest.php b/test/BlacklistTest.php index aeed84db..8da85af3 100644 --- a/test/BlacklistTest.php +++ b/test/BlacklistTest.php @@ -11,6 +11,7 @@ use Zend\Filter\FilterPluginManager; use Zend\Filter\Blacklist as BlacklistFilter; +use Zend\ServiceManager\ServiceManager; use Zend\Stdlib\ArrayObject; /** @@ -39,7 +40,7 @@ public function testConstructorDefaults() public function testWithPluginManager() { - $pluginManager = new FilterPluginManager(); + $pluginManager = new FilterPluginManager(new ServiceManager()); $filter = $pluginManager->get('blacklist'); $this->assertInstanceOf('Zend\Filter\Blacklist', $filter); diff --git a/test/Compress/Bz2Test.php b/test/Compress/Bz2Test.php index 442e1ff6..7baa84c1 100644 --- a/test/Compress/Bz2Test.php +++ b/test/Compress/Bz2Test.php @@ -16,17 +16,21 @@ */ class Bz2Test extends \PHPUnit_Framework_TestCase { + public $target; + public function setUp() { if (!extension_loaded('bz2')) { $this->markTestSkipped('This adapter needs the bz2 extension'); } + + $this->target = sprintf('%s/%s.bz2', sys_get_temp_dir(), uniqid('zfilter')); } public function tearDown() { - if (file_exists(__DIR__ . '/../_files/compressed.bz2')) { - unlink(__DIR__ . '/../_files/compressed.bz2'); + if (file_exists($this->target)) { + unlink($this->target); } } @@ -119,7 +123,7 @@ public function testBz2GetSetArchive() public function testBz2CompressToFile() { $filter = new Bz2Compression(); - $archive = __DIR__ . '/../_files/compressed.bz2'; + $archive = $this->target; $filter->setArchive($archive); $content = $filter->compress('compress me'); @@ -154,7 +158,7 @@ public function testBz2ToString() public function testBz2DecompressArchive() { $filter = new Bz2Compression(); - $archive = __DIR__ . '/../_files/compressed.bz2'; + $archive = $this->target; $filter->setArchive($archive); $content = $filter->compress('compress me'); diff --git a/test/Compress/GzTest.php b/test/Compress/GzTest.php index 8c1b1067..7b097626 100644 --- a/test/Compress/GzTest.php +++ b/test/Compress/GzTest.php @@ -16,17 +16,21 @@ */ class GzTest extends \PHPUnit_Framework_TestCase { + public $target; + public function setUp() { if (!extension_loaded('zlib')) { $this->markTestSkipped('This adapter needs the zlib extension'); } + + $this->target = sprintf('%s/%s.gz', sys_get_temp_dir(), uniqid('zfilter')); } public function tearDown() { - if (file_exists(__DIR__ . '/../_files/compressed.gz')) { - unlink(__DIR__ . '/../_files/compressed.gz'); + if (file_exists($this->target)) { + unlink($this->target); } } @@ -137,7 +141,7 @@ public function testGzGetSetArchive() public function testGzCompressToFile() { $filter = new GzCompression(); - $archive = __DIR__ . '/../_files/compressed.gz'; + $archive = $this->target; $filter->setArchive($archive); $content = $filter->compress('compress me'); diff --git a/test/Compress/RarTest.php b/test/Compress/RarTest.php index 74ed9179..10630128 100644 --- a/test/Compress/RarTest.php +++ b/test/Compress/RarTest.php @@ -16,22 +16,27 @@ */ class RarTest extends \PHPUnit_Framework_TestCase { + public $tmp; + public function setUp() { if (!extension_loaded('rar')) { $this->markTestSkipped('This adapter needs the rar extension'); } + $this->tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('zfilter'); + mkdir($this->tmp); + $files = [ - dirname(__DIR__) . '/_files/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First/Second/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First/Second', - dirname(__DIR__) . '/_files/_compress/Compress/First/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First', - dirname(__DIR__) . '/_files/_compress/Compress/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress', - dirname(__DIR__) . '/_files/_compress/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress' + $this->tmp . '/zipextracted.txt', + $this->tmp . '/_compress/Compress/First/Second/zipextracted.txt', + $this->tmp . '/_compress/Compress/First/Second', + $this->tmp . '/_compress/Compress/First/zipextracted.txt', + $this->tmp . '/_compress/Compress/First', + $this->tmp . '/_compress/Compress/zipextracted.txt', + $this->tmp . '/_compress/Compress', + $this->tmp . '/_compress/zipextracted.txt', + $this->tmp . '/_compress' ]; foreach ($files as $file) { @@ -43,27 +48,20 @@ public function setUp() } } } - - if (!file_exists(dirname(__DIR__) . '/_files/Compress/First/Second')) { - mkdir(dirname(__DIR__) . '/_files/Compress/First/Second', 0777, true); - file_put_contents(dirname(__DIR__) . '/_files/Compress/First/Second/zipextracted.txt', 'compress me'); - file_put_contents(dirname(__DIR__) . '/_files/Compress/First/zipextracted.txt', 'compress me'); - file_put_contents(dirname(__DIR__) . '/_files/Compress/zipextracted.txt', 'compress me'); - } } public function tearDown() { $files = [ - dirname(__DIR__) . '/_files/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First/Second/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First/Second', - dirname(__DIR__) . '/_files/_compress/Compress/First/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First', - dirname(__DIR__) . '/_files/_compress/Compress/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress', - dirname(__DIR__) . '/_files/_compress/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress' + $this->tmp . '/zipextracted.txt', + $this->tmp . '/_compress/Compress/First/Second/zipextracted.txt', + $this->tmp . '/_compress/Compress/First/Second', + $this->tmp . '/_compress/Compress/First/zipextracted.txt', + $this->tmp . '/_compress/Compress/First', + $this->tmp . '/_compress/Compress/zipextracted.txt', + $this->tmp . '/_compress/Compress', + $this->tmp . '/_compress/zipextracted.txt', + $this->tmp . '/_compress' ]; foreach ($files as $file) { @@ -75,13 +73,6 @@ public function tearDown() } } } - - if (!file_exists(dirname(__DIR__) . '/_files/Compress/First/Second')) { - mkdir(dirname(__DIR__) . '/_files/Compress/First/Second', 0777, true); - file_put_contents(dirname(__DIR__) . '/_files/Compress/First/Second/zipextracted.txt', 'compress me'); - file_put_contents(dirname(__DIR__) . '/_files/Compress/First/zipextracted.txt', 'compress me'); - file_put_contents(dirname(__DIR__) . '/_files/Compress/zipextracted.txt', 'compress me'); - } } /** @@ -94,18 +85,20 @@ public function testBasicUsage() $filter = new RarCompression( [ 'archive' => dirname(__DIR__) . '/_files/compressed.rar', - 'target' => dirname(__DIR__) . '/_files/zipextracted.txt', + 'target' => $this->tmp . '/zipextracted.txt', 'callback' => [__CLASS__, 'rarCompress'] ] ); $content = $filter->compress('compress me'); - $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' - . DIRECTORY_SEPARATOR . 'compressed.rar', $content); + $this->assertEquals( + dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'compressed.rar', + $content + ); $content = $filter->decompress($content); $this->assertTrue($content); - $content = file_get_contents(dirname(__DIR__) . '/_files/zipextracted.txt'); + $content = file_get_contents($this->tmp . '/zipextracted.txt'); $this->assertEquals('compress me', $content); } @@ -225,19 +218,21 @@ public function testRarCompressFile() $filter = new RarCompression( [ 'archive' => dirname(__DIR__) . '/_files/compressed.rar', - 'target' => dirname(__DIR__) . '/_files/zipextracted.txt', + 'target' => $this->tmp . '/zipextracted.txt', 'callback' => [__CLASS__, 'rarCompress'] ] ); - file_put_contents(dirname(__DIR__) . '/_files/zipextracted.txt', 'compress me'); + file_put_contents($this->tmp . '/zipextracted.txt', 'compress me'); - $content = $filter->compress(dirname(__DIR__) . '/_files/zipextracted.txt'); - $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' - . DIRECTORY_SEPARATOR . 'compressed.rar', $content); + $content = $filter->compress($this->tmp . '/zipextracted.txt'); + $this->assertEquals( + dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'compressed.rar', + $content + ); $content = $filter->decompress($content); $this->assertTrue($content); - $content = file_get_contents(dirname(__DIR__) . '/_files/zipextracted.txt'); + $content = file_get_contents($this->tmp . '/zipextracted.txt'); $this->assertEquals('compress me', $content); } @@ -251,27 +246,32 @@ public function testRarCompressDirectory() $filter = new RarCompression( [ 'archive' => dirname(__DIR__) . '/_files/compressed.rar', - 'target' => dirname(__DIR__) . '/_files/_compress', + 'target' => $this->tmp . '/_compress', 'callback' => [__CLASS__, 'rarCompress'] ] ); $content = $filter->compress(dirname(__DIR__) . '/_files/Compress'); - $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' - . DIRECTORY_SEPARATOR . 'compressed.rar', $content); + $this->assertEquals( + dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'compressed.rar', + $content + ); - mkdir(dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . '_compress'); + mkdir($this->tmp . '/_compress'); $content = $filter->decompress($content); $this->assertTrue($content); - $base = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' - . DIRECTORY_SEPARATOR . '_compress' . DIRECTORY_SEPARATOR . 'Compress' . DIRECTORY_SEPARATOR; + $base = $this->tmp + . DIRECTORY_SEPARATOR + . '_compress' + . DIRECTORY_SEPARATOR + . 'Compress' + . DIRECTORY_SEPARATOR; $this->assertFileExists($base); $this->assertFileExists($base . 'zipextracted.txt'); $this->assertFileExists($base . 'First' . DIRECTORY_SEPARATOR . 'zipextracted.txt'); - $this->assertFileExists($base . 'First' . DIRECTORY_SEPARATOR . - 'Second' . DIRECTORY_SEPARATOR . 'zipextracted.txt'); - $content = file_get_contents(dirname(__DIR__) . '/_files/Compress/zipextracted.txt'); - $this->assertEquals('compress me', $content); + $this->assertFileExists( + $base . 'First' . DIRECTORY_SEPARATOR . 'Second' . DIRECTORY_SEPARATOR . 'zipextracted.txt' + ); } /** diff --git a/test/Compress/TarTest.php b/test/Compress/TarTest.php index 7f5684ea..c545406c 100644 --- a/test/Compress/TarTest.php +++ b/test/Compress/TarTest.php @@ -17,6 +17,8 @@ */ class TarTest extends \PHPUnit_Framework_TestCase { + public $tmp; + public function setUp() { if (!class_exists('Archive_Tar')) { @@ -27,52 +29,25 @@ public function setUp() } } - $files = [ - dirname(__DIR__) . '/_files/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First/Second/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First/Second', - dirname(__DIR__) . '/_files/_compress/Compress/First/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First', - dirname(__DIR__) . '/_files/_compress/Compress/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress', - dirname(__DIR__) . '/_files/_compress/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress', - dirname(__DIR__) . '/_files/compressed.tar' - ]; - - foreach ($files as $file) { - if (file_exists($file)) { - if (is_dir($file)) { - rmdir($file); - } else { - unlink($file); - } - } - } - - if (!file_exists(dirname(__DIR__) . '/_files/Compress/First/Second')) { - mkdir(dirname(__DIR__) . '/_files/Compress/First/Second', 0777, true); - file_put_contents(dirname(__DIR__) . '/_files/Compress/First/Second/zipextracted.txt', 'compress me'); - file_put_contents(dirname(__DIR__) . '/_files/Compress/First/zipextracted.txt', 'compress me'); - file_put_contents(dirname(__DIR__) . '/_files/Compress/zipextracted.txt', 'compress me'); - } + $this->tmp = sprintf('%s/%s', sys_get_temp_dir(), uniqid('zfilter')); + mkdir($this->tmp, 0775, true); } public function tearDown() { $files = [ - dirname(__DIR__) . '/_files/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First/Second/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First/Second', - dirname(__DIR__) . '/_files/_compress/Compress/First/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress/First', - dirname(__DIR__) . '/_files/_compress/Compress/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress/Compress', - dirname(__DIR__) . '/_files/_compress/zipextracted.txt', - dirname(__DIR__) . '/_files/_compress', - dirname(__DIR__) . '/_files/compressed.tar', - dirname(__DIR__) . '/_files/compressed.tar.gz', - dirname(__DIR__) . '/_files/compressed.tar.bz2' + $this->tmp . '/zipextracted.txt', + $this->tmp . '/_compress/Compress/First/Second/zipextracted.txt', + $this->tmp . '/_compress/Compress/First/Second', + $this->tmp . '/_compress/Compress/First/zipextracted.txt', + $this->tmp . '/_compress/Compress/First', + $this->tmp . '/_compress/Compress/zipextracted.txt', + $this->tmp . '/_compress/Compress', + $this->tmp . '/_compress/zipextracted.txt', + $this->tmp . '/_compress', + $this->tmp . '/compressed.tar', + $this->tmp . '/compressed.tar.gz', + $this->tmp . '/compressed.tar.bz2' ]; foreach ($files as $file) { @@ -84,13 +59,6 @@ public function tearDown() } } } - - if (!file_exists(dirname(__DIR__) . '/_files/Compress/First/Second')) { - mkdir(dirname(__DIR__) . '/_files/Compress/First/Second', 0777, true); - file_put_contents(dirname(__DIR__) . '/_files/Compress/First/Second/zipextracted.txt', 'compress me'); - file_put_contents(dirname(__DIR__) . '/_files/Compress/First/zipextracted.txt', 'compress me'); - file_put_contents(dirname(__DIR__) . '/_files/Compress/zipextracted.txt', 'compress me'); - } } /** @@ -102,18 +70,20 @@ public function testBasicUsage() { $filter = new TarCompression( [ - 'archive' => dirname(__DIR__) . '/_files/compressed.tar', - 'target' => dirname(__DIR__) . '/_files/zipextracted.txt' + 'archive' => $this->tmp . '/compressed.tar', + 'target' => $this->tmp . '/zipextracted.txt' ] ); $content = $filter->compress('compress me'); - $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' - . DIRECTORY_SEPARATOR . 'compressed.tar', $content); + $this->assertEquals( + $this->tmp . DIRECTORY_SEPARATOR . 'compressed.tar', + $content + ); $content = $filter->decompress($content); - $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR, $content); - $content = file_get_contents(dirname(__DIR__) . '/_files/zipextracted.txt'); + $this->assertEquals($this->tmp . DIRECTORY_SEPARATOR, $content); + $content = file_get_contents($this->tmp . '/zipextracted.txt'); $this->assertEquals('compress me', $content); } @@ -183,19 +153,21 @@ public function testTarCompressToFile() { $filter = new TarCompression( [ - 'archive' => dirname(__DIR__) . '/_files/compressed.tar', - 'target' => dirname(__DIR__) . '/_files/zipextracted.txt' + 'archive' => $this->tmp . '/compressed.tar', + 'target' => $this->tmp . '/zipextracted.txt' ] ); - file_put_contents(dirname(__DIR__) . '/_files/zipextracted.txt', 'compress me'); + file_put_contents($this->tmp . '/zipextracted.txt', 'compress me'); - $content = $filter->compress(dirname(__DIR__) . '/_files/zipextracted.txt'); - $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' - . DIRECTORY_SEPARATOR . 'compressed.tar', $content); + $content = $filter->compress($this->tmp . '/zipextracted.txt'); + $this->assertEquals( + $this->tmp . DIRECTORY_SEPARATOR . 'compressed.tar', + $content + ); $content = $filter->decompress($content); - $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR, $content); - $content = file_get_contents(dirname(__DIR__) . '/_files/zipextracted.txt'); + $this->assertEquals($this->tmp . DIRECTORY_SEPARATOR, $content); + $content = file_get_contents($this->tmp . '/zipextracted.txt'); $this->assertEquals('compress me', $content); } @@ -208,22 +180,27 @@ public function testTarCompressDirectory() { $filter = new TarCompression( [ - 'archive' => dirname(__DIR__) . '/_files/compressed.tar', - 'target' => dirname(__DIR__) . '/_files/_compress' + 'archive' => $this->tmp . '/compressed.tar', + 'target' => $this->tmp . '/_compress' ] ); $content = $filter->compress(dirname(__DIR__) . '/_files/Compress'); - $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' - . DIRECTORY_SEPARATOR . 'compressed.tar', $content); + $this->assertEquals( + $this->tmp . DIRECTORY_SEPARATOR . 'compressed.tar', + $content + ); } public function testSetModeShouldWorkWithCaseInsensitive() { $filter = new TarCompression; - $filter->setTarget(dirname(__DIR__).'/_files/zipextracted.txt'); + $filter->setTarget($this->tmp . '/zipextracted.txt'); foreach (['GZ', 'Bz2'] as $mode) { - $archive = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'compressed.tar.'.strtolower($mode); + $archive = implode(DIRECTORY_SEPARATOR, [ + $this->tmp, + 'compressed.tar.', + ]) . strtolower($mode); $filter->setArchive($archive); $filter->setMode($mode); $content = $filter->compress('compress me'); diff --git a/test/CompressTest.php b/test/CompressTest.php index 75e6b8f4..a2f635c6 100644 --- a/test/CompressTest.php +++ b/test/CompressTest.php @@ -16,17 +16,25 @@ */ class CompressTest extends \PHPUnit_Framework_TestCase { + public $tmpDir; + public function setUp() { if (!extension_loaded('bz2')) { $this->markTestSkipped('This filter is tested with the bz2 extension'); } + + $this->tmpDir = sprintf('%s/%s', sys_get_temp_dir(), uniqid('zfilter')); + mkdir($this->tmpDir, 0775, true); } public function tearDown() { - if (file_exists(__DIR__ . '/_files/compressed.bz2')) { - unlink(__DIR__ . '/_files/compressed.bz2'); + if (is_dir($this->tmpDir)) { + if (file_exists($this->tmpDir . '/compressed.bz2')) { + unlink($this->tmpDir . '/compressed.bz2'); + } + rmdir($this->tmpDir); } } @@ -131,7 +139,7 @@ public function testGetSetArchive() public function testCompressToFile() { $filter = new CompressFilter('bz2'); - $archive = __DIR__ . '/_files/compressed.bz2'; + $archive = $this->tmpDir . '/compressed.bz2'; $filter->setArchive($archive); $content = $filter('compress me'); @@ -200,7 +208,7 @@ public function testSetAdapter() public function testDecompressArchive() { $filter = new CompressFilter('bz2'); - $archive = __DIR__ . '/_files/compressed.bz2'; + $archive = $this->tmpDir . '/compressed.bz2'; $filter->setArchive($archive); $content = $filter('compress me'); diff --git a/test/DecompressTest.php b/test/DecompressTest.php index 5007e4a4..681c2a5a 100644 --- a/test/DecompressTest.php +++ b/test/DecompressTest.php @@ -16,17 +16,25 @@ */ class DecompressTest extends \PHPUnit_Framework_TestCase { + public $tmpDir; + public function setUp() { if (!extension_loaded('bz2')) { $this->markTestSkipped('This filter is tested with the bz2 extension'); } + + $this->tmpDir = sprintf('%s/%s', sys_get_temp_dir(), uniqid('zfilter')); + mkdir($this->tmpDir, 0775, true); } public function tearDown() { - if (file_exists(__DIR__ . '/_files/compressed.bz2')) { - unlink(__DIR__ . '/_files/compressed.bz2'); + if (is_dir($this->tmpDir)) { + if (file_exists($this->tmpDir . '/compressed.bz2')) { + unlink($this->tmpDir . '/compressed.bz2'); + } + rmdir($this->tmpDir); } } @@ -54,8 +62,8 @@ public function testBasicUsage() */ public function testCompressToFile() { - $filter = new DecompressFilter('bz2'); - $archive = __DIR__ . '/_files/compressed.bz2'; + $filter = new DecompressFilter('bz2'); + $archive = $this->tmpDir . '/compressed.bz2'; $filter->setArchive($archive); $content = $filter->compress('compress me'); @@ -78,8 +86,8 @@ public function testCompressToFile() */ public function testDecompressArchive() { - $filter = new DecompressFilter('bz2'); - $archive = __DIR__ . '/_files/compressed.bz2'; + $filter = new DecompressFilter('bz2'); + $archive = $this->tmpDir . '/compressed.bz2'; $filter->setArchive($archive); $content = $filter->compress('compress me'); @@ -92,8 +100,8 @@ public function testDecompressArchive() public function testFilterMethodProxiesToDecompress() { - $filter = new DecompressFilter('bz2'); - $archive = __DIR__ . '/_files/compressed.bz2'; + $filter = new DecompressFilter('bz2'); + $archive = $this->tmpDir . '/compressed.bz2'; $filter->setArchive($archive); $content = $filter->compress('compress me'); diff --git a/test/File/DecryptTest.php b/test/File/DecryptTest.php index d886b60a..1645d263 100644 --- a/test/File/DecryptTest.php +++ b/test/File/DecryptTest.php @@ -17,29 +17,34 @@ */ class DecryptTest extends \PHPUnit_Framework_TestCase { + public $fileToEncrypt; + + public $tmpDir; + public function setUp() { if (!extension_loaded('mcrypt')) { $this->markTestSkipped('This filter needs the mcrypt extension'); } - if (file_exists(dirname(__DIR__).'/_files/newencryption.txt')) { - unlink(dirname(__DIR__).'/_files/newencryption.txt'); - } + $this->tmpDir = sprintf('%s/%s', sys_get_temp_dir(), uniqid('zfilter')); + mkdir($this->tmpDir, 0775, true); - if (file_exists(dirname(__DIR__).'/_files/newencryption2.txt')) { - unlink(dirname(__DIR__).'/_files/newencryption2.txt'); - } + $this->fileToEncrypt = dirname(__DIR__) . '/_files/encryption.txt'; } public function tearDown() { - if (file_exists(dirname(__DIR__).'/_files/newencryption.txt')) { - unlink(dirname(__DIR__).'/_files/newencryption.txt'); - } + if (is_dir($this->tmpDir)) { + if (file_exists($this->tmpDir . '/newencryption.txt')) { + unlink($this->tmpDir . '/newencryption.txt'); + } + + if (file_exists($this->tmpDir . '/newencryption2.txt')) { + unlink($this->tmpDir . '/newencryption2.txt'); + } - if (file_exists(dirname(__DIR__).'/_files/newencryption2.txt')) { - unlink(dirname(__DIR__).'/_files/newencryption2.txt'); + rmdir($this->tmpDir); } } @@ -51,57 +56,66 @@ public function tearDown() public function testBasic() { $filter = new FileEncrypt(); - $filter->setFilename(dirname(__DIR__).'/_files/newencryption.txt'); + $filter->setFilename($this->tmpDir . '/newencryption.txt'); $this->assertEquals( - dirname(__DIR__).'/_files/newencryption.txt', - $filter->getFilename()); + $this->tmpDir . '/newencryption.txt', + $filter->getFilename() + ); $filter->setKey('1234567890123456'); - $filter->filter(dirname(__DIR__).'/_files/encryption.txt'); + $filter->filter($this->fileToEncrypt); $filter = new FileDecrypt(); $this->assertNotEquals( 'Encryption', - file_get_contents(dirname(__DIR__).'/_files/newencryption.txt')); + file_get_contents($this->tmpDir . '/newencryption.txt') + ); $filter->setKey('1234567890123456'); $this->assertEquals( - dirname(__DIR__).'/_files/newencryption.txt', - $filter->filter(dirname(__DIR__).'/_files/newencryption.txt')); + $this->tmpDir . '/newencryption.txt', + $filter->filter($this->tmpDir . '/newencryption.txt') + ); $this->assertEquals( 'Encryption', - trim(file_get_contents(dirname(__DIR__).'/_files/newencryption.txt'))); + trim(file_get_contents($this->tmpDir . '/newencryption.txt')) + ); } public function testEncryptionWithDecryption() { $filter = new FileEncrypt(); - $filter->setFilename(dirname(__DIR__).'/_files/newencryption.txt'); + $filter->setFilename($this->tmpDir . '/newencryption.txt'); $filter->setKey('1234567890123456'); - $this->assertEquals(dirname(__DIR__).'/_files/newencryption.txt', - $filter->filter(dirname(__DIR__).'/_files/encryption.txt')); + $this->assertEquals( + $this->tmpDir . '/newencryption.txt', + $filter->filter($this->fileToEncrypt) + ); $this->assertNotEquals( 'Encryption', - file_get_contents(dirname(__DIR__).'/_files/newencryption.txt')); + file_get_contents($this->tmpDir . '/newencryption.txt') + ); $filter = new FileDecrypt(); - $filter->setFilename(dirname(__DIR__).'/_files/newencryption2.txt'); + $filter->setFilename($this->tmpDir . '/newencryption2.txt'); $this->assertEquals( - dirname(__DIR__).'/_files/newencryption2.txt', - $filter->getFilename()); + $this->tmpDir . '/newencryption2.txt', + $filter->getFilename() + ); $filter->setKey('1234567890123456'); - $input = $filter->filter(dirname(__DIR__).'/_files/newencryption.txt'); - $this->assertEquals(dirname(__DIR__).'/_files/newencryption2.txt', $input); + $input = $filter->filter($this->tmpDir . '/newencryption.txt'); + $this->assertEquals($this->tmpDir . '/newencryption2.txt', $input); $this->assertEquals( 'Encryption', - trim(file_get_contents(dirname(__DIR__).'/_files/newencryption2.txt'))); + trim(file_get_contents($this->tmpDir . '/newencryption2.txt')) + ); } /** @@ -113,7 +127,7 @@ public function testNonExistingFile() $filter->setVector('1234567890123456'); $this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'not found'); - $filter->filter(dirname(__DIR__).'/_files/nofile.txt'); + $filter->filter($this->tmpDir . '/nofile.txt'); } public function returnUnfilteredDataProvider() @@ -122,8 +136,8 @@ public function returnUnfilteredDataProvider() [null], [new \stdClass()], [[ - dirname(__DIR__).'/_files/nofile.txt', - dirname(__DIR__).'/_files/nofile2.txt' + $this->tmpDir . '/nofile.txt', + $this->tmpDir . '/nofile2.txt' ]] ]; } diff --git a/test/File/EncryptTest.php b/test/File/EncryptTest.php index aa2926ae..c39d39ed 100644 --- a/test/File/EncryptTest.php +++ b/test/File/EncryptTest.php @@ -16,21 +16,25 @@ */ class EncryptTest extends \PHPUnit_Framework_TestCase { + public $fileToEncrypt; + public $testDir; + public $testFile; + public function setUp() { if (! extension_loaded('mcrypt')) { $this->markTestSkipped('This filter needs the mcrypt extension'); } - if (file_exists(dirname(__DIR__) . '/_files/newencryption.txt')) { - unlink(dirname(__DIR__) . '/_files/newencryption.txt'); - } + $this->fileToEncrypt = dirname(__DIR__) . '/_files/encryption.txt'; + $this->testDir = sys_get_temp_dir(); + $this->testFile = sprintf('%s/%s.txt', sys_get_temp_dir(), uniqid('zfilter')); } public function tearDown() { - if (file_exists(dirname(__DIR__) . '/_files/newencryption.txt')) { - unlink(dirname(__DIR__) . '/_files/newencryption.txt'); + if (file_exists($this->testFile)) { + unlink($this->testFile); } } @@ -42,33 +46,33 @@ public function tearDown() public function testBasic() { $filter = new FileEncrypt(); - $filter->setFilename(dirname(__DIR__) . '/_files/newencryption.txt'); + $filter->setFilename($this->testFile); - $this->assertEquals(dirname(__DIR__) . '/_files/newencryption.txt', $filter->getFilename()); + $this->assertEquals($this->testFile, $filter->getFilename()); $filter->setKey('1234567890123456'); - $this->assertEquals(dirname(__DIR__) . '/_files/newencryption.txt', $filter->filter(dirname(__DIR__) . '/_files/encryption.txt')); + $this->assertEquals($this->testFile, $filter->filter($this->fileToEncrypt)); - $this->assertEquals('Encryption', file_get_contents(dirname(__DIR__) . '/_files/encryption.txt')); + $this->assertEquals('Encryption', file_get_contents($this->fileToEncrypt)); - $this->assertNotEquals('Encryption', file_get_contents(dirname(__DIR__) . '/_files/newencryption.txt')); + $this->assertNotEquals('Encryption', file_get_contents($this->testFile)); } public function testEncryptionWithDecryption() { $filter = new FileEncrypt(); - $filter->setFilename(dirname(__DIR__) . '/_files/newencryption.txt'); + $filter->setFilename($this->testFile); $filter->setKey('1234567890123456'); - $this->assertEquals(dirname(__DIR__) . '/_files/newencryption.txt', $filter->filter(dirname(__DIR__) . '/_files/encryption.txt')); + $this->assertEquals($this->testFile, $filter->filter($this->fileToEncrypt)); - $this->assertNotEquals('Encryption', file_get_contents(dirname(__DIR__) . '/_files/newencryption.txt')); + $this->assertNotEquals('Encryption', file_get_contents($this->testFile)); $filter = new FileDecrypt(); $filter->setKey('1234567890123456'); - $input = $filter->filter(dirname(__DIR__) . '/_files/newencryption.txt'); - $this->assertEquals(dirname(__DIR__) . '/_files/newencryption.txt', $input); + $input = $filter->filter($this->testFile); + $this->assertEquals($this->testFile, $input); - $this->assertEquals('Encryption', trim(file_get_contents(dirname(__DIR__) . '/_files/newencryption.txt'))); + $this->assertEquals('Encryption', trim(file_get_contents($this->testFile))); } /** @@ -81,7 +85,7 @@ public function testNonExistingFile() $filter->setKey('1234567890123456'); $this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'not found'); - echo $filter->filter(dirname(__DIR__) . '/_files/nofile.txt'); + $filter->filter(sprintf('%s/%s.txt', $this->testDir, uniqid())); } /** @@ -93,10 +97,10 @@ public function testEncryptionInSameFile() $filter = new FileEncrypt(); $filter->setKey('1234567890123456'); - copy(dirname(__DIR__) . '/_files/encryption.txt', dirname(__DIR__) . '/_files/newencryption.txt'); - $filter->filter(dirname(__DIR__) . '/_files/newencryption.txt'); + copy($this->fileToEncrypt, $this->testFile); + $filter->filter($this->testFile); - $this->assertNotEquals('Encryption', trim(file_get_contents(dirname(__DIR__) . '/_files/newencryption.txt'))); + $this->assertNotEquals('Encryption', trim(file_get_contents($this->testFile))); } public function returnUnfilteredDataProvider() @@ -105,8 +109,8 @@ public function returnUnfilteredDataProvider() [null], [new \stdClass()], [[ - dirname(__DIR__) . '/_files/nofile.txt', - dirname(__DIR__) . '/_files/nofile2.txt' + sprintf('%s/%s.txt', sys_get_temp_dir(), uniqid()), + sprintf('%s/%s.txt', sys_get_temp_dir(), uniqid()), ]] ]; } diff --git a/test/File/LowerCaseTest.php b/test/File/LowerCaseTest.php index 3e3c32ac..bff1795d 100644 --- a/test/File/LowerCaseTest.php +++ b/test/File/LowerCaseTest.php @@ -16,26 +16,14 @@ */ class LowerCaseTest extends \PHPUnit_Framework_TestCase { - /** - * Path to test files - * - * @var string - */ - protected $_filesPath; - - /** - * Original testfile - * - * @var string - */ - protected $_origFile; + protected $testDir; /** * Testfile * * @var string */ - protected $_newFile; + protected $testFile; /** * Sets the path to test files @@ -44,13 +32,10 @@ class LowerCaseTest extends \PHPUnit_Framework_TestCase */ public function setUp() { - $this->_filesPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR; - $this->_origFile = $this->_filesPath . 'testfile2.txt'; - $this->_newFile = $this->_filesPath . 'newtestfile2.txt'; - - if (!file_exists($this->_newFile)) { - copy($this->_origFile, $this->_newFile); - } + $source = dirname(__DIR__) . '/_files/testfile2.txt'; + $this->testDir = sys_get_temp_dir(); + $this->testFile = sprintf('%s/%s.txt', $this->testDir, uniqid('zfilter')); + copy($source, $this->testFile); } /** @@ -60,8 +45,8 @@ public function setUp() */ public function tearDown() { - if (file_exists($this->_newFile)) { - unlink($this->_newFile); + if (file_exists($this->testFile)) { + unlink($this->testFile); } } @@ -70,10 +55,10 @@ public function tearDown() */ public function testInstanceCreationAndNormalWorkflow() { - $this->assertContains('This is a File', file_get_contents($this->_newFile)); + $this->assertContains('This is a File', file_get_contents($this->testFile)); $filter = new FileLowerCase(); - $filter($this->_newFile); - $this->assertContains('this is a file', file_get_contents($this->_newFile)); + $filter($this->testFile); + $this->assertContains('this is a file', file_get_contents($this->testFile)); } /** @@ -81,10 +66,10 @@ public function testInstanceCreationAndNormalWorkflow() */ public function testNormalWorkflowWithFilesArray() { - $this->assertContains('This is a File', file_get_contents($this->_newFile)); + $this->assertContains('This is a File', file_get_contents($this->testFile)); $filter = new FileLowerCase(); - $filter(['tmp_name' => $this->_newFile]); - $this->assertContains('this is a file', file_get_contents($this->_newFile)); + $filter(['tmp_name' => $this->testFile]); + $this->assertContains('this is a file', file_get_contents($this->testFile)); } /** @@ -94,7 +79,7 @@ public function testFileNotFoundException() { $this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'not found'); $filter = new FileLowerCase(); - $filter($this->_newFile . 'unknown'); + $filter($this->testFile . 'unknown'); } /** @@ -102,11 +87,11 @@ public function testFileNotFoundException() */ public function testCheckSettingOfEncodingInIstance() { - $this->assertContains('This is a File', file_get_contents($this->_newFile)); + $this->assertContains('This is a File', file_get_contents($this->testFile)); try { $filter = new FileLowerCase('ISO-8859-1'); - $filter($this->_newFile); - $this->assertContains('this is a file', file_get_contents($this->_newFile)); + $filter($this->testFile); + $this->assertContains('this is a file', file_get_contents($this->testFile)); } catch (\Zend\Filter\Exception\ExtensionNotLoadedException $e) { $this->assertContains('mbstring is required', $e->getMessage()); } @@ -117,12 +102,12 @@ public function testCheckSettingOfEncodingInIstance() */ public function testCheckSettingOfEncodingWithMethod() { - $this->assertContains('This is a File', file_get_contents($this->_newFile)); + $this->assertContains('This is a File', file_get_contents($this->testFile)); try { $filter = new FileLowerCase(); $filter->setEncoding('ISO-8859-1'); - $filter($this->_newFile); - $this->assertContains('this is a file', file_get_contents($this->_newFile)); + $filter($this->testFile); + $this->assertContains('this is a file', file_get_contents($this->testFile)); } catch (\Zend\Filter\Exception\ExtensionNotLoadedException $e) { $this->assertContains('mbstring is required', $e->getMessage()); } @@ -134,8 +119,8 @@ public function returnUnfilteredDataProvider() [null], [new \stdClass()], [[ - dirname(__DIR__).'/_files/nofile.txt', - dirname(__DIR__).'/_files/nofile2.txt' + sprintf('%s/%s.txt', $this->testDir, uniqid()), + sprintf('%s/%s.txt', $this->testDir, uniqid()), ]] ]; } diff --git a/test/File/RenameTest.php b/test/File/RenameTest.php index a39d9751..81f9ada0 100644 --- a/test/File/RenameTest.php +++ b/test/File/RenameTest.php @@ -21,42 +21,42 @@ class RenameTest extends \PHPUnit_Framework_TestCase * * @var string */ - protected $_filesPath; + protected $tmpPath; /** * Original testfile * * @var string */ - protected $_origFile; + protected $origFile; /** * Testfile * * @var string */ - protected $_oldFile; + protected $oldFile; /** * Testfile * * @var string */ - protected $_newFile; + protected $newFile; /** * Testdirectory * * @var string */ - protected $_newDir; + protected $newDir; /** * Testfile in Testdirectory * * @var string */ - protected $_newDirFile; + protected $newDirFile; /** * Sets the path to test files @@ -65,26 +65,19 @@ class RenameTest extends \PHPUnit_Framework_TestCase */ public function setUp() { - $this->_filesPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR; - $this->_origFile = $this->_filesPath . 'original.file'; - $this->_oldFile = $this->_filesPath . 'testfile.txt'; - $this->_newFile = $this->_filesPath . 'newfile.xml'; - $this->_newDir = $this->_filesPath . DIRECTORY_SEPARATOR . '_testDir2'; - $this->_newDirFile = $this->_newDir . DIRECTORY_SEPARATOR . 'testfile.txt'; - - if (file_exists($this->_origFile)) { - unlink($this->_origFile); - } - - if (file_exists($this->_newFile)) { - unlink($this->_newFile); - } - - if (file_exists($this->_newDirFile)) { - unlink($this->_newDirFile); - } - - copy($this->_oldFile, $this->_origFile); + $control = sprintf('%s/_files/testfile.txt', dirname(__DIR__)); + $this->tmpPath = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, uniqid('zfilter')); + mkdir($this->tmpPath, 0775, true); + + $this->oldFile = sprintf('%s%stestfile.txt', $this->tmpPath, DIRECTORY_SEPARATOR); + $this->origFile = sprintf('%s%soriginal.file', $this->tmpPath, DIRECTORY_SEPARATOR); + $this->newFile = sprintf('%s%snewfile.xml', $this->tmpPath, DIRECTORY_SEPARATOR); + $this->newDir = sprintf('%s%stestdir', $this->tmpPath, DIRECTORY_SEPARATOR); + $this->newDirFile = sprintf('%s%stestfile.txt', $this->newDir, DIRECTORY_SEPARATOR); + + copy($control, $this->oldFile); + copy($control, $this->origFile); + mkdir($this->newDir, 0775, true); } /** @@ -94,20 +87,23 @@ public function setUp() */ public function tearDown() { - if (!file_exists($this->_oldFile)) { - copy($this->_origFile, $this->_oldFile); - } - - if (file_exists($this->_origFile)) { - unlink($this->_origFile); - } - - if (file_exists($this->_newFile)) { - unlink($this->_newFile); - } - - if (file_exists($this->_newDirFile)) { - unlink($this->_newDirFile); + if (is_dir($this->tmpPath)) { + if (file_exists($this->oldFile)) { + unlink($this->oldFile); + } + if (file_exists($this->origFile)) { + unlink($this->origFile); + } + if (file_exists($this->newFile)) { + unlink($this->newFile); + } + if (is_dir($this->newDir)) { + if (file_exists($this->newDirFile)) { + unlink($this->newDirFile); + } + rmdir($this->newDir); + } + rmdir($this->tmpPath); } } @@ -118,18 +114,18 @@ public function tearDown() */ public function testConstructSingleValue() { - $filter = new FileRename($this->_newFile); + $filter = new FileRename($this->newFile); $this->assertEquals( [0 => [ 'source' => '*', - 'target' => $this->_newFile, + 'target' => $this->newFile, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->newFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -140,20 +136,20 @@ public function testConstructSingleValue() */ public function testConstructSingleValueWithFilesArray() { - $filter = new FileRename($this->_newFile); + $filter = new FileRename($this->newFile); $this->assertEquals( [0 => [ 'source' => '*', - 'target' => $this->_newFile, + 'target' => $this->newFile, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); $this->assertEquals( - ['tmp_name' => $this->_newFile], - $filter(['tmp_name' => $this->_oldFile]) + ['tmp_name' => $this->newFile], + $filter(['tmp_name' => $this->oldFile]) ); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -166,19 +162,19 @@ public function testConstructSingleValueWithFilesArray() public function testConstructSingleArray() { $filter = new FileRename([ - 'source' => $this->_oldFile, - 'target' => $this->_newFile]); + 'source' => $this->oldFile, + 'target' => $this->newFile]); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->newFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -190,8 +186,8 @@ public function testConstructSingleArray() public function testConstructFullOptionsArray() { $filter = new FileRename([ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'overwrite' => true, 'randomize' => false, 'unknown' => false @@ -199,14 +195,14 @@ public function testConstructFullOptionsArray() $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'overwrite' => true, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->newFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -219,19 +215,19 @@ public function testConstructDoubleArray() { $filter = new FileRename([ 0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newFile]]); + 'source' => $this->oldFile, + 'target' => $this->newFile]]); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->newFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -243,18 +239,18 @@ public function testConstructDoubleArray() public function testConstructTruncatedTarget() { $filter = new FileRename([ - 'source' => $this->_oldFile]); + 'source' => $this->oldFile]); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, + 'source' => $this->oldFile, 'target' => '*', 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_oldFile, $filter($this->_oldFile)); + $this->assertEquals($this->oldFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -266,18 +262,18 @@ public function testConstructTruncatedTarget() public function testConstructTruncatedSource() { $filter = new FileRename([ - 'target' => $this->_newFile]); + 'target' => $this->newFile]); $this->assertEquals( [0 => [ 'source' => '*', - 'target' => $this->_newFile, + 'target' => $this->newFile, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->newFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -288,18 +284,18 @@ public function testConstructTruncatedSource() */ public function testConstructSingleDirectory() { - $filter = new FileRename($this->_newDir); + $filter = new FileRename($this->newDir); $this->assertEquals( [0 => [ 'source' => '*', - 'target' => $this->_newDir, + 'target' => $this->newDir, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newDirFile, $filter($this->_oldFile)); + $this->assertEquals($this->newDirFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -311,19 +307,19 @@ public function testConstructSingleDirectory() public function testConstructSingleArrayDirectory() { $filter = new FileRename([ - 'source' => $this->_oldFile, - 'target' => $this->_newDir]); + 'source' => $this->oldFile, + 'target' => $this->newDir]); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newDir, + 'source' => $this->oldFile, + 'target' => $this->newDir, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newDirFile, $filter($this->_oldFile)); + $this->assertEquals($this->newDirFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -336,19 +332,19 @@ public function testConstructDoubleArrayDirectory() { $filter = new FileRename([ 0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newDir]]); + 'source' => $this->oldFile, + 'target' => $this->newDir]]); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newDir, + 'source' => $this->oldFile, + 'target' => $this->newDir, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newDirFile, $filter($this->_oldFile)); + $this->assertEquals($this->newDirFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -360,18 +356,18 @@ public function testConstructDoubleArrayDirectory() public function testConstructTruncatedSourceDirectory() { $filter = new FileRename([ - 'target' => $this->_newDir]); + 'target' => $this->newDir]); $this->assertEquals( [0 => [ 'source' => '*', - 'target' => $this->_newDir, + 'target' => $this->newDir, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newDirFile, $filter($this->_oldFile)); + $this->assertEquals($this->newDirFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -381,23 +377,23 @@ public function testConstructTruncatedSourceDirectory() public function testAddSameFileAgainAndOverwriteExistingTarget() { $filter = new FileRename([ - 'source' => $this->_oldFile, - 'target' => $this->_newDir]); + 'source' => $this->oldFile, + 'target' => $this->newDir]); $filter->addFile([ - 'source' => $this->_oldFile, - 'target' => $this->_newFile]); + 'source' => $this->oldFile, + 'target' => $this->newFile]); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->newFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -407,19 +403,20 @@ public function testAddSameFileAgainAndOverwriteExistingTarget() public function testGetNewName() { $filter = new FileRename([ - 'source' => $this->_oldFile, - 'target' => $this->_newDir]); + 'source' => $this->oldFile, + 'target' => $this->newDir, + ]); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newDir, + 'source' => $this->oldFile, + 'target' => $this->newDir, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newDirFile, $filter->getNewName($this->_oldFile)); + $this->assertEquals($this->newDirFile, $filter->getNewName($this->oldFile)); } /** @@ -428,22 +425,22 @@ public function testGetNewName() public function testGetNewNameExceptionWithExistingFile() { $filter = new FileRename([ - 'source' => $this->_oldFile, - 'target' => $this->_newFile]); + 'source' => $this->oldFile, + 'target' => $this->newFile]); - copy($this->_oldFile, $this->_newFile); + copy($this->oldFile, $this->newFile); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); $this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'could not be renamed'); - $this->assertEquals($this->_newFile, $filter->getNewName($this->_oldFile)); + $this->assertEquals($this->newFile, $filter->getNewName($this->oldFile)); } /** @@ -452,22 +449,22 @@ public function testGetNewNameExceptionWithExistingFile() public function testGetNewNameOverwriteWithExistingFile() { $filter = new FileRename([ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'overwrite' => true]); - copy($this->_oldFile, $this->_newFile); + copy($this->oldFile, $this->newFile); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'overwrite' => true, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newFile, $filter->getNewName($this->_oldFile)); + $this->assertEquals($this->newFile, $filter->getNewName($this->oldFile)); } /** @@ -476,22 +473,22 @@ public function testGetNewNameOverwriteWithExistingFile() public function testGetRandomizedFile() { $filter = new FileRename([ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'randomize' => true ]); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, - 'target' => $this->_newFile, + 'source' => $this->oldFile, + 'target' => $this->newFile, 'overwrite' => false, 'randomize' => true, ]], $filter->getFile() ); - $fileNoExt = $this->_filesPath . 'newfile'; - $this->assertRegExp('#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}\.xml#', $filter->getNewName($this->_oldFile)); + $fileNoExt = $this->tmpPath . DIRECTORY_SEPARATOR . 'newfile'; + $this->assertRegExp('#' . preg_quote($fileNoExt) . '_.{13}\.xml#', $filter->getNewName($this->oldFile)); } /** @@ -499,23 +496,23 @@ public function testGetRandomizedFile() */ public function testGetRandomizedFileWithoutExtension() { - $fileNoExt = $this->_filesPath . 'newfile'; + $fileNoExt = $this->tmpPath . DIRECTORY_SEPARATOR . 'newfile'; $filter = new FileRename([ - 'source' => $this->_oldFile, + 'source' => $this->oldFile, 'target' => $fileNoExt, 'randomize' => true ]); $this->assertEquals( [0 => [ - 'source' => $this->_oldFile, + 'source' => $this->oldFile, 'target' => $fileNoExt, 'overwrite' => false, 'randomize' => true, ]], $filter->getFile() ); - $this->assertRegExp('#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}#', $filter->getNewName($this->_oldFile)); + $this->assertRegExp('#' . preg_quote($fileNoExt) . '_.{13}#', $filter->getNewName($this->oldFile)); } /** @@ -523,19 +520,19 @@ public function testGetRandomizedFileWithoutExtension() */ public function testAddFileWithString() { - $filter = new FileRename($this->_oldFile); - $filter->addFile($this->_newFile); + $filter = new FileRename($this->oldFile); + $filter->addFile($this->newFile); $this->assertEquals( [0 => [ 'source' => '*', - 'target' => $this->_newFile, + 'target' => $this->newFile, 'overwrite' => false, 'randomize' => false, ]], $filter->getFile() ); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->newFile, $filter($this->oldFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -544,7 +541,7 @@ public function testAddFileWithString() */ public function testAddFileWithInvalidOption() { - $filter = new FileRename($this->_oldFile); + $filter = new FileRename($this->oldFile); $this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'Invalid options'); $filter->addFile(1234); } @@ -564,8 +561,8 @@ public function returnUnfilteredDataProvider() [null], [new \stdClass()], [[ - $this->_oldFile, - $this->_origFile + $this->oldFile, + $this->origFile ]] ]; } @@ -576,7 +573,7 @@ public function returnUnfilteredDataProvider() */ public function testReturnUnfiltered($input) { - $filter = new FileRename($this->_newFile); + $filter = new FileRename($this->newFile); $this->assertEquals($input, $filter($input)); } diff --git a/test/File/RenameUploadTest.php b/test/File/RenameUploadTest.php index b56857a5..ebedc43a 100644 --- a/test/File/RenameUploadTest.php +++ b/test/File/RenameUploadTest.php @@ -21,35 +21,35 @@ class RenameUploadTest extends \PHPUnit_Framework_TestCase * * @var string */ - protected $_filesPath; + protected $filesPath; /** * Testfile * * @var string */ - protected $_oldFile; + protected $sourceFile; /** * Testfile * * @var string */ - protected $_newFile; + protected $targetFile; /** * Testdirectory * * @var string */ - protected $_newDir; + protected $targetPath; /** * Testfile in Testdirectory * * @var string */ - protected $_newDirFile; + protected $targetPathFile; /** * Sets the path to test files @@ -58,19 +58,16 @@ class RenameUploadTest extends \PHPUnit_Framework_TestCase */ public function setUp() { - $this->_filesPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'RenameUploadTest'; - $this->_newDir = $this->_filesPath . DIRECTORY_SEPARATOR . '_testDir2'; + $this->filesPath = sprintf('%s%s%s', sys_get_temp_dir(), DIRECTORY_SEPARATOR, uniqid('zfilter')); + $this->targetPath = sprintf('%s%s%s', $this->filesPath, DIRECTORY_SEPARATOR, 'targetPath'); - $this->tearDown(); + mkdir($this->targetPath, 0775, true); - mkdir($this->_filesPath); - mkdir($this->_newDir); + $this->sourceFile = $this->filesPath . DIRECTORY_SEPARATOR . 'testfile.txt'; + $this->targetFile = $this->filesPath . DIRECTORY_SEPARATOR . 'newfile.xml'; + $this->targetPathFile = $this->targetPath . DIRECTORY_SEPARATOR . 'testfile.txt'; - $this->_oldFile = $this->_filesPath . DIRECTORY_SEPARATOR . 'testfile.txt'; - $this->_newFile = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile.xml'; - $this->_newDirFile = $this->_newDir . DIRECTORY_SEPARATOR . 'testfile.txt'; - - touch($this->_oldFile); + touch($this->sourceFile); } /** @@ -80,8 +77,7 @@ public function setUp() */ public function tearDown() { - $this->removeDir($this->_newDir); - $this->removeDir($this->_filesPath); + $this->removeDir($this->filesPath); } protected function removeDir($dir) @@ -93,6 +89,11 @@ protected function removeDir($dir) foreach (glob($dir . DIRECTORY_SEPARATOR . '*') as $file) { if (is_file($file)) { unlink($file); + continue; + } + if (is_dir($file)) { + $this->removeDir($file); + continue; } } @@ -106,13 +107,14 @@ protected function removeDir($dir) */ public function testThrowsExceptionWithNonUploadedFile() { - $filter = new FileRenameUpload($this->_newFile); - $this->assertEquals($this->_newFile, $filter->getTarget()); + $filter = new FileRenameUpload($this->targetFile); + $this->assertEquals($this->targetFile, $filter->getTarget()); $this->assertEquals('falsefile', $filter('falsefile')); $this->setExpectedException( - 'Zend\Filter\Exception\RuntimeException', 'could not be renamed' + 'Zend\Filter\Exception\RuntimeException', + 'could not be renamed' ); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->targetFile, $filter($this->sourceFile)); } /** @@ -120,19 +122,19 @@ public function testThrowsExceptionWithNonUploadedFile() */ public function testOptions() { - $filter = new FileRenameUpload($this->_newFile); - $this->assertEquals($this->_newFile, $filter->getTarget()); + $filter = new FileRenameUpload($this->targetFile); + $this->assertEquals($this->targetFile, $filter->getTarget()); $this->assertFalse($filter->getUseUploadName()); $this->assertFalse($filter->getOverwrite()); $this->assertFalse($filter->getRandomize()); $filter = new FileRenameUpload([ - 'target' => $this->_oldFile, + 'target' => $this->sourceFile, 'use_upload_name' => true, 'overwrite' => true, 'randomize' => true, ]); - $this->assertEquals($this->_oldFile, $filter->getTarget()); + $this->assertEquals($this->sourceFile, $filter->getTarget()); $this->assertTrue($filter->getUseUploadName()); $this->assertTrue($filter->getOverwrite()); $this->assertTrue($filter->getRandomize()); @@ -143,9 +145,9 @@ public function testOptions() */ public function testStringConstructorParam() { - $filter = new RenameUploadMock($this->_newFile); - $this->assertEquals($this->_newFile, $filter->getTarget()); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $filter = new RenameUploadMock($this->targetFile); + $this->assertEquals($this->targetFile, $filter->getTarget()); + $this->assertEquals($this->targetFile, $filter($this->sourceFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -154,16 +156,16 @@ public function testStringConstructorParam() */ public function testStringConstructorWithFilesArray() { - $filter = new RenameUploadMock($this->_newFile); - $this->assertEquals($this->_newFile, $filter->getTarget()); + $filter = new RenameUploadMock($this->targetFile); + $this->assertEquals($this->targetFile, $filter->getTarget()); $this->assertEquals( [ - 'tmp_name' => $this->_newFile, - 'name' => $this->_newFile, + 'tmp_name' => $this->targetFile, + 'name' => $this->targetFile, ], $filter([ - 'tmp_name' => $this->_oldFile, - 'name' => $this->_newFile, + 'tmp_name' => $this->sourceFile, + 'name' => $this->targetFile, ]) ); $this->assertEquals('falsefile', $filter('falsefile')); @@ -175,10 +177,10 @@ public function testStringConstructorWithFilesArray() public function testArrayConstructorParam() { $filter = new RenameUploadMock([ - 'target' => $this->_newFile, + 'target' => $this->targetFile, ]); - $this->assertEquals($this->_newFile, $filter->getTarget()); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->targetFile, $filter->getTarget()); + $this->assertEquals($this->targetFile, $filter($this->sourceFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -189,7 +191,7 @@ public function testConstructTruncatedTarget() { $filter = new FileRenameUpload('*'); $this->assertEquals('*', $filter->getTarget()); - $this->assertEquals($this->_oldFile, $filter($this->_oldFile)); + $this->assertEquals($this->sourceFile, $filter($this->sourceFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -198,9 +200,9 @@ public function testConstructTruncatedTarget() */ public function testTargetDirectory() { - $filter = new RenameUploadMock($this->_newDir); - $this->assertEquals($this->_newDir, $filter->getTarget()); - $this->assertEquals($this->_newDirFile, $filter($this->_oldFile)); + $filter = new RenameUploadMock($this->targetPath); + $this->assertEquals($this->targetPath, $filter->getTarget()); + $this->assertEquals($this->targetPathFile, $filter($this->sourceFile)); $this->assertEquals('falsefile', $filter('falsefile')); } @@ -210,14 +212,14 @@ public function testTargetDirectory() public function testOverwriteWithExistingFile() { $filter = new RenameUploadMock([ - 'target' => $this->_newFile, + 'target' => $this->targetFile, 'overwrite' => true, ]); - copy($this->_oldFile, $this->_newFile); + copy($this->sourceFile, $this->targetFile); - $this->assertEquals($this->_newFile, $filter->getTarget()); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->targetFile, $filter->getTarget()); + $this->assertEquals($this->targetFile, $filter($this->sourceFile)); } /** @@ -226,18 +228,19 @@ public function testOverwriteWithExistingFile() public function testCannotOverwriteExistingFile() { $filter = new RenameUploadMock([ - 'target' => $this->_newFile, + 'target' => $this->targetFile, 'overwrite' => false, ]); - copy($this->_oldFile, $this->_newFile); + copy($this->sourceFile, $this->targetFile); - $this->assertEquals($this->_newFile, $filter->getTarget()); + $this->assertEquals($this->targetFile, $filter->getTarget()); $this->assertFalse($filter->getOverwrite()); $this->setExpectedException( - 'Zend\Filter\Exception\InvalidArgumentException', 'already exists' + 'Zend\Filter\Exception\InvalidArgumentException', + 'already exists' ); - $this->assertEquals($this->_newFile, $filter($this->_oldFile)); + $this->assertEquals($this->targetFile, $filter($this->sourceFile)); } /** @@ -245,46 +248,46 @@ public function testCannotOverwriteExistingFile() */ public function testGetRandomizedFile() { - $fileNoExt = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile'; + $fileNoExt = $this->filesPath . DIRECTORY_SEPARATOR . 'newfile'; $filter = new RenameUploadMock([ - 'target' => $this->_newFile, + 'target' => $this->targetFile, 'randomize' => true, ]); - $this->assertRegExp('#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}\.xml#', $filter($this->_oldFile)); + $this->assertRegExp('#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}\.xml#', $filter($this->sourceFile)); } public function testGetFileWithOriginalExtension() { - $fileNoExt = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile'; + $fileNoExt = $this->filesPath . DIRECTORY_SEPARATOR . 'newfile'; $filter = new RenameUploadMock([ - 'target' => $this->_newFile, + 'target' => $this->targetFile, 'use_upload_extension' => true, 'randomize' => false, ]); - $oldFilePathInfo = pathinfo($this->_oldFile); + $oldFilePathInfo = pathinfo($this->sourceFile); $this->assertRegExp( '#' . str_replace('\\', '\\\\', $fileNoExt) . '.'.$oldFilePathInfo['extension'].'#', - $filter($this->_oldFile) + $filter($this->sourceFile) ); } public function testGetRandomizedFileWithOriginalExtension() { - $fileNoExt = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile'; + $fileNoExt = $this->filesPath . DIRECTORY_SEPARATOR . 'newfile'; $filter = new RenameUploadMock([ - 'target' => $this->_newFile, + 'target' => $this->targetFile, 'use_upload_extension' => true, 'randomize' => true, ]); - $oldFilePathInfo = pathinfo($this->_oldFile); + $oldFilePathInfo = pathinfo($this->sourceFile); $this->assertRegExp( '#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}\.'.$oldFilePathInfo['extension'].'#', - $filter($this->_oldFile) + $filter($this->sourceFile) ); } @@ -293,13 +296,13 @@ public function testGetRandomizedFileWithOriginalExtension() */ public function testGetRandomizedFileWithoutExtension() { - $fileNoExt = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile'; + $fileNoExt = $this->filesPath . DIRECTORY_SEPARATOR . 'newfile'; $filter = new RenameUploadMock([ 'target' => $fileNoExt, 'randomize' => true, ]); - $this->assertRegExp('#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}#', $filter($this->_oldFile)); + $this->assertRegExp('#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}#', $filter($this->sourceFile)); } /** @@ -317,15 +320,15 @@ public function testInvalidConstruction() public function testCanFilterMultipleTimesWithSameResult() { $filter = new RenameUploadMock([ - 'target' => $this->_newFile, + 'target' => $this->targetFile, 'randomize' => true, ]); - $firstResult = $filter($this->_oldFile); + $firstResult = $filter($this->sourceFile); $this->assertContains('newfile', $firstResult); - $secondResult = $filter($this->_oldFile); + $secondResult = $filter($this->sourceFile); $this->assertSame($firstResult, $secondResult); } @@ -337,7 +340,7 @@ public function returnUnfilteredDataProvider() [null], [new \stdClass()], [[ - $this->_oldFile, + $this->sourceFile, 'something invalid' ]] ]; @@ -350,7 +353,7 @@ public function returnUnfilteredDataProvider() public function testReturnUnfiltered($input) { $filter = new RenameUploadMock([ - 'target' => $this->_newFile, + 'target' => $this->targetFile, 'randomize' => true, ]); diff --git a/test/File/UpperCaseTest.php b/test/File/UpperCaseTest.php index d391aa31..046f5202 100644 --- a/test/File/UpperCaseTest.php +++ b/test/File/UpperCaseTest.php @@ -16,26 +16,12 @@ */ class UpperCaseTest extends \PHPUnit_Framework_TestCase { - /** - * Path to test files - * - * @var string - */ - protected $_filesPath; - - /** - * Original testfile - * - * @var string - */ - protected $_origFile; - /** * Testfile * * @var string */ - protected $_newFile; + protected $testFile; /** * Sets the path to test files @@ -44,13 +30,11 @@ class UpperCaseTest extends \PHPUnit_Framework_TestCase */ public function setUp() { - $this->_filesPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR; - $this->_origFile = $this->_filesPath . 'testfile2.txt'; - $this->_newFile = $this->_filesPath . 'newtestfile2.txt'; + $filesPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR; + $origFile = $filesPath . 'testfile2.txt'; + $this->testFile = sprintf('%s/%s.txt', sys_get_temp_dir(), uniqid('zfilter')); - if (!file_exists($this->_newFile)) { - copy($this->_origFile, $this->_newFile); - } + copy($origFile, $this->testFile); } /** @@ -60,8 +44,8 @@ public function setUp() */ public function tearDown() { - if (file_exists($this->_newFile)) { - unlink($this->_newFile); + if (file_exists($this->testFile)) { + unlink($this->testFile); } } @@ -70,10 +54,10 @@ public function tearDown() */ public function testInstanceCreationAndNormalWorkflow() { - $this->assertContains('This is a File', file_get_contents($this->_newFile)); + $this->assertContains('This is a File', file_get_contents($this->testFile)); $filter = new FileUpperCase(); - $filter($this->_newFile); - $this->assertContains('THIS IS A FILE', file_get_contents($this->_newFile)); + $filter($this->testFile); + $this->assertContains('THIS IS A FILE', file_get_contents($this->testFile)); } /** @@ -81,10 +65,10 @@ public function testInstanceCreationAndNormalWorkflow() */ public function testNormalWorkflowWithFilesArray() { - $this->assertContains('This is a File', file_get_contents($this->_newFile)); + $this->assertContains('This is a File', file_get_contents($this->testFile)); $filter = new FileUpperCase(); - $filter(['tmp_name' => $this->_newFile]); - $this->assertContains('THIS IS A FILE', file_get_contents($this->_newFile)); + $filter(['tmp_name' => $this->testFile]); + $this->assertContains('THIS IS A FILE', file_get_contents($this->testFile)); } /** @@ -94,7 +78,7 @@ public function testFileNotFoundException() { $filter = new FileUpperCase(); $this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'not found'); - $filter($this->_newFile . 'unknown'); + $filter($this->testFile . 'unknown'); } /** @@ -102,11 +86,11 @@ public function testFileNotFoundException() */ public function testCheckSettingOfEncodingInIstance() { - $this->assertContains('This is a File', file_get_contents($this->_newFile)); + $this->assertContains('This is a File', file_get_contents($this->testFile)); try { $filter = new FileUpperCase('ISO-8859-1'); - $filter($this->_newFile); - $this->assertContains('THIS IS A FILE', file_get_contents($this->_newFile)); + $filter($this->testFile); + $this->assertContains('THIS IS A FILE', file_get_contents($this->testFile)); } catch (\Zend\Filter\Exception\ExtensionNotLoadedException $e) { $this->assertContains('mbstring is required', $e->getMessage()); } @@ -117,12 +101,12 @@ public function testCheckSettingOfEncodingInIstance() */ public function testCheckSettingOfEncodingWithMethod() { - $this->assertContains('This is a File', file_get_contents($this->_newFile)); + $this->assertContains('This is a File', file_get_contents($this->testFile)); try { $filter = new FileUpperCase(); $filter->setEncoding('ISO-8859-1'); - $filter($this->_newFile); - $this->assertContains('THIS IS A FILE', file_get_contents($this->_newFile)); + $filter($this->testFile); + $this->assertContains('THIS IS A FILE', file_get_contents($this->testFile)); } catch (\Zend\Filter\Exception\ExtensionNotLoadedException $e) { $this->assertContains('mbstring is required', $e->getMessage()); } @@ -134,7 +118,7 @@ public function returnUnfilteredDataProvider() [null], [new \stdClass()], [[ - $this->_newFile, + $this->testFile, 'something invalid' ]] ]; diff --git a/test/FilterChainTest.php b/test/FilterChainTest.php index 78810723..c8d9aa08 100644 --- a/test/FilterChainTest.php +++ b/test/FilterChainTest.php @@ -9,8 +9,12 @@ namespace ZendTest\Filter; -use Zend\Filter\FilterChain; use Zend\Filter\AbstractFilter; +use Zend\Filter\FilterChain; +use Zend\Filter\PregReplace; +use Zend\Filter\StringToLower; +use Zend\Filter\StringTrim; +use Zend\Filter\StripTags; /** * @group Zend_Filter @@ -28,8 +32,8 @@ public function testFiltersAreExecutedInFifoOrder() { $chain = new FilterChain(); $chain->attach(new LowerCase()) - ->attach(new StripUpperCase()); - $value = 'AbC'; + ->attach(new StripUpperCase()); + $value = 'AbC'; $valueExpected = 'abc'; $this->assertEquals($valueExpected, $chain->filter($value)); } @@ -38,8 +42,8 @@ public function testFiltersAreExecutedAccordingToPriority() { $chain = new FilterChain(); $chain->attach(new StripUpperCase()) - ->attach(new LowerCase, 100); - $value = 'AbC'; + ->attach(new LowerCase, 100); + $value = 'AbC'; $valueExpected = 'b'; $this->assertEquals($valueExpected, $chain->filter($value)); } @@ -61,10 +65,11 @@ public function testAllowsConnectingViaClassShortName() } $chain = new FilterChain(); - $chain->attachByName('string_trim', null, 100) - ->attachByName('strip_tags') - ->attachByName('string_to_lower', ['encoding' => 'utf-8'], 900); - $value = ' ABC '; + $chain->attachByName(StringTrim::class, null, 100) + ->attachByName(StripTags::class) + ->attachByName(StringToLower::class, ['encoding' => 'utf-8'], 900); + + $value = ' ABC '; $valueExpected = 'abc'; $this->assertEquals($valueExpected, $chain->filter($value)); } @@ -74,33 +79,33 @@ public function testAllowsConfiguringFilters() $config = $this->getChainConfig(); $chain = new FilterChain(); $chain->setOptions($config); - $value = ' abc '; + $value = ' abc '; $valueExpected = 'ABC '; $this->assertEquals($valueExpected, $chain->filter($value)); } public function testAllowsConfiguringFiltersViaConstructor() { - $config = $this->getChainConfig(); - $chain = new FilterChain($config); - $value = ' abc '; + $config = $this->getChainConfig(); + $chain = new FilterChain($config); + $value = ' abc '; $valueExpected = 'ABC'; $this->assertEquals($valueExpected, $chain->filter($value)); } public function testConfigurationAllowsTraversableObjects() { - $config = $this->getChainConfig(); - $config = new \ArrayIterator($config); - $chain = new FilterChain($config); - $value = ' abc '; + $config = $this->getChainConfig(); + $config = new \ArrayIterator($config); + $chain = new FilterChain($config); + $value = ' abc '; $valueExpected = 'ABC'; $this->assertEquals($valueExpected, $chain->filter($value)); } public function testCanRetrieveFilterWithUndefinedConstructor() { - $chain = new FilterChain([ + $chain = new FilterChain([ 'filters' => [ ['name' => 'int'], ], @@ -114,12 +119,19 @@ protected function getChainConfig() return [ 'callbacks' => [ ['callback' => __CLASS__ . '::staticUcaseFilter'], - ['priority' => 10000, 'callback' => function ($value) { - return trim($value); - }], + [ + 'priority' => 10000, + 'callback' => function ($value) { + return trim($value); + } + ], ], - 'filters' => [ - ['name' => 'strip_tags', 'options' => ['allowTags' => 'img', 'allowAttribs' => 'id'], 'priority' => 10100], + 'filters' => [ + [ + 'name' => StripTags::class, + 'options' => ['allowTags' => 'img', 'allowAttribs' => 'id'], + 'priority' => 10100 + ], ], ]; } @@ -135,11 +147,11 @@ public static function staticUcaseFilter($value) public function testCanAttachMultipleFiltersOfTheSameTypeAsDiscreteInstances() { $chain = new FilterChain(); - $chain->attachByName('PregReplace', [ + $chain->attachByName(PregReplace::class, [ 'pattern' => '/Foo/', 'replacement' => 'Bar', ]); - $chain->attachByName('PregReplace', [ + $chain->attachByName(PregReplace::class, [ 'pattern' => '/Bar/', 'replacement' => 'PARTY', ]); @@ -160,7 +172,7 @@ public function testClone() $chain = new FilterChain(); $clone = clone $chain; - $chain->attachByName('strip_tags'); + $chain->attachByName(StripTags::class); $this->assertCount(0, $clone); } @@ -169,7 +181,7 @@ public function testCanSerializeFilterChain() { $chain = new FilterChain(); $chain->attach(new LowerCase()) - ->attach(new StripUpperCase()); + ->attach(new StripUpperCase()); $serialized = serialize($chain); $unserialized = unserialize($serialized); @@ -187,12 +199,12 @@ public function testMergingTwoFilterChainsKeepFiltersPriority() $chain = new FilterChain(); $chain->attach(new StripUpperCase()) - ->attach(new LowerCase(), 1001); + ->attach(new LowerCase(), 1001); $this->assertEquals($valueExpected, $chain->filter($value)); $chain = new FilterChain(); $chain->attach(new LowerCase(), 1001) - ->attach(new StripUpperCase()); + ->attach(new StripUpperCase()); $this->assertEquals($valueExpected, $chain->filter($value)); $chain = new FilterChain(); diff --git a/test/FilterPluginManagerTest.php b/test/FilterPluginManagerTest.php index 1e3bbf65..fa59e4bd 100644 --- a/test/FilterPluginManagerTest.php +++ b/test/FilterPluginManagerTest.php @@ -10,15 +10,23 @@ namespace ZendTest\Filter; use Zend\Filter\FilterPluginManager; +use Zend\Filter\Word\SeparatorToSeparator; +use Zend\ServiceManager\Exception\InvalidServiceException; +use Zend\ServiceManager\ServiceManager; /** * @group Zend_Filter */ class FilterPluginManagerTest extends \PHPUnit_Framework_TestCase { + /** + * @var FilterPluginManager + */ + private $filters; + public function setUp() { - $this->filters = new FilterPluginManager(); + $this->filters = new FilterPluginManager(new ServiceManager()); } public function testFilterSuccessfullyRetrieved() @@ -29,14 +37,15 @@ public function testFilterSuccessfullyRetrieved() public function testRegisteringInvalidFilterRaisesException() { - $this->setExpectedException('Zend\Filter\Exception\RuntimeException'); + $this->setExpectedException(InvalidServiceException::class); $this->filters->setService('test', $this); + $this->filters->get('test'); } public function testLoadingInvalidFilterRaisesException() { $this->filters->setInvokableClass('test', get_class($this)); - $this->setExpectedException('Zend\Filter\Exception\RuntimeException'); + $this->setExpectedException(InvalidServiceException::class); $this->filters->get('test'); } @@ -45,16 +54,19 @@ public function testLoadingInvalidFilterRaisesException() */ public function testFilterSuccessfullyConstructed() { - $search_separator = ';'; - $replacement_separator = '|'; + $searchSeparator = ';'; + $replacementSeparator = '|'; + $options = [ - 'search_separator' => $search_separator, - 'replacement_separator' => $replacement_separator, + 'search_separator' => $searchSeparator, + 'replacement_separator' => $replacementSeparator, ]; - $filter = $this->filters->get('wordseparatortoseparator', $options); - $this->assertInstanceOf('Zend\Filter\Word\SeparatorToSeparator', $filter); - $this->assertEquals(';', $filter->getSearchSeparator()); - $this->assertEquals('|', $filter->getReplacementSeparator()); + + $filter = $this->filters->build('wordseparatortoseparator', $options); + + $this->assertInstanceOf(SeparatorToSeparator::class, $filter); + $this->assertEquals($searchSeparator, $filter->getSearchSeparator()); + $this->assertEquals($replacementSeparator, $filter->getReplacementSeparator()); } /** @@ -69,6 +81,7 @@ public function testFiltersConstructedAreDifferent() 'replacement_separator' => '|', ] ); + $filterTwo = $this->filters->get( 'wordseparatortoseparator', [ diff --git a/test/InflectorTest.php b/test/InflectorTest.php index 50baaaa7..ae4a5ba6 100644 --- a/test/InflectorTest.php +++ b/test/InflectorTest.php @@ -9,8 +9,15 @@ namespace ZendTest\Filter; -use Zend\Filter\Inflector as InflectorFilter; use Zend\Filter\FilterPluginManager; +use Zend\Filter\Inflector as InflectorFilter; +use Zend\Filter\PregReplace; +use Zend\Filter\StringToLower; +use Zend\Filter\StringToUpper; +use Zend\Filter\Word\CamelCaseToDash; +use Zend\Filter\Word\CamelCaseToUnderscore; +use Zend\I18n\Filter\Alpha; +use Zend\ServiceManager\ServiceManager; /** * Test class for Zend\Filter\Inflector. @@ -50,7 +57,7 @@ public function testGetPluginManagerReturnsFilterManagerByDefault() public function testSetPluginManagerAllowsSettingAlternatePluginManager() { $defaultManager = $this->inflector->getPluginManager(); - $manager = new FilterPluginManager(); + $manager = new FilterPluginManager(new ServiceManager()); $this->inflector->setPluginManager($manager); $receivedManager = $this->inflector->getPluginManager(); $this->assertNotSame($defaultManager, $receivedManager); @@ -87,7 +94,7 @@ public function testSetFilterRuleWithStringRuleCreatesRuleEntryAndFilterObject() { $rules = $this->inflector->getRules(); $this->assertEquals(0, count($rules)); - $this->inflector->setFilterRule('controller', 'PregReplace'); + $this->inflector->setFilterRule('controller', PregReplace::class); $rules = $this->inflector->getRules('controller'); $this->assertEquals(1, count($rules)); $filter = $rules[0]; @@ -98,7 +105,7 @@ public function testSetFilterRuleWithFilterObjectCreatesRuleEntryWithFilterObjec { $rules = $this->inflector->getRules(); $this->assertEquals(0, count($rules)); - $filter = new \Zend\Filter\PregReplace(); + $filter = new PregReplace(); $this->inflector->setFilterRule('controller', $filter); $rules = $this->inflector->getRules('controller'); $this->assertEquals(1, count($rules)); @@ -115,7 +122,7 @@ public function testAddFilterRuleAppendsRuleEntries() $rules = $this->inflector->getRules(); $this->assertEquals(0, count($rules)); - $this->inflector->setFilterRule('controller', ['PregReplace', 'Alpha']); + $this->inflector->setFilterRule('controller', [PregReplace::class, Alpha::class]); $rules = $this->inflector->getRules('controller'); $this->assertEquals(2, count($rules)); $this->assertInstanceOf('Zend\Filter\FilterInterface', $rules[0]); @@ -165,7 +172,7 @@ public function testAddRulesCreatesAppropriateRuleEntries() $rules = $this->inflector->getRules(); $this->assertEquals(0, count($rules)); $this->inflector->addRules([ - ':controller' => ['PregReplace', 'Alpha'], + ':controller' => [PregReplace::class, Alpha::class], 'suffix' => 'phtml', ]); $rules = $this->inflector->getRules(); @@ -184,7 +191,7 @@ public function testSetRulesCreatesAppropriateRuleEntries() $rules = $this->inflector->getRules(); $this->assertEquals(1, count($rules)); $this->inflector->setRules([ - ':controller' => ['PregReplace', 'Alpha'], + ':controller' => [PregReplace::class, Alpha::class], 'suffix' => 'phtml', ]); $rules = $this->inflector->getRules(); @@ -199,20 +206,22 @@ public function testGetRule() $this->markTestSkipped('ext/intl not enabled'); } - $this->inflector->setFilterRule(':controller', ['Alpha', 'StringToLower']); + $this->inflector->setFilterRule(':controller', [Alpha::class, StringToLower::class]); $this->assertInstanceOf('Zend\Filter\StringToLower', $this->inflector->getRule('controller', 1)); $this->assertFalse($this->inflector->getRule('controller', 2)); } public function testFilterTransformsStringAccordingToRules() { - $this->inflector->setTarget(':controller/:action.:suffix') - ->addRules([ - ':controller' => ['Word\\CamelCaseToDash'], - ':action' => ['Word\\CamelCaseToDash'], - 'suffix' => 'phtml' - ]); - $filter = $this->inflector; + $this->inflector + ->setTarget(':controller/:action.:suffix') + ->addRules([ + ':controller' => [CamelCaseToDash::class], + ':action' => [CamelCaseToDash::class], + 'suffix' => 'phtml' + ]); + + $filter = $this->inflector; $filtered = $filter([ 'controller' => 'FooBar', 'action' => 'bazBat' @@ -232,13 +241,13 @@ public function testTargetReplacementIdentiferWorksWhenInflected() $inflector = new InflectorFilter( '?=##controller/?=##action.?=##suffix', [ - ':controller' => ['Word\\CamelCaseToDash'], - ':action' => ['Word\\CamelCaseToDash'], - 'suffix' => 'phtml' - ], + ':controller' => [CamelCaseToDash::class], + ':action' => [CamelCaseToDash::class], + 'suffix' => 'phtml' + ], null, '?=##' - ); + ); $filtered = $inflector([ 'controller' => 'FooBar', @@ -267,13 +276,13 @@ public function testTargetExceptionThrownWhenTargetSourceNotSatisfied() $inflector = new InflectorFilter( '?=##controller/?=##action.?=##suffix', [ - ':controller' => ['Word\\CamelCaseToDash'], - ':action' => ['Word\\CamelCaseToDash'], - 'suffix' => 'phtml' - ], + ':controller' => [CamelCaseToDash::class], + ':action' => [CamelCaseToDash::class], + 'suffix' => 'phtml' + ], true, '?=##' - ); + ); $this->setExpectedException('\Zend\Filter\Exception\RuntimeException', 'perhaps a rule was not satisfied'); $filtered = $inflector(['controller' => 'FooBar']); @@ -284,13 +293,13 @@ public function testTargetExceptionNotThrownOnIdentifierNotFollowedByCharacter() $inflector = new InflectorFilter( 'e:\path\to\:controller\:action.:suffix', [ - ':controller' => ['Word\\CamelCaseToDash', 'StringToLower'], - ':action' => ['Word\\CamelCaseToDash'], - 'suffix' => 'phtml' - ], + ':controller' => [CamelCaseToDash::class, StringToLower::class], + ':action' => [CamelCaseToDash::class], + 'suffix' => 'phtml' + ], true, ':' - ); + ); $filtered = $inflector(['controller' => 'FooBar', 'action' => 'MooToo']); $this->assertEquals($filtered, 'e:\path\to\foo-bar\Moo-Too.phtml'); @@ -299,27 +308,29 @@ public function testTargetExceptionNotThrownOnIdentifierNotFollowedByCharacter() public function getOptions() { $options = [ - 'target' => '$controller/$action.$suffix', - 'throwTargetExceptionsOn' => true, + 'target' => '$controller/$action.$suffix', + 'throwTargetExceptionsOn' => true, 'targetReplacementIdentifier' => '$', - 'rules' => [ + 'rules' => [ ':controller' => [ - 'rule1' => 'Word\\CamelCaseToUnderscore', - 'rule2' => 'StringToLower', + 'rule1' => CamelCaseToUnderscore::class, + 'rule2' => StringToLower::class, ], - ':action' => [ - 'rule1' => 'Word\\CamelCaseToDash', - 'rule2' => 'StringToUpper', + ':action' => [ + 'rule1' => CamelCaseToDash::class, + 'rule2' => StringToUpper::class, ], - 'suffix' => 'php' + 'suffix' => 'php' ], ]; + return $options; } public function getConfig() { $options = $this->getOptions(); + return new \Zend\Config\Config($options); } @@ -347,14 +358,14 @@ protected function _testOptions($inflector) public function testPassingConfigObjectToConstructorSetsStateAndRules() { - $config = $this->getConfig(); + $config = $this->getConfig(); $inflector = new InflectorFilter($config); $this->_testOptions($inflector); } public function testSetConfigSetsStateAndRules() { - $config = $this->getConfig(); + $config = $this->getConfig(); $inflector = new InflectorFilter(); $inflector->setOptions($config); $this->_testOptions($inflector); @@ -370,21 +381,22 @@ public function testCheckInflectorWithPregBackreferenceLikeParts() $inflector = new InflectorFilter( ':moduleDir' . DIRECTORY_SEPARATOR . ':controller' . DIRECTORY_SEPARATOR . ':action.:suffix', [ - ':controller' => ['Word\\CamelCaseToDash', 'StringToLower'], - ':action' => ['Word\\CamelCaseToDash'], + ':controller' => [CamelCaseToDash::class, StringToLower::class], + ':action' => [CamelCaseToDash::class], 'suffix' => 'phtml' - ], + ], true, ':' - ); + ); $inflector->setStaticRule('moduleDir', 'C:\htdocs\public\cache\00\01\42\app\modules'); $filtered = $inflector([ 'controller' => 'FooBar', - 'action' => 'MooToo' - ]); - $this->assertEquals($filtered, 'C:\htdocs\public\cache\00\01\42\app\modules' . DIRECTORY_SEPARATOR . 'foo-bar' . DIRECTORY_SEPARATOR . 'Moo-Too.phtml'); + 'action' => 'MooToo' + ]); + $this->assertEquals($filtered, + 'C:\htdocs\public\cache\00\01\42\app\modules' . DIRECTORY_SEPARATOR . 'foo-bar' . DIRECTORY_SEPARATOR . 'Moo-Too.phtml'); } /** @@ -420,15 +432,15 @@ public function testAddFilterRuleMultipleTimes() $rules = $this->inflector->getRules(); $this->assertEquals(0, count($rules)); - $this->inflector->setFilterRule('controller', 'PregReplace'); + $this->inflector->setFilterRule('controller', PregReplace::class); $rules = $this->inflector->getRules('controller'); $this->assertEquals(1, count($rules)); - $this->inflector->addFilterRule('controller', ['Alpha', 'StringToLower']); + $this->inflector->addFilterRule('controller', [Alpha::class, StringToLower::class]); $rules = $this->inflector->getRules('controller'); $this->assertEquals(3, count($rules)); - $this->_context = 'StringToLower'; + $this->_context = StringToLower::class; $this->inflector->setStaticRuleReference('context', $this->_context); - $this->inflector->addFilterRule('controller', ['Alpha', 'StringToLower']); + $this->inflector->addFilterRule('controller', [Alpha::class, StringToLower::class]); $rules = $this->inflector->getRules('controller'); $this->assertEquals(5, count($rules)); } @@ -438,7 +450,7 @@ public function testAddFilterRuleMultipleTimes() */ public function testPassingArrayToConstructorSetsStateAndRules() { - $options = $this->getOptions(); + $options = $this->getOptions(); $inflector = new InflectorFilter($options); } @@ -447,7 +459,7 @@ public function testPassingArrayToConstructorSetsStateAndRules() */ public function testPassingArrayToSetConfigSetsStateAndRules() { - $options = $this->getOptions(); + $options = $this->getOptions(); $inflector = new InflectorFilter(); $inflector->setOptions($options); $this->_testOptions($inflector); @@ -458,7 +470,7 @@ public function testPassingArrayToSetConfigSetsStateAndRules() */ public function testPassingZendConfigObjectToConstructorSetsStateAndRules() { - $config = $this->getConfig(); + $config = $this->getConfig(); $inflector = new InflectorFilter($config); $this->_testOptions($inflector); } @@ -468,7 +480,7 @@ public function testPassingZendConfigObjectToConstructorSetsStateAndRules() */ public function testPassingZendConfigObjectToSetConfigSetsStateAndRules() { - $config = $this->getConfig(); + $config = $this->getConfig(); $inflector = new InflectorFilter(); $inflector->setOptions($config); $this->_testOptions($inflector); diff --git a/test/StaticFilterTest.php b/test/StaticFilterTest.php index 50d27cd9..c79564aa 100644 --- a/test/StaticFilterTest.php +++ b/test/StaticFilterTest.php @@ -9,8 +9,12 @@ namespace ZendTest\Filter; +use Zend\Filter\Callback; +use Zend\Filter\Digits; +use Zend\Filter\HtmlEntities; use Zend\Filter\StaticFilter; use Zend\Filter\FilterPluginManager; +use Zend\ServiceManager\ServiceManager; /** * @group Zend_Filter @@ -35,14 +39,14 @@ public function testUsesFilterPluginManagerByDefault() public function testCanSpecifyCustomPluginManager() { - $plugins = new FilterPluginManager(); + $plugins = new FilterPluginManager(new ServiceManager()); StaticFilter::setPluginManager($plugins); $this->assertSame($plugins, StaticFilter::getPluginManager()); } public function testCanResetPluginManagerByPassingNull() { - $plugins = new FilterPluginManager(); + $plugins = new FilterPluginManager(new ServiceManager()); StaticFilter::setPluginManager($plugins); $this->assertSame($plugins, StaticFilter::getPluginManager()); StaticFilter::setPluginManager(null); @@ -58,7 +62,7 @@ public function testCanResetPluginManagerByPassingNull() */ public function testStaticFactory() { - $filteredValue = StaticFilter::execute('1a2b3c4d', 'Digits'); + $filteredValue = StaticFilter::execute('1a2b3c4d', Digits::class); $this->assertEquals('1234', $filteredValue); } @@ -69,13 +73,13 @@ public function testStaticFactory() public function testStaticFactoryWithConstructorArguments() { // Test HtmlEntities with one ctor argument. - $filteredValue = StaticFilter::execute('"O\'Reilly"', 'HtmlEntities', ['quotestyle' => ENT_COMPAT]); + $filteredValue = StaticFilter::execute('"O\'Reilly"', HtmlEntities::class, ['quotestyle' => ENT_COMPAT]); $this->assertEquals('"O\'Reilly"', $filteredValue); // Test HtmlEntities with a different ctor argument, // and make sure it gives the correct response // so we know it passed the arg to the ctor. - $filteredValue = StaticFilter::execute('"O\'Reilly"', 'HtmlEntities', ['quotestyle' => ENT_QUOTES]); + $filteredValue = StaticFilter::execute('"O\'Reilly"', HtmlEntities::class, ['quotestyle' => ENT_QUOTES]); $this->assertEquals('"O'Reilly"', $filteredValue); } @@ -95,12 +99,12 @@ public function testStaticFactoryClassNotFound() public function testUsesDifferentConfigurationOnEachRequest() { - $first = StaticFilter::execute('foo', 'callback', [ + $first = StaticFilter::execute('foo', Callback::class, [ 'callback' => function ($value) { return 'FOO'; }, ]); - $second = StaticFilter::execute('foo', 'callback', [ + $second = StaticFilter::execute('foo', Callback::class, [ 'callback' => function ($value) { return 'BAR'; }, diff --git a/test/WhitelistTest.php b/test/WhitelistTest.php index 53de1f05..fb84879d 100644 --- a/test/WhitelistTest.php +++ b/test/WhitelistTest.php @@ -11,6 +11,7 @@ use Zend\Filter\FilterPluginManager; use Zend\Filter\Whitelist as WhitelistFilter; +use Zend\ServiceManager\ServiceManager; use Zend\Stdlib\ArrayObject; /** @@ -39,7 +40,7 @@ public function testConstructorDefaults() public function testWithPluginManager() { - $pluginManager = new FilterPluginManager(); + $pluginManager = new FilterPluginManager(new ServiceManager()); $filter = $pluginManager->get('whitelist'); $this->assertInstanceOf('Zend\Filter\Whitelist', $filter);