|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Models; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Spiral\Models\ModelSchema; |
|
16
|
|
|
use Spiral\Models\SchematicEntity; |
|
17
|
|
|
|
|
18
|
|
|
class SchematicEntityTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
public function testFillable(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$schema = [ModelSchema::SECURED => []]; |
|
23
|
|
|
|
|
24
|
|
|
$data = ['a' => 1, 'b' => 2, 'c' => 3]; |
|
25
|
|
|
|
|
26
|
|
|
$entity = new SchematicEntity([], $schema); |
|
27
|
|
|
$entity->setFields($data); |
|
28
|
|
|
$this->assertSame($data, $entity->getFields()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testFillable2(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$schema = [ModelSchema::FILLABLE => '*']; |
|
34
|
|
|
|
|
35
|
|
|
$data = ['a' => 1, 'b' => 2, 'c' => 3]; |
|
36
|
|
|
|
|
37
|
|
|
$entity = new SchematicEntity([], $schema); |
|
38
|
|
|
$entity->setFields($data); |
|
39
|
|
|
$this->assertSame($data, $entity->getFields()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testSecured(): void |
|
43
|
|
|
{ |
|
44
|
|
|
$schema = [ModelSchema::SECURED => '*']; |
|
45
|
|
|
|
|
46
|
|
|
$data = ['a' => 1, 'b' => 2, 'c' => 3]; |
|
47
|
|
|
|
|
48
|
|
|
$entity = new SchematicEntity([], $schema); |
|
49
|
|
|
$entity->setFields($data); |
|
50
|
|
|
$this->assertSame([], $entity->getFields()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testPartiallySecured(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$schema = [ |
|
56
|
|
|
ModelSchema::SECURED => '*', |
|
57
|
|
|
ModelSchema::FILLABLE => ['a', 'b'], |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
$data = ['a' => 1, 'b' => 2, 'c' => 3]; |
|
61
|
|
|
|
|
62
|
|
|
$entity = new SchematicEntity([], $schema); |
|
63
|
|
|
$entity->setFields($data); |
|
64
|
|
|
$this->assertSame(['a' => 1, 'b' => 2], $entity->getFields()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getSetters(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$schema = [ |
|
70
|
|
|
ModelSchema::MUTATORS => [ |
|
71
|
|
|
'setter' => ['a' => 'intval'], |
|
72
|
|
|
], |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
$entity = new SchematicEntity([], $schema); |
|
76
|
|
|
$entity->setField('a', '123'); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertInternalType('int', $entity->getField('a')); |
|
|
|
|
|
|
79
|
|
|
$this->assertSame(123, $entity->getField('a')); |
|
80
|
|
|
|
|
81
|
|
|
$entity->a = '800'; |
|
|
|
|
|
|
82
|
|
|
$this->assertInternalType('int', $entity->a); |
|
|
|
|
|
|
83
|
|
|
$this->assertSame(800, $entity->a); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testGetters(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$schema = [ |
|
89
|
|
|
ModelSchema::MUTATORS => [ |
|
90
|
|
|
'getter' => ['a' => 'intval'], |
|
91
|
|
|
], |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
$entity = new SchematicEntity([], $schema); |
|
95
|
|
|
|
|
96
|
|
|
$entity->setField('a', false); |
|
97
|
|
|
$this->assertInternalType('int', $entity->getField('a')); |
|
|
|
|
|
|
98
|
|
|
$this->assertInternalType('bool', $entity->getValue()['a']); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$entity->a = 8000.1; |
|
|
|
|
|
|
101
|
|
|
$this->assertInternalType('int', $entity->a); |
|
|
|
|
|
|
102
|
|
|
$this->assertInternalType('float', $entity->getValue()['a']); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.