Skip to content

Commit

Permalink
Testing setters
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Sep 16, 2013
1 parent d2d7792 commit f2d5531
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cake/ORM/Entity.php
Expand Up @@ -86,7 +86,7 @@ public function __set($property, $value) {
* or bypass them
* @return \Cake\ORM\Entity
*/
public function set($property, $value = false, $useSetters = true) {
public function set($property, $value = true, $useSetters = true) {
if (is_string($property)) {
$property = [$property => $value];
} else {
Expand Down
102 changes: 102 additions & 0 deletions Cake/Test/TestCase/ORM/EntityTest.php
@@ -0,0 +1,102 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\ORM;

use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;

/**
* Entity test case.
*/
class EntityTest extends TestCase {

/**
* Tests setting a single property in an entity without custom setters
*
* @return void
*/
public function testSetOneParamNoSetters() {
$entity = new Entity;
$entity->set('foo', 'bar');
$this->assertEquals('bar', $entity->foo);

$entity->set('foo', 'baz');
$this->assertEquals('baz', $entity->foo);

$entity->set('id', 1);
$this->assertSame(1, $entity->id);
}

/**
* Tests setting multiple properties without custom setters
*
* @return void
*/
public function testSetMultiplePropertiesNOSetters() {
$entity = new Entity;
$entity->set(['foo' => 'bar', 'id' => 1]);
$this->assertEquals('bar', $entity->foo);
$this->assertSame(1, $entity->id);

$entity->set(['foo' => 'baz', 'id' => 2, 'thing' => 3]);
$this->assertEquals('baz', $entity->foo);
$this->assertSame(2, $entity->id);
$this->assertSame(3, $entity->thing);
}

/**
* Tests setting a single property using a setter function
*
* @return void
*/
public function testSetOneParamWithSetter() {
$entity = $this->getMock('\Cake\ORM\Entity', ['setName']);
$entity->expects($this->once())->method('setName')
->with('Jones')
->will($this->returnCallback(function($name) {
$this->assertEquals('Jones', $name);
return 'Dr. ' . $name;
}));
$entity->set('name', 'Jones');
$this->assertEquals('Dr. Jones', $entity->name);
}

/**
* Tests setting multiple properties using a setter function
*
* @return void
*/
public function testMultipleWithSetter() {
$entity = $this->getMock('\Cake\ORM\Entity', ['setName', 'setStuff']);
$entity->expects($this->once())->method('setName')
->with('Jones')
->will($this->returnCallback(function($name) {
$this->assertEquals('Jones', $name);
return 'Dr. ' . $name;
}));
$entity->expects($this->once())->method('setStuff')
->with(['a', 'b'])
->will($this->returnCallback(function($stuff) {
$this->assertEquals(['a', 'b'], $stuff);
return ['c', 'd'];
}));
$entity->set(['name' => 'Jones', 'stuff' => ['a', 'b']]);
$this->assertEquals('Dr. Jones', $entity->name);
$this->assertEquals(['c', 'd'], $entity->stuff);
}

}

0 comments on commit f2d5531

Please sign in to comment.