diff --git a/.travis.yml b/.travis.yml index 5ceb768e..78d867bf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,10 @@ language: php php: - - 5.5 - 5.6 - 7.0 + - 7.1 + - 7.2 sudo: false @@ -22,10 +23,10 @@ matrix: fast_finish: true include: - - php: 5.5 + - php: 5.6 env: PHPCS=1 DEFAULT=0 - - php: 5.5 + - php: 5.6 env: COVERALLS=1 DEFAULT=0 - php: hhvm diff --git a/README.md b/README.md index ebe23e80..5333de16 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -[![Build Status](https://img.shields.io/travis/FriendsOfCake/cakephp-upload/master.svg?style=flat-square)](https://travis-ci.org/FriendsOfCake/cakephp-upload) -[![Coverage Status](https://img.shields.io/coveralls/FriendsOfCake/cakephp-upload.svg?style=flat-square)](https://coveralls.io/r/FriendsOfCake/cakephp-upload?branch=master) -[![Total Downloads](https://img.shields.io/packagist/dt/FriendsOfCake/cakephp-upload.svg?style=flat-square)](https://packagist.org/packages/FriendsOfCake/cakephp-upload) -[![Latest Stable Version](https://img.shields.io/packagist/v/FriendsOfCake/cakephp-upload.svg?style=flat-square)](https://packagist.org/packages/FriendsOfCake/cakephp-upload) +[![Build Status](https://img.shields.io/travis/FriendsOfCake/cakephp-upload/master.svg?style=flat-square)](https://travis-ci.org/FriendsOfCake/cakephp-upload) +[![Coverage Status](https://img.shields.io/coveralls/FriendsOfCake/cakephp-upload.svg?style=flat-square)](https://coveralls.io/r/FriendsOfCake/cakephp-upload?branch=master) +[![Total Downloads](https://img.shields.io/packagist/dt/FriendsOfCake/cakephp-upload.svg?style=flat-square)](https://packagist.org/packages/FriendsOfCake/cakephp-upload) +[![Latest Stable Version](https://img.shields.io/packagist/v/FriendsOfCake/cakephp-upload.svg?style=flat-square)](https://packagist.org/packages/FriendsOfCake/cakephp-upload) [![Documentation Status](https://readthedocs.org/projects/cakephp-upload/badge/?version=latest&style=flat-square)](https://readthedocs.org/projects/cakephp-upload/?badge=latest) [![Gratipay](https://img.shields.io/gratipay/josegonzalez.svg?style=flat-square)](https://gratipay.com/~josegonzalez/) @@ -15,8 +15,8 @@ See [this blog post](http://josediazgonzalez.com/2015/12/05/uploading-files-and- ## Requirements -* CakePHP 3.x -* PHP 5.4+ +* CakePHP 3.4+ +* PHP 5.6+ ## Documentation For documentation, please see [the docs](http://cakephp-upload.readthedocs.org/en/latest/). diff --git a/composer.json b/composer.json index 93c04f4b..82242c00 100644 --- a/composer.json +++ b/composer.json @@ -13,11 +13,11 @@ ], "require": { "cakephp/orm": "3.*", - "php": ">=5.5", + "php": ">=5.6", "league/flysystem": "*" }, "require-dev": { - "cakephp/cakephp": "~3.0", + "cakephp/cakephp": "~3.4", "phpunit/phpunit": "<6.0", "league/flysystem-vfs": "*", "cakephp/cakephp-codesniffer": "dev-master", diff --git a/docs/examples.rst b/docs/examples.rst index 6ced8a55..5212375a 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -63,14 +63,17 @@ Basic example */ ?> Form->create($user, ['type' => 'file']); ?> - Form->input('username'); ?> - Form->input('photo', ['type' => 'file']); ?> + Form->control('username'); ?> + Form->control('photo', ['type' => 'file']); ?> + // for CakePHP 3.0.x-3.3.x, use the following lines instead of the previous: + // Form->input('username'); ?> + // Form->input('photo', ['type' => 'file']); ?> Form->end(); ?> - - Note: If you used *bake* to generate MVC structure after creating + + Note: If you used *bake* to generate MVC structure after creating the users table, you will need to remove the default scalar validation - for the photos field. - + for the photos field. + .. code:: php public function validationDefault(Validator $validator) { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9546893d..8dca33a0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,12 +18,9 @@ - - ./docs - ./vendor - ./tests - ./tests/bootstrap.php - + + ./src + diff --git a/src/Database/Type/FileType.php b/src/Database/Type/FileType.php index c8d57668..9d6e59a6 100644 --- a/src/Database/Type/FileType.php +++ b/src/Database/Type/FileType.php @@ -1,6 +1,7 @@ entity->isNew()) { throw new LogicException('{primaryKey} substitution not allowed for new entities'); } - if (is_array($this->table->primaryKey())) { + if (is_array($this->table->getPrimaryKey())) { throw new LogicException('{primaryKey} substitution not valid for composite primary keys'); } } $replacements = [ - '{primaryKey}' => $this->entity->get($this->table->primaryKey()), - '{model}' => $this->table->alias(), - '{table}' => $this->table->table(), + '{primaryKey}' => $this->entity->get($this->table->getPrimaryKey()), + '{model}' => $this->table->getAlias(), + '{table}' => $this->table->getTable(), '{field}' => $this->field, '{year}' => date("Y"), '{month}' => date("m"), diff --git a/src/File/Transformer/SlugTransformer.php b/src/File/Transformer/SlugTransformer.php index 0b4370ce..4c058f61 100644 --- a/src/File/Transformer/SlugTransformer.php +++ b/src/File/Transformer/SlugTransformer.php @@ -1,7 +1,7 @@ data['name'], PATHINFO_FILENAME); - $filename = Inflector::slug($filename, '-'); + $filename = Text::slug($filename, '-'); $ext = pathinfo($this->data['name'], PATHINFO_EXTENSION); if (!empty($ext)) { diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index fc895204..876bc5ff 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -37,15 +37,15 @@ public function initialize(array $config) } } - $this->config($configs); - $this->config('className', null); + $this->setConfig($configs); + $this->setConfig('className', null); Type::map('upload.file', 'Josegonzalez\Upload\Database\Type\FileType'); - $schema = $this->_table->schema(); - foreach (array_keys($this->config()) as $field) { - $schema->columnType($field, 'upload.file'); + $schema = $this->_table->getSchema(); + foreach (array_keys($this->getConfig()) as $field) { + $schema->setColumnType($field, 'upload.file'); } - $this->_table->schema($schema); + $this->_table->setSchema($schema); } /** @@ -58,9 +58,9 @@ public function initialize(array $config) */ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) { - $validator = $this->_table->validator(); + $validator = $this->_table->getValidator(); $dataArray = $data->getArrayCopy(); - foreach (array_keys($this->config()) as $field) { + foreach (array_keys($this->getConfig(null, [])) as $field) { if (!$validator->isEmptyAllowed($field, false)) { continue; } @@ -82,7 +82,7 @@ public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $opti */ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) { - foreach ($this->config() as $field => $settings) { + foreach ($this->getConfig(null, []) as $field => $settings) { if (in_array($field, $this->protectedFieldNames)) { continue; } @@ -90,7 +90,7 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) if (Hash::get((array)$entity->get($field), 'error') !== UPLOAD_ERR_OK) { if (Hash::get($settings, 'restoreValueOnFailure', true)) { $entity->set($field, $entity->getOriginal($field)); - $entity->dirty($field, false); + $entity->setDirty($field, false); } continue; } @@ -128,7 +128,7 @@ public function afterDelete(Event $event, Entity $entity, ArrayObject $options) { $result = true; - foreach ($this->config() as $field => $settings) { + foreach ($this->getConfig(null, []) as $field => $settings) { if (in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true)) { continue; } diff --git a/tests/TestCase/File/Path/Basepath/DefaultTraitTest.php b/tests/TestCase/File/Path/Basepath/DefaultTraitTest.php index 5e6ad502..a6d37fa1 100644 --- a/tests/TestCase/File/Path/Basepath/DefaultTraitTest.php +++ b/tests/TestCase/File/Path/Basepath/DefaultTraitTest.php @@ -17,8 +17,8 @@ public function testNoSpecialConfiguration() $mock->data = ['name' => 'filename']; $mock->field = 'field'; $mock->entity->expects($this->once())->method('get')->will($this->returnValue(1)); - $mock->table->expects($this->once())->method('alias')->will($this->returnValue('Table')); - $mock->table->expects($this->once())->method('primaryKey')->will($this->returnValue('id')); + $mock->table->expects($this->once())->method('getAlias')->will($this->returnValue('Table')); + $mock->table->expects($this->once())->method('getPrimaryKey')->will($this->returnValue('id')); $this->assertEquals('webroot/files/Table/field/', $mock->basepath()); } @@ -31,8 +31,8 @@ public function testCustomPath() $mock->data = ['name' => 'filename']; $mock->field = 'field'; $mock->entity->expects($this->once())->method('get')->will($this->returnValue(1)); - $mock->table->expects($this->once())->method('alias')->will($this->returnValue('Table')); - $mock->table->expects($this->once())->method('primaryKey')->will($this->returnValue('id')); + $mock->table->expects($this->once())->method('getAlias')->will($this->returnValue('Table')); + $mock->table->expects($this->once())->method('getPrimaryKey')->will($this->returnValue('id')); $this->assertEquals('webroot/files/Table-field/', $mock->basepath()); } @@ -45,8 +45,8 @@ public function testExistingEntityWithPrimaryKey() $mock->data = ['name' => 'filename']; $mock->field = 'field'; $mock->entity->expects($this->once())->method('get')->will($this->returnValue(1)); - $mock->table->expects($this->once())->method('alias')->will($this->returnValue('Table')); - $mock->table->expects($this->exactly(2))->method('primaryKey')->will($this->returnValue('id')); + $mock->table->expects($this->once())->method('getAlias')->will($this->returnValue('Table')); + $mock->table->expects($this->exactly(2))->method('getPrimaryKey')->will($this->returnValue('id')); $this->assertEquals('webroot/files/Table-field/1/', $mock->basepath()); } @@ -75,7 +75,7 @@ public function testExitingEntityWithCompositePrimaryKey() $mock->data = ['name' => 'filename']; $mock->field = 'field'; $mock->entity->expects($this->once())->method('isNew')->will($this->returnValue(false)); - $mock->table->expects($this->once())->method('primaryKey')->will($this->returnValue(['id', 'other_id'])); + $mock->table->expects($this->once())->method('getPrimaryKey')->will($this->returnValue(['id', 'other_id'])); $mock->basepath(); } @@ -113,7 +113,7 @@ public function testModelFieldYearWithMonthAndDayPath() $mock->data = ['name' => 'filename']; $mock->field = 'field'; $mock->entity->expects($this->once())->method('get')->will($this->returnValue(1)); - $mock->table->expects($this->once())->method('alias')->will($this->returnValue('Table')); + $mock->table->expects($this->once())->method('getAlias')->will($this->returnValue('Table')); $this->assertEquals('webroot/files/Table/field/' . date("Y") . '/' . date("m") . '/' . date("d") . '/', $mock->basepath()); } @@ -169,7 +169,7 @@ public function testFieldValue() $mock->data = ['name' => 'filename']; $mock->field = 'field'; $mock->entity->expects($this->any())->method('get')->will($this->returnValue('value')); - $mock->table->expects($this->once())->method('alias')->will($this->returnValue('Table')); + $mock->table->expects($this->once())->method('getAlias')->will($this->returnValue('Table')); $this->assertEquals('webroot/files/Table/value/', $mock->basepath()); } } diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 0e6b43eb..e3aaa9aa 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -64,13 +64,13 @@ public function testInitialize() ->setConstructorArgs([$table, []]) ->getMock(); $schema->expects($this->once()) - ->method('columnType') + ->method('setColumnType') ->with('field', 'upload.file'); $table->expects($this->at(0)) - ->method('schema') + ->method('getSchema') ->will($this->returnValue($schema)); $table->expects($this->at(1)) - ->method('schema') + ->method('setSchema') ->will($this->returnValue($schema)); $methods = array_diff($this->behaviorMethods, ['initialize']); @@ -83,8 +83,8 @@ public function testInitialize() $property->setAccessible(true); $property->setValue($behavior, $table); - $behavior->expects($this->exactly(3)) - ->method('config') + $behavior->expects($this->exactly(1)) + ->method('getConfig') ->will($this->returnValue($this->settings)); $behavior->initialize($this->settings); @@ -95,7 +95,7 @@ public function testInheritedConfig() $table = TableRegistry::get('Josegonzales/Upload.Files'); $behavior = new ChildBehavior($table, []); - $result = $behavior->config(); + $result = $behavior->getConfig(); $expected = ['key' => 'value']; $this->assertEquals($expected, $result); } @@ -109,16 +109,16 @@ public function testInitializeIndexedConfig() ->setConstructorArgs([$table, []]) ->getMock(); $schema->expects($this->once()) - ->method('columnType') + ->method('setColumnType') ->with('field', 'upload.file'); $table->expects($this->at(0)) - ->method('schema') + ->method('getSchema') ->will($this->returnValue($schema)); $table->expects($this->at(1)) - ->method('schema') + ->method('setSchema') ->will($this->returnValue($schema)); - $methods = array_diff($this->behaviorMethods, ['initialize', 'config', 'setConfig', 'getConfig']); + $methods = array_diff($this->behaviorMethods, ['initialize', 'setConfig', 'getConfig']); $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') ->setMethods($methods) ->disableOriginalConstructor() @@ -129,7 +129,7 @@ public function testInitializeIndexedConfig() $property->setValue($behavior, $table); $behavior->initialize($settings); - $this->assertEquals(['field' => []], $behavior->config()); + $this->assertEquals(['field' => []], $behavior->getConfig()); } public function testInitializeAddBehaviorOptionsInterfaceConfig() @@ -144,16 +144,16 @@ public function testInitializeAddBehaviorOptionsInterfaceConfig() ->setConstructorArgs([$table, []]) ->getMock(); $schema->expects($this->once()) - ->method('columnType') + ->method('setColumnType') ->with('field', 'upload.file'); $table->expects($this->at(0)) - ->method('schema') + ->method('getSchema') ->will($this->returnValue($schema)); $table->expects($this->at(1)) - ->method('schema') + ->method('setSchema') ->will($this->returnValue($schema)); - $methods = array_diff($this->behaviorMethods, ['initialize', 'config', 'setConfig', 'getConfig']); + $methods = array_diff($this->behaviorMethods, ['initialize', 'setConfig', 'getConfig']); //$behavior = $this->getMock('Josegonzalez\Upload\Model\Behavior\UploadBehavior', $methods, [$table, $settings], '', false); $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') ->setMethods($methods) @@ -166,7 +166,7 @@ public function testInitializeAddBehaviorOptionsInterfaceConfig() $property->setValue($behavior, $table); $behavior->initialize($settings); - $this->assertEquals(['field' => []], $behavior->config()); + $this->assertEquals(['field' => []], $behavior->getConfig()); } public function testBeforeMarshalOk() @@ -178,7 +178,7 @@ public function testBeforeMarshalOk() $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $table->expects($this->once()) - ->method('validator') + ->method('getValidator') ->will($this->returnValue($validator)); $methods = array_diff($this->behaviorMethods, ['beforeMarshal']); @@ -187,7 +187,7 @@ public function testBeforeMarshalOk() ->setConstructorArgs([$table, $this->settings]) ->getMock(); $behavior->expects($this->any()) - ->method('config') + ->method('getConfig') ->will($this->returnValue($this->settings)); $data = new ArrayObject($this->dataOk); @@ -204,7 +204,7 @@ public function testBeforeMarshalError() $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $table->expects($this->once()) - ->method('validator') + ->method('getValidator') ->will($this->returnValue($validator)); $methods = array_diff($this->behaviorMethods, ['beforeMarshal']); @@ -213,7 +213,7 @@ public function testBeforeMarshalError() ->setConstructorArgs([$table, $this->settings]) ->getMock(); $behavior->expects($this->any()) - ->method('config') + ->method('getConfig') ->will($this->returnValue($this->settings)); $data = new ArrayObject($this->dataError); @@ -230,7 +230,7 @@ public function testBeforeMarshalEmptyAllowed() $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $table->expects($this->once()) - ->method('validator') + ->method('getValidator') ->will($this->returnValue($validator)); $methods = array_diff($this->behaviorMethods, ['beforeMarshal']); @@ -239,7 +239,7 @@ public function testBeforeMarshalEmptyAllowed() ->setConstructorArgs([$table, $this->settings]) ->getMock(); $behavior->expects($this->any()) - ->method('config') + ->method('getConfig') ->will($this->returnValue($this->settings)); $data = new ArrayObject($this->dataError); @@ -251,12 +251,12 @@ public function testBeforeSaveUploadError() { $originalValue = rand(1000, 9999); - $methods = array_diff($this->behaviorMethods, ['beforeSave', 'config', 'setConfig', 'getConfig']); + $methods = array_diff($this->behaviorMethods, ['beforeSave', 'setConfig', 'getConfig']); $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') ->setMethods($methods) ->setConstructorArgs([$this->table, $this->settings]) ->getMock(); - $behavior->config($this->settings); + $behavior->setConfig($this->settings); $this->entity->expects($this->any()) ->method('get') ->with('field') @@ -273,19 +273,19 @@ public function testBeforeSaveUploadError() ->method('set') ->with('field', $originalValue); $this->entity->expects($this->once()) - ->method('dirty') + ->method('setDirty') ->with('field', false); $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); } public function testBeforeSaveWriteFail() { - $methods = array_diff($this->behaviorMethods, ['beforeSave', 'config', 'setConfig', 'getConfig']); + $methods = array_diff($this->behaviorMethods, ['beforeSave', 'setConfig', 'getConfig']); $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') ->setMethods($methods) ->setConstructorArgs([$this->table, $this->settings]) ->getMock(); - $behavior->config($this->settings); + $behavior->setConfig($this->settings); $this->entity->expects($this->any()) ->method('get') ->with('field') @@ -313,7 +313,7 @@ public function testBeforeSaveOk() ->setMethods($methods) ->setConstructorArgs([$this->table, $this->settings]) ->getMock(); - $behavior->config($this->settings); + $behavior->setConfig($this->settings); $this->entity->expects($this->any()) ->method('get') ->with('field') @@ -339,12 +339,12 @@ public function testBeforeSaveDoesNotRestoreOriginalValue() $settings = $this->settings; $settings['field']['restoreValueOnFailure'] = false; - $methods = array_diff($this->behaviorMethods, ['beforeSave', 'config', 'setConfig', 'getConfig']); + $methods = array_diff($this->behaviorMethods, ['beforeSave', 'setConfig', 'getConfig']); $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') ->setMethods($methods) ->setConstructorArgs([$this->table, $this->settings]) ->getMock(); - $behavior->config($settings); + $behavior->setConfig($settings); $this->entity->expects($this->never())->method('getOriginal'); $this->entity->expects($this->never())->method('set'); @@ -361,7 +361,7 @@ public function testBeforeSaveWithProtectedFieldName() ->setMethods($methods) ->setConstructorArgs([$this->table, $this->settings]) ->getMock(); - $behavior->config($settings); + $behavior->setConfig($settings); $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); } @@ -373,7 +373,7 @@ public function testAfterDeleteOk() ->setMethods($methods) ->setConstructorArgs([$this->table, $this->dataOk]) ->getMock(); - $behavior->config($this->dataOk); + $behavior->setConfig($this->dataOk); $behavior->expects($this->any()) ->method('getPathProcessor') @@ -395,7 +395,7 @@ public function testAfterDeleteFail() ->setMethods($methods) ->setConstructorArgs([$this->table, $this->dataOk]) ->getMock(); - $behavior->config($this->dataOk); + $behavior->setConfig($this->dataOk); $behavior->expects($this->any()) ->method('getPathProcessor') @@ -417,7 +417,7 @@ public function testAfterDeleteSkip() ->setMethods($methods) ->setConstructorArgs([$this->table, $this->dataError]) ->getMock(); - $behavior->config($this->dataError); + $behavior->setConfig($this->dataError); $behavior->expects($this->any()) ->method('getWriter') @@ -439,7 +439,7 @@ public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile() ->setMethods($methods) ->setConstructorArgs([$this->table, $this->dataOk]) ->getMock(); - $behavior->config($this->dataOk); + $behavior->setConfig($this->dataOk); $this->entity->expects($this->at(0)) ->method('has') @@ -487,7 +487,7 @@ public function testAfterDeletePrefersStoredPathOverPathProcessor() ->setMethods($methods) ->setConstructorArgs([$this->table, $this->dataOk]) ->getMock(); - $behavior->config($this->dataOk); + $behavior->setConfig($this->dataOk); $this->entity->expects($this->at(0)) ->method('has') @@ -528,7 +528,7 @@ public function testAfterDeleteNoDeleteCallback() $this->dataOk['field']['deleteCallback'] = null; - $behavior->config($this->dataOk); + $behavior->setConfig($this->dataOk); $behavior->expects($this->once())->method('getPathProcessor') ->with($this->entity, $this->entity->field, 'field', $this->dataOk['field']) ->willReturn($this->processor); @@ -565,7 +565,7 @@ public function testAfterDeleteUsesDeleteCallback() ]; }; - $behavior->config($this->dataOk); + $behavior->setConfig($this->dataOk); $behavior->expects($this->once())->method('getPathProcessor') ->with($this->entity, $this->entity->field, 'field', $this->dataOk['field']) ->willReturn($this->processor); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 980d576f..d27cc49c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -59,7 +59,7 @@ ] ]; -Cake\Cache\Cache::config($cache); +Cake\Cache\Cache::setConfig($cache); Cake\Core\Configure::write('Session', [ 'defaults' => 'php' ]); @@ -71,7 +71,7 @@ putenv('db_dsn=sqlite:///:memory:'); } -Cake\Datasource\ConnectionManager::config('test', [ +Cake\Datasource\ConnectionManager::setConfig('test', [ 'url' => getenv('db_dsn'), 'timezone' => 'UTC' ]);