From 23711e1bdc42ec4346043e208c7eae6f39280c51 Mon Sep 17 00:00:00 2001 From: David Yell Date: Thu, 1 Aug 2019 12:36:22 +0100 Subject: [PATCH 1/6] Updated the installation instructions --- docs/installation.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/installation.md b/docs/installation.md index ef07129..ffc2997 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -17,10 +17,16 @@ It's always advised to lock your dependencies to a specific version number. You :warning: Installing the plugin without the use of Composer is unsupported, you do so at your own risk. ## CakePHP -Then you'll need to load the plugin in your `config/bootstrap.php` file. +Then you'll need to load the plugin in your `src/Application.php` file. ```php -Plugin::load('Proffer'); +$this->addPlugin('Proffer'); +``` + +or you can use the console to do this for you. + +```bash +bin/cake plugin load Proffer ``` ## Database From 36db79ed012505c01118e5e47c2f6011e1703a40 Mon Sep 17 00:00:00 2001 From: David Yell Date: Thu, 1 Aug 2019 12:36:39 +0100 Subject: [PATCH 2/6] Updated some of the development dependancies --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 71d2f53..cec0f3f 100644 --- a/composer.json +++ b/composer.json @@ -17,14 +17,14 @@ "source": "https://github.com/davidyell/CakePHP3-Proffer" }, "require": { - "php": ">=5.6", + "php": ">=5.6.0", "cakephp/orm": "3.*", "intervention/image": "^2.3" }, "require-dev": { - "phpunit/phpunit": "^5.5", - "cakephp/cakephp": "^3.4.0", - "cakephp/cakephp-codesniffer": "^2.0" + "phpunit/phpunit": "^5|^6", + "cakephp/cakephp": "~3.4", + "cakephp/cakephp-codesniffer": "~3.0" }, "autoload": { "psr-4": { From 0572f4fb79d9f1814f93e2ea86db5acff8ef8e58 Mon Sep 17 00:00:00 2001 From: David Yell Date: Thu, 1 Aug 2019 12:36:59 +0100 Subject: [PATCH 3/6] Fixed deprecations in the test suite --- tests/TestCase/Lib/ProfferPathTest.php | 11 +++++-- .../Model/Behavior/ProfferBehaviorTest.php | 33 +++++++------------ .../Model/Validation/ProfferRulesTest.php | 12 ++++--- tests/bootstrap.php | 2 -- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/tests/TestCase/Lib/ProfferPathTest.php b/tests/TestCase/Lib/ProfferPathTest.php index bfb1cc0..a8a1c94 100644 --- a/tests/TestCase/Lib/ProfferPathTest.php +++ b/tests/TestCase/Lib/ProfferPathTest.php @@ -8,10 +8,10 @@ use Cake\Core\Plugin; use Cake\ORM\Entity; -use PHPUnit_Framework_TestCase; +use Cake\TestSuite\TestCase; use Proffer\Lib\ProfferPath; -class ProfferPathTest extends PHPUnit_Framework_TestCase +class ProfferPathTest extends TestCase { /** @@ -37,6 +37,13 @@ protected function _rrmdir($dir) } } + public function setUp() + { + parent::setUp(); + + $this->loadPlugins(['Proffer' => ['path' => ROOT]]); + } + /** * Clear up any generated images after each test * diff --git a/tests/TestCase/Model/Behavior/ProfferBehaviorTest.php b/tests/TestCase/Model/Behavior/ProfferBehaviorTest.php index 305d092..cc6973d 100644 --- a/tests/TestCase/Model/Behavior/ProfferBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/ProfferBehaviorTest.php @@ -15,8 +15,8 @@ use Cake\Event\EventManager; use Cake\ORM\Entity; use Cake\ORM\Table; +use Cake\TestSuite\TestCase; use Cake\Validation\Validator; -use PHPUnit_Framework_TestCase; use Proffer\Lib\ProfferPath; use Proffer\Model\Behavior\ProfferBehavior; use Proffer\Tests\Stubs\TestPath; @@ -26,7 +26,7 @@ * * @package Proffer\Tests\Model\Behavior */ -class ProfferBehaviorTest extends PHPUnit_Framework_TestCase +class ProfferBehaviorTest extends TestCase { private $config = [ @@ -45,6 +45,10 @@ class ProfferBehaviorTest extends PHPUnit_Framework_TestCase */ public function setUp() { + $this->loadPlugins([ + 'Proffer' => ['path' => ROOT] + ]); + $this->config['photo']['root'] = TMP . 'ProfferTests' . DS; } @@ -209,11 +213,11 @@ public function testBeforeMarshal(array $data, $allowEmpty, array $expected) public function validFileProvider() { return [ - [ + 'landscape image' => [ [ 'photo' => [ 'name' => 'image_640x480.jpg', - 'tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg', + 'tmp_name' => ROOT . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg', 'size' => 33000, 'error' => UPLOAD_ERR_OK ], @@ -224,26 +228,11 @@ public function validFileProvider() 'dir' => 'proffer_test' ] ], - [ - [ - 'photo' => [ - 'name' => 'image_480x640.jpg', - 'tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_480x640.jpg', - 'size' => 45704, - 'error' => UPLOAD_ERR_OK - ], - 'photo_dir' => 'proffer_test' - ], - [ - 'filename' => 'image_480x640.jpg', - 'dir' => 'proffer_test' - ] - ], - [ + 'portrait image' => [ [ 'photo' => [ 'name' => 'image_480x640.jpg', - 'tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_480x640.jpg', + 'tmp_name' => ROOT . 'tests' . DS . 'Fixture' . DS . 'image_480x640.jpg', 'size' => 45704, 'error' => UPLOAD_ERR_OK ], @@ -253,7 +242,7 @@ public function validFileProvider() 'filename' => 'image_480x640.jpg', 'dir' => 'proffer_test' ] - ], + ] ]; } diff --git a/tests/TestCase/Model/Validation/ProfferRulesTest.php b/tests/TestCase/Model/Validation/ProfferRulesTest.php index 6063641..415551a 100644 --- a/tests/TestCase/Model/Validation/ProfferRulesTest.php +++ b/tests/TestCase/Model/Validation/ProfferRulesTest.php @@ -3,16 +3,18 @@ namespace Proffer\Tests\Model\Validation; use Cake\Core\Plugin; -use PHPUnit_Framework_TestCase; +use Cake\TestSuite\TestCase; use Proffer\Model\Validation\ProfferRules; -class ProfferRulesTest extends PHPUnit_Framework_TestCase +class ProfferRulesTest extends TestCase { private $Rules; public function setUp() { + $this->loadPlugins(['Proffer' => ['path' => ROOT]]); + $this->Rules = new ProfferRules; } @@ -20,7 +22,7 @@ public function providerDimensions() { return [ [ - ['tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg'], + ['tmp_name' => ROOT . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg'], [ 'min' => ['w' => 100, 'h' => 100], 'max' => ['w' => 500, 'h' => 500] @@ -28,7 +30,7 @@ public function providerDimensions() false ], [ - ['tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg'], + ['tmp_name' => ROOT . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg'], [ 'min' => ['w' => 700, 'h' => 500], 'max' => ['w' => 1000, 'h' => 800] @@ -36,7 +38,7 @@ public function providerDimensions() false ], [ - ['tmp_name' => Plugin::path('Proffer') . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg'], + ['tmp_name' => ROOT . 'tests' . DS . 'Fixture' . DS . 'image_640x480.jpg'], [ 'min' => ['w' => 100, 'h' => 100], 'max' => ['w' => 700, 'h' => 700] diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9a9603a..b0fb010 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -102,5 +102,3 @@ 'file' => 'error', ] ]); - -Plugin::load('Proffer', ['path' => ROOT]); From 9f187722dee6065c133f60e2557028fbf5ad8d25 Mon Sep 17 00:00:00 2001 From: David Yell Date: Thu, 1 Aug 2019 14:15:10 +0100 Subject: [PATCH 4/6] Fixed incorrect test case usage of the class --- tests/TestCase/Model/Validation/ProfferRulesTest.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/TestCase/Model/Validation/ProfferRulesTest.php b/tests/TestCase/Model/Validation/ProfferRulesTest.php index 415551a..9074400 100644 --- a/tests/TestCase/Model/Validation/ProfferRulesTest.php +++ b/tests/TestCase/Model/Validation/ProfferRulesTest.php @@ -2,20 +2,14 @@ namespace Proffer\Tests\Model\Validation; -use Cake\Core\Plugin; use Cake\TestSuite\TestCase; use Proffer\Model\Validation\ProfferRules; class ProfferRulesTest extends TestCase { - - private $Rules; - public function setUp() { $this->loadPlugins(['Proffer' => ['path' => ROOT]]); - - $this->Rules = new ProfferRules; } public function providerDimensions() @@ -53,7 +47,7 @@ public function providerDimensions() */ public function testDimensions($value, $dimensions, $expected) { - $result = $this->Rules->dimensions($value, $dimensions); + $result = ProfferRules::dimensions($value, $dimensions); $this->assertEquals($expected, $result); } } From db3bd0c460072cda4f43f2561cf30823067a5ff1 Mon Sep 17 00:00:00 2001 From: David Yell Date: Thu, 1 Aug 2019 16:18:41 +0100 Subject: [PATCH 5/6] Code quality fixes --- composer.json | 9 ++++- phpstan.neon | 3 ++ src/Lib/ImageTransform.php | 6 +++- src/Lib/ProfferPath.php | 16 ++++++--- src/Model/Behavior/ProfferBehavior.php | 15 ++++---- src/Shell/ProfferShell.php | 34 +++++++++++++------ .../Model/Behavior/ProfferBehaviorTest.php | 2 +- 7 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index cec0f3f..2060d09 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "require-dev": { "phpunit/phpunit": "^5|^6", "cakephp/cakephp": "~3.4", - "cakephp/cakephp-codesniffer": "~3.0" + "cakephp/cakephp-codesniffer": "~3.0", + "phpstan/phpstan": "^0.11.12" }, "autoload": { "psr-4": { @@ -37,5 +38,11 @@ "Proffer\\Tests\\Fixture\\": "tests/Fixture", "Proffer\\Tests\\Stubs\\": "tests/Stubs" } + }, + "scripts": { + "cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", + "cs-fix": "phpcbf --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", + "test": "phpunit --colors=always", + "phpstan": "phpstan analyse --level=6 --memory-limit=512M --configuration=phpstan.neon ./src" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..23dd29f --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,3 @@ +parameters: + autoload_files: + - tests/bootstrap.php diff --git a/src/Lib/ImageTransform.php b/src/Lib/ImageTransform.php index ee8cd60..79cdb2c 100644 --- a/src/Lib/ImageTransform.php +++ b/src/Lib/ImageTransform.php @@ -172,7 +172,11 @@ protected function thumbnailResize(Image $image, $width, $height) */ protected function thumbnailCustom(Image $image, $custom, $params) { - return call_user_func_array([$image, $custom], $params); + if (method_exists($image, $custom)) { + return call_user_func_array($image->{$custom}(), $params); + } + + return $image; } /** diff --git a/src/Lib/ProfferPath.php b/src/Lib/ProfferPath.php index 45225fd..ca24946 100644 --- a/src/Lib/ProfferPath.php +++ b/src/Lib/ProfferPath.php @@ -8,7 +8,7 @@ namespace Proffer\Lib; -use Cake\ORM\Entity; +use Cake\Datasource\EntityInterface; use Cake\ORM\Table; use Cake\Utility\Text; @@ -31,11 +31,11 @@ class ProfferPath implements ProfferPathInterface * Construct the class and setup the defaults * * @param Table $table Instance of the table - * @param Entity $entity Instance of the entity data + * @param EntityInterface $entity Instance of the entity data * @param string $field The name of the upload field * @param array $settings Array of settings for the upload field */ - public function __construct(Table $table, Entity $entity, $field, array $settings) + public function __construct(Table $table, EntityInterface $entity, $field, array $settings) { if (isset($settings['root'])) { $this->setRoot($settings['root']); @@ -253,13 +253,19 @@ public function createPathFolder() * * @param string $folder Absolute path to the folder * @param bool $rmdir If you want to remove the folder as well - * @return void + * @return bool */ public function deleteFiles($folder, $rmdir = false) { - array_map('unlink', glob($folder . DS . '*')); + $fileList = glob($folder . DS . '*'); + if ($fileList !== false) { + array_map('unlink', $fileList); + } + if ($rmdir) { rmdir($folder); } + + return true; } } diff --git a/src/Model/Behavior/ProfferBehavior.php b/src/Model/Behavior/ProfferBehavior.php index 46c4c02..82fa7b3 100644 --- a/src/Model/Behavior/ProfferBehavior.php +++ b/src/Model/Behavior/ProfferBehavior.php @@ -37,7 +37,9 @@ public function initialize(array $config) Type::map('proffer.file', '\Proffer\Database\Type\FileType'); $schema = $this->_table->getSchema(); foreach (array_keys($this->getConfig()) as $field) { - $schema->setColumnType($field, 'proffer.file'); + if (is_string($field)) { + $schema->setColumnType($field, 'proffer.file'); + } } $this->_table->setSchema($schema); } @@ -75,12 +77,10 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti * @param \Proffer\Lib\ProfferPathInterface|null $path Inject an instance of ProfferPath * * @return true - * * @throws \Exception */ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options, ProfferPathInterface $path = null) { - foreach ($this->getConfig() as $field => $settings) { $tableEntityClass = $this->_table->getEntityClass(); @@ -109,9 +109,8 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o * @param \Cake\Datasource\EntityInterface $entity The current entity to process * @param \Proffer\Lib\ProfferPathInterface|null $path Inject an instance of ProfferPath * - * @throws \Exception If the file cannot be renamed / moved to the correct path - * * @return void + * @throws \Exception If the file cannot be renamed / moved to the correct path */ protected function process($field, array $settings, EntityInterface $entity, ProfferPathInterface $path = null) { @@ -153,9 +152,8 @@ protected function process($field, array $settings, EntityInterface $entity, Pro * @param array $settings Array of upload settings for the field * @param \Proffer\Lib\ProfferPathInterface|null $path Inject an instance of ProfferPath * - * @throws \App\Exception\InvalidClassException If the custom class doesn't implement the interface - * * @return \Proffer\Lib\ProfferPathInterface + * @throws \Proffer\Exception\InvalidClassException If the custom class doesn't implement the interface */ protected function createPath(EntityInterface $entity, $field, array $settings, ProfferPathInterface $path = null) { @@ -187,9 +185,8 @@ protected function createPath(EntityInterface $entity, $field, array $settings, * @param array $settings Array of upload field settings * @param \Proffer\Lib\ProfferPathInterface $path Instance of the path class * - * @throws \App\Exception\InvalidClassException If the transform class doesn't implement the interface - * * @return void + * @throws \Proffer\Exception\InvalidClassException If the transform class doesn't implement the interface */ protected function createThumbnails(EntityInterface $entity, array $settings, ProfferPathInterface $path) { diff --git a/src/Shell/ProfferShell.php b/src/Shell/ProfferShell.php index c1b015b..7e1cde9 100644 --- a/src/Shell/ProfferShell.php +++ b/src/Shell/ProfferShell.php @@ -3,6 +3,8 @@ use Cake\Console\Shell; use Cake\Core\Exception\Exception; +use Cake\ORM\Entity; +use Cake\ORM\ResultSet; use Proffer\Lib\ImageTransform; use Proffer\Lib\ProfferPath; @@ -75,7 +77,7 @@ public function getOptionParser() /** * Introduction to the shell * - * @return void + * @return bool|int|null */ public function main() { @@ -83,6 +85,8 @@ public function main() $this->out('This shell can be used to regenerate thumbnails and cleanup unlinked images.'); $this->hr(); $this->out($this->OptionParser->help()); + + return parent::main(); } /** @@ -161,19 +165,28 @@ public function cleanup($table) $this->out($this->nl(0)); } - $config = $this->Table->behaviors()->Proffer->config(); + $config = $this->Table->behaviors()->get('Proffer')->config(); // Get the root upload folder for this table $uploadFieldFolders = glob(WWW_ROOT . 'files' . DS . strtolower($table) . DS . '*'); + if (!is_array($uploadFieldFolders)) { + $this->err('No files found to process.'); + $this->_stop(); + } // Loop through each upload field configured for this table (field) - foreach ($uploadFieldFolders as $fieldFolder) { + foreach ((array)$uploadFieldFolders as $fieldFolder) { // Loop through each instance of an upload for this field (seed) - $pathFieldName = pathinfo($fieldFolder, PATHINFO_BASENAME); + $pathFieldName = pathinfo((string)$fieldFolder, PATHINFO_BASENAME); $uploadFolders = glob($fieldFolder . DS . '*'); - foreach ($uploadFolders as $seedFolder) { + if (!is_array($uploadFolders)) { + $this->err('No folders found to process.'); + $this->_stop(); + } + + foreach ((array)$uploadFolders as $seedFolder) { // Does the seed exist in the db? - $seed = pathinfo($seedFolder, PATHINFO_BASENAME); + $seed = pathinfo((string)$seedFolder, PATHINFO_BASENAME); foreach ($config as $field => $settings) { if ($pathFieldName != $field) { @@ -182,7 +195,8 @@ public function cleanup($table) $targets = []; - $record = $this->{$this->Table->alias()}->find() + /** @var Entity|false $record */ + $record = $this->{$this->Table->getAlias()}->find() ->select([ $field, $settings['dir'] @@ -207,8 +221,8 @@ public function cleanup($table) $this->out(__("Would remove folder `$seed`")); } } else { - array_map('unlink', glob($seedFolder . DS . '*')); - rmdir($seedFolder); + array_map('unlink', (array)glob($seedFolder . DS . '*')); + rmdir((string)$seedFolder); if ($this->param('verbose')) { $this->out(__("Remove `$seedFolder` folder and contents")); @@ -217,7 +231,7 @@ public function cleanup($table) } } } else { - $files = glob($seedFolder . DS . '*'); + $files = (array)glob($seedFolder . DS . '*'); $filenames = array_map(function ($p) { return pathinfo($p, PATHINFO_BASENAME); diff --git a/tests/TestCase/Model/Behavior/ProfferBehaviorTest.php b/tests/TestCase/Model/Behavior/ProfferBehaviorTest.php index cc6973d..5065109 100644 --- a/tests/TestCase/Model/Behavior/ProfferBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/ProfferBehaviorTest.php @@ -279,7 +279,7 @@ public function testBeforeSaveWithValidFile(array $entityData, array $expected) ->willReturnCallback(function ($source, $destination) { if (!file_exists(pathinfo($destination, PATHINFO_DIRNAME))) { mkdir(pathinfo($destination, PATHINFO_DIRNAME), 0777, true); - } + } return copy($source, $destination); }); From 33e79051be278be830b1144d7213283a61aa585a Mon Sep 17 00:00:00 2001 From: David Yell Date: Thu, 1 Aug 2019 17:00:07 +0100 Subject: [PATCH 6/6] Removed phpstan, as it requires php 7 --- composer.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 2060d09..ccffc97 100644 --- a/composer.json +++ b/composer.json @@ -24,8 +24,7 @@ "require-dev": { "phpunit/phpunit": "^5|^6", "cakephp/cakephp": "~3.4", - "cakephp/cakephp-codesniffer": "~3.0", - "phpstan/phpstan": "^0.11.12" + "cakephp/cakephp-codesniffer": "~3.0" }, "autoload": { "psr-4": { @@ -42,7 +41,6 @@ "scripts": { "cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", "cs-fix": "phpcbf --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", - "test": "phpunit --colors=always", - "phpstan": "phpstan analyse --level=6 --memory-limit=512M --configuration=phpstan.neon ./src" + "test": "phpunit --colors=always" } }