Skip to content

Commit

Permalink
Merge pull request #485 from raul338/cakephp-deprecations
Browse files Browse the repository at this point in the history
Prepare for CakePHP 3.6
  • Loading branch information
josegonzalez authored Apr 26, 2018
2 parents d42df95 + 66b7630 commit dd91ff8
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 90 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2

sudo: false

Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/)

Expand All @@ -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/).
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 9 additions & 6 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ Basic example
*/
?>
<?php echo $this->Form->create($user, ['type' => 'file']); ?>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('photo', ['type' => 'file']); ?>
<?php echo $this->Form->control('username'); ?>
<?php echo $this->Form->control('photo', ['type' => 'file']); ?>
// for CakePHP 3.0.x-3.3.x, use the following lines instead of the previous:
// <?php echo $this->Form->input('username'); ?>
// <?php echo $this->Form->input('photo', ['type' => 'file']); ?>
<?php echo $this->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)
{
Expand Down
9 changes: 3 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
</testsuites>

<filter>
<blacklist>
<directory suffix=".php">./docs</directory>
<directory suffix=".php">./vendor</directory>
<directory suffix=".php">./tests</directory>
<file>./tests/bootstrap.php</file>
</blacklist>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

<!-- Setup a listener for fixtures -->
Expand Down
17 changes: 17 additions & 0 deletions src/Database/Type/FileType.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Josegonzalez\Upload\Database\Type;

use Cake\Database\Driver;
use Cake\Database\Type;

class FileType extends Type
Expand All @@ -18,4 +19,20 @@ public function marshal($value)
{
return $value;
}

/**
* {@inheritDoc}
*/
public function toDatabase($value, Driver $driver)
{
return $value;
}

/**
* {@inheritDoc}
*/
public function toPHP($value, Driver $driver)
{
return $value;
}
}
8 changes: 4 additions & 4 deletions src/File/Path/Basepath/DefaultTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public function basepath()
if ($this->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"),
Expand Down
6 changes: 3 additions & 3 deletions src/File/Transformer/SlugTransformer.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
namespace Josegonzalez\Upload\File\Transformer;

use Cake\Utility\Inflector;
use Cake\Utility\Text;
use Josegonzalez\Upload\File\Transformer\DefaultTransformer;

class SlugTransformer extends DefaultTransformer
{
/**
* Creates a set of files from the initial data and returns them as key/value
* pairs, where the path on disk maps to name which each file should have.
* The file name will be sluggified (using Inflector::slug()) and lowercased.
* The file name will be sluggified (using Text::slug()) and lowercased.
*
* Example:
*
Expand All @@ -25,7 +25,7 @@ class SlugTransformer extends DefaultTransformer
public function transform()
{
$filename = pathinfo($this->data['name'], PATHINFO_FILENAME);
$filename = Inflector::slug($filename, '-');
$filename = Text::slug($filename, '-');

$ext = pathinfo($this->data['name'], PATHINFO_EXTENSION);
if (!empty($ext)) {
Expand Down
22 changes: 11 additions & 11 deletions src/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
Expand All @@ -82,15 +82,15 @@ 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;
}

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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 9 additions & 9 deletions tests/TestCase/File/Path/Basepath/DefaultTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
}
Loading

0 comments on commit dd91ff8

Please sign in to comment.