/
EntityTest.php
102 lines (92 loc) · 2.83 KB
/
EntityTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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);
}
}