diff --git a/README.md b/README.md index da32c47..b1b010f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status](https://api.travis-ci.org/dereuromark/cakephp-shim.svg?branch=master)](https://travis-ci.org/dereuromark/cakephp-shim) [![Coverage Status](https://coveralls.io/repos/dereuromark/cakephp-shim/badge.svg)](https://coveralls.io/r/dereuromark/cakephp-shim) [![Latest Stable Version](https://poser.pugx.org/dereuromark/cakephp-shim/v/stable.svg)](https://packagist.org/packages/dereuromark/cakephp-shim) -[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%205.5-8892BF.svg)](https://php.net/) +[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%205.6-8892BF.svg)](https://php.net/) [![License](https://poser.pugx.org/dereuromark/cakephp-shim/license.svg)](https://packagist.org/packages/dereuromark/cakephp-shim) [![Total Downloads](https://poser.pugx.org/dereuromark/cakephp-shim/d/total.svg)](https://packagist.org/packages/dereuromark/cakephp-shim) [![Coding Standards](https://img.shields.io/badge/cs-PSR--2--R-yellow.svg)](https://github.com/php-fig-rectified/fig-rectified-standards) @@ -17,7 +17,7 @@ This is mainly useful when upgrading large applications to the next major framew Tons of code needs to be adjusted, using this Shim plugin quite a few lines less need to be touched. Especially the ORM layer, which would need heavy refactoring, requires a lot less changes to get things working againc. -**This plugin requires CakePHP 3.0+.** +**This plugin requires CakePHP 3.x.** ## Installation Please see [SETUP.md](docs/SETUP.md) diff --git a/composer.json b/composer.json index b6bfea7..e1d66e3 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,8 @@ } ], "require": { - "php": ">=5.5", - "cakephp/cakephp": "^3.0" + "php": ">=5.6", + "cakephp/cakephp": "^3.4" }, "require-dev": { "fig-r/psr2r-sniffer": "dev-master" diff --git a/src/Model/Table/Table.php b/src/Model/Table/Table.php index 2525d26..51a0311 100644 --- a/src/Model/Table/Table.php +++ b/src/Model/Table/Table.php @@ -185,6 +185,14 @@ public function validationDefault(Validator $validator) { } foreach ((array)$rules as $key => $rule) { + if (is_string($rule)) { + $ruleArray = ['rule' => $rule]; + $rules[$rule] = $ruleArray; + unset($rules[$key]); + $key = $rule; + $rule = $ruleArray; + } + if (isset($rule['required'])) { $validator->requirePresence($field, $rule['required']); unset($rules[$key]['required']); @@ -205,10 +213,6 @@ public function validationDefault(Validator $validator) { $rules[$key]['message'] = $message; } - if (is_string($rule)) { - $rules[$key] = ['rule' => $rule]; - } - if (!empty($rules[$key]['rule']) && ($rules[$key]['rule'] === 'notEmpty' || $rules[$key]['rule'] === ['notEmpty'])) { $rules[$key]['rule'] = 'notBlank'; } diff --git a/src/View/Helper/CookieHelper.php b/src/View/Helper/CookieHelper.php index 8c77e18..f219d30 100644 --- a/src/View/Helper/CookieHelper.php +++ b/src/View/Helper/CookieHelper.php @@ -9,16 +9,31 @@ */ class CookieHelper extends Helper { + /** + * Return all cookie names available. + * + * @return array + */ + public function getCookies() { + $cookies = $this->request->getCookieParams(); + if (!$cookies) { + return []; + } + + return array_keys($cookies); + } + /** * Reads a cookie value for a key or return values for all keys. * * In your view: `$this->Cookie->read('key');` * - * @param string|null $name the name of the cookie key you want to read - * @return mixed values from the cookie vars + * @param string|null $key The name of the cookie key you want to read + * @param string|null $default + * @return mixed Values from the cookie vars */ - public function read($name = null) { - return $this->request->cookie($name); + public function read($key = null, $default = null) { + return $this->request->getCookie($key, $default); } /** @@ -26,11 +41,11 @@ public function read($name = null) { * * In your view: `$this->Cookie->check('key');` * - * @param string $name Cookie name to check. + * @param string $key Cookie name to check. * @return bool */ - public function check($name) { - return $this->request->cookie($name) !== null; + public function check($key) { + return $this->request->getCookie($key) !== null; } /** diff --git a/tests/TestCase/Model/Table/TableTest.php b/tests/TestCase/Model/Table/TableTest.php index 6d3b51c..c1cf704 100644 --- a/tests/TestCase/Model/Table/TableTest.php +++ b/tests/TestCase/Model/Table/TableTest.php @@ -442,6 +442,18 @@ public function testValidationShims() { $wheel = $this->Wheels->newEntity(['position' => 'rear left']); $result = $this->Wheels->save($wheel); $this->assertTrue((bool)$result); + + $wheel = $this->Wheels->newEntity(['position' => 'rear left', 'car_id' => 'a']); + $expected = [ + 'car_id' => [ + 'numeric' => 'The provided value is invalid' + ], + ]; + $this->assertSame($expected, $wheel->errors()); + + $wheel = $this->Wheels->newEntity(['position' => 'rear left', 'car_id' => '1']); + $result = $this->Wheels->save($wheel); + $this->assertTrue((bool)$result); } /** diff --git a/tests/TestCase/View/Helper/CookieHelperTest.php b/tests/TestCase/View/Helper/CookieHelperTest.php index 5337b63..f25466e 100644 --- a/tests/TestCase/View/Helper/CookieHelperTest.php +++ b/tests/TestCase/View/Helper/CookieHelperTest.php @@ -21,7 +21,7 @@ public function setUp() { parent::setUp(); $this->Cookie = new CookieHelper(new View(null)); - $this->Cookie->request = $this->getMockBuilder(Request::class)->setMethods(['cookie'])->getMock(); + $this->Cookie->request = $this->getMockBuilder(Request::class)->setMethods(['getCookie', 'getCookieParams'])->getMock(); } /** @@ -34,12 +34,14 @@ public function tearDown() { } /** - * CookieHelperTest::testObject() - * * @return void */ - public function testObject() { - $this->assertInstanceOf(CookieHelper::class, $this->Cookie); + public function testGetChookies() { + $this->Cookie->request->expects($this->at(0)) + ->method('getCookieParams') + ->will($this->returnValue(['one' => 1, 'two' => 2])); + + $this->assertSame(['one', 'two'], $this->Cookie->getCookies()); } /** @@ -49,10 +51,10 @@ public function testObject() { */ public function testCheck() { $this->Cookie->request->expects($this->at(0)) - ->method('cookie') + ->method('getCookie') ->will($this->returnValue(null)); $this->Cookie->request->expects($this->at(1)) - ->method('cookie') + ->method('getCookie') ->will($this->returnValue('val')); $this->assertFalse($this->Cookie->check('Foo.key')); @@ -66,7 +68,7 @@ public function testCheck() { */ public function testRead() { $this->Cookie->request->expects($this->once()) - ->method('cookie') + ->method('getCookie') ->will($this->returnValue('val')); $output = $this->Cookie->read('Foo.key'); diff --git a/tests/test_app/src/Model/Table/WheelsTable.php b/tests/test_app/src/Model/Table/WheelsTable.php index 1b06a7b..9a903a0 100644 --- a/tests/test_app/src/Model/Table/WheelsTable.php +++ b/tests/test_app/src/Model/Table/WheelsTable.php @@ -30,6 +30,9 @@ class WheelsTable extends Table { * @var array */ public $validate = [ + 'car_id' => [ + 'numeric', + ], 'position' => [ 'notBlank' => [ 'rule' => 'notEmpty', // old way of notBlank