Skip to content

Commit a780815

Browse files
committed
symbolic definition
1 parent b6da161 commit a780815

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

src/ArrayFlavour/Items.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Yaoi\Schema\ArrayFlavour;
44

5-
65
use Yaoi\Schema\AbstractConstraint;
76
use Yaoi\Schema\Schema;
87

src/ObjectFlavour/Properties.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function setOwnerSchema(Schema $ownerSchema)
3636
}
3737

3838

39+
public function setProperty($name, $value)
40+
{
41+
return $this->__set($name, $value);
42+
}
43+
3944
public function __set($name, $value)
4045
{
4146
if ($value instanceof Constraint) {
@@ -45,6 +50,7 @@ public function __set($name, $value)
4550
}
4651

4752
$this->properties[$name] = $value;
53+
return $this;
4854
}
4955

5056
public function __get($name)

src/Types/AbstractType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(Schema $ownerSchema = null)
1818
}
1919

2020
/**
21-
* @param Constraint $constraint
21+
* @param Constraint ...$constraint
2222
* @return Schema
2323
*/
2424
public static function makeSchema($constraint = null)

tests/src/Schema/ParentTest.php

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Yaoi\Schema\Exception;
77
use Yaoi\Schema\ObjectFlavour\Properties;
88
use Yaoi\Schema\Schema;
9+
use Yaoi\Schema\Types\IntegerType;
10+
use Yaoi\Schema\Types\ObjectType;
911

1012
class ParentTest extends \PHPUnit_Framework_TestCase
1113
{
@@ -33,10 +35,33 @@ private function deepSchema()
3335
return $schema;
3436
}
3537

36-
public function testParent()
38+
39+
private function deepSchemaSymbolic()
3740
{
38-
$schema = $this->deepSchema();
41+
$schema = ObjectType::makeSchema(
42+
Properties::create()
43+
->setProperty(
44+
'level1',
45+
ObjectType::makeSchema(
46+
Properties::create()
47+
->setProperty(
48+
'level2',
49+
ObjectType::makeSchema(
50+
Properties::create()
51+
->setProperty(
52+
'level3',
53+
IntegerType::makeSchema()
54+
)
55+
)
56+
)
57+
)
58+
)
59+
);
60+
return $schema;
61+
}
3962

63+
private function assertSchema(Schema $schema)
64+
{
4065
$level1Schema = Properties::getFromSchema($schema)->getProperty('level1');
4166
$level2Schema = Properties::getFromSchema($level1Schema)->getProperty('level2');
4267
$level3Schema = Properties::getFromSchema($level2Schema)->getProperty('level3');
@@ -50,6 +75,12 @@ public function testParent()
5075
$this->assertSame($level2Schema, $level3Schema->getParentSchema());
5176
}
5277

78+
public function testParent()
79+
{
80+
$this->assertSchema($this->deepSchema());
81+
$this->assertSchema($this->deepSchemaSymbolic());
82+
}
83+
5384

5485
public function testAttach()
5586
{
@@ -78,6 +109,27 @@ public function testInvalidImport()
78109
//$this->assertSame('abc', $object->level1->level2->level3);
79110
}
80111

112+
public function testInvalidImportSymbolic()
113+
{
114+
$schema = $this->deepSchemaSymbolic();
115+
$this->setExpectedException(get_class(new Exception()), 'Validation failed (level1->level2->level3)',
116+
Exception::INVALID_VALUE);
117+
try {
118+
$object = $schema->import(array(
119+
'level1' => array(
120+
'level2' => array(
121+
'level3' => 'abc' // integer required
122+
),
123+
),
124+
));
125+
} catch (Exception $exception) {
126+
$this->assertSame(array('level1', 'level2', 'level3'), $exception->getStructureTrace());
127+
throw $exception;
128+
}
129+
//$this->assertSame('abc', $object->level1->level2->level3);
130+
}
131+
132+
81133
public function testImport()
82134
{
83135
$object = $this->deepSchema()->import(array(
@@ -89,4 +141,17 @@ public function testImport()
89141
));
90142
$this->assertSame(123, $object->level1->level2->level3);
91143
}
144+
145+
public function testImportSymbolic()
146+
{
147+
$object = $this->deepSchemaSymbolic()->import(array(
148+
'level1' => array(
149+
'level2' => array(
150+
'level3' => 123 // integer required
151+
),
152+
),
153+
));
154+
$this->assertSame(123, $object->level1->level2->level3);
155+
}
156+
92157
}

0 commit comments

Comments
 (0)