Skip to content

Commit

Permalink
Fix shim for validation in simple string cases. Also fixed CookieHelp…
Browse files Browse the repository at this point in the history
…er to new methods.
  • Loading branch information
dereuromark committed Dec 27, 2017
1 parent 0882feb commit a4c5e6d
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions src/Model/Table/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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';
}
Expand Down
29 changes: 22 additions & 7 deletions src/View/Helper/CookieHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,43 @@
*/
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);
}

/**
* Checks if a cookie key has been set.
*
* 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;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Model/Table/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
18 changes: 10 additions & 8 deletions tests/TestCase/View/Helper/CookieHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

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

/**
Expand All @@ -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'));
Expand All @@ -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');
Expand Down
3 changes: 3 additions & 0 deletions tests/test_app/src/Model/Table/WheelsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class WheelsTable extends Table {
* @var array
*/
public $validate = [
'car_id' => [
'numeric',
],
'position' => [
'notBlank' => [
'rule' => 'notEmpty', // old way of notBlank
Expand Down

0 comments on commit a4c5e6d

Please sign in to comment.