* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class TypeVisitorTest extends TestCase { public function testVisitArray() { $type = TypeFactory::getArray(); $data = (new TypeVisitor())->visitArray([10], $type, ''); $this->assertIsArray($data); $this->assertSame([10], $data); } public function testVisitArrayValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function (array $data) { return count($data) < 2; }]) ]); $type = TypeFactory::getArray(); (new TypeVisitor($validator))->visitArray([10, 8, 6], $type, '/foo/bar'); } public function testVisitBinary() { $type = TypeFactory::getBinary(); $data = (new TypeVisitor())->visitBinary(base64_encode('foo'), $type, ''); $this->assertIsResource($data); $this->assertSame('foo', stream_get_contents($data)); } public function testVisitBinaryValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function ($data) { return fstat($data)['size'] < 2; }]) ]); $type = TypeFactory::getBinary(); (new TypeVisitor($validator))->visitBinary(base64_encode('foo'), $type, '/foo/bar'); } public function testVisitBoolean() { $type = TypeFactory::getBoolean(); $this->assertSame(true, (new TypeVisitor())->visitBoolean(true, $type, '')); } public function testVisitBooleanValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function ($data) { return $data === true; }]) ]); $type = TypeFactory::getBoolean(); (new TypeVisitor($validator))->visitBoolean(false, $type, '/foo/bar'); } public function testVisitStruct() { $type = TypeFactory::getStruct() ->setAttribute(TypeAbstract::ATTR_CLASS, PopoClass::class) ->addProperty('foo', TypeFactory::getString()) ->addProperty('bar', TypeFactory::getString()); // popo class $type->setAttribute(TypeAbstract::ATTR_CLASS, PopoClass::class); $record = (new TypeVisitor())->visitStruct((object) ['foo' => 'bar', 'bar' => 'foo'], $type, ''); $this->assertInstanceOf(PopoClass::class, $record); $this->assertEquals('bar', $record->getFoo()); $this->assertEquals('foo', $record->getBar()); } public function testVisitStructMapping() { $type = TypeFactory::getStruct() ->setAttribute(TypeAbstract::ATTR_CLASS, PopoClass::class) ->setAttribute(TypeAbstract::ATTR_MAPPING, ['my-custom-prop' => 'bar']) ->addProperty('foo', TypeFactory::getString()) ->addProperty('bar', TypeFactory::getString()); // popo class $type->setAttribute(TypeAbstract::ATTR_CLASS, PopoClass::class); $record = (new TypeVisitor())->visitStruct((object) ['foo' => 'bar', 'my-custom-prop' => 'foo'], $type, ''); $this->assertInstanceOf(PopoClass::class, $record); $this->assertEquals('bar', $record->getFoo()); $this->assertEquals('foo', $record->getBar()); } public function testVisitStructValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function (RecordInterface $data) { return isset($data->foo); }]) ]); $type = TypeFactory::getStruct(); (new TypeVisitor($validator))->visitStruct((object) ['bar' => 'foo'], $type, '/foo/bar'); } public function testVisitStructValidatePopo() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function (PopoClass $data) { return $data->getFoo() == 'foo'; }]) ]); $type = TypeFactory::getStruct(); $type->setAttribute(TypeAbstract::ATTR_CLASS, PopoClass::class); (new TypeVisitor($validator))->visitStruct((object) ['foo' => 'bar', 'bar' => 'foo'], $type, '/foo/bar'); } public function testVisitMap() { $type = TypeFactory::getMap() ->setAttribute(TypeAbstract::ATTR_CLASS, ArrayAccessClass::class) ->setAdditionalProperties(TypeFactory::getString()); // array access class $record = (new TypeVisitor())->visitMap((object) ['foo' => 'bar', 'bar' => 'foo'], $type, ''); $this->assertInstanceOf(ArrayAccessClass::class, $record); $this->assertEquals(['foo' => 'bar', 'bar' => 'foo'], $record->getArrayCopy()); // record class $type->setAttribute(TypeAbstract::ATTR_CLASS, RecordClass::class); $record = (new TypeVisitor())->visitMap((object) ['foo' => 'bar', 'bar' => 'foo'], $type, ''); $this->assertInstanceOf(RecordClass::class, $record); $this->assertEquals(['foo' => 'bar', 'bar' => 'foo'], $record->getProperties()); } public function testVisitDateTime() { $type = TypeFactory::getDateTime(); $this->assertInstanceOf('DateTime', (new TypeVisitor())->visitDateTime('2002-10-10T17:00:00Z', $type, '')); $this->assertInstanceOf('DateTime', (new TypeVisitor())->visitDateTime('2002-10-10T17:00:00+01:00', $type, '')); } public function testVisitDateTimeInvalidFormat() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Must be valid date time format'); $type = TypeFactory::getDateTime(); (new TypeVisitor())->visitDateTime('foo', $type, ''); } public function testVisitDateTimeValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function (DateTime $data) { return $data->format('d') == 8; }]) ]); $type = TypeFactory::getDateTime(); (new TypeVisitor($validator))->visitDateTime('2002-10-10T17:00:00Z', $type, '/foo/bar'); } public function testVisitDate() { $type = TypeFactory::getDate(); $this->assertInstanceOf(Date::class, (new TypeVisitor())->visitDate('2000-01-01', $type, '')); $this->assertInstanceOf(Date::class, (new TypeVisitor())->visitDate('2000-01-01+13:00', $type, '')); } public function testVisitDateInvalidFormat() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Must be valid date format'); $type = TypeFactory::getDate(); (new TypeVisitor())->visitDate('foo', $type, ''); } public function testVisitDateValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function (Date $data) { return $data->format('d') == 8; }]) ]); $type = TypeFactory::getDate(); (new TypeVisitor($validator))->visitDate('2002-10-10', $type, '/foo/bar'); } public function testVisitDuration() { $type = TypeFactory::getDuration(); $this->assertInstanceOf(Duration::class, (new TypeVisitor())->visitDuration('P1D', $type, '')); $this->assertInstanceOf(Duration::class, (new TypeVisitor())->visitDuration('P1DT12H', $type, '')); } public function testVisitDurationInvalidFormat() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Must be duration forma'); $type = TypeFactory::getDuration(); (new TypeVisitor())->visitDuration('foo', $type, ''); } public function testVisitDurationValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function (Duration $data) { return $data->d == 2; }]) ]); $type = TypeFactory::getDuration(); (new TypeVisitor($validator))->visitDuration('P1D', $type, '/foo/bar'); } public function testVisitNumber() { $type = TypeFactory::getNumber(); $this->assertSame(1.1, (new TypeVisitor())->visitNumber(1.1, $type, '')); } public function testVisitNumberValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function ($data) { return $data < 2.2; }]) ]); $type = TypeFactory::getNumber(); (new TypeVisitor($validator))->visitNumber(12.34, $type, '/foo/bar'); } public function testVisitInteger() { $type = TypeFactory::getInteger(); $this->assertSame(1, (new TypeVisitor())->visitNumber(1, $type, '')); } public function testVisitIntegerValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function ($data) { return $data < 2; }]) ]); $type = TypeFactory::getInteger(); (new TypeVisitor($validator))->visitInteger(12, $type, '/foo/bar'); } public function testVisitString() { $type = TypeFactory::getString(); $this->assertSame('foo', (new TypeVisitor())->visitString('foo', $type, '')); } public function testVisitStringValidate() { $this->expectException(ValidationException::class); $this->expectExceptionMessage('/foo/bar has an invalid length min 8 and max 16 signs'); $validator = new Validator([ new Field('/foo/bar', [new Filter\Length(8, 16)]) ]); $type = TypeFactory::getString(); (new TypeVisitor($validator))->visitString('foo', $type, '/foo/bar'); } public function testVisitTime() { $type = TypeFactory::getTime(); $this->assertInstanceOf(Time::class, (new TypeVisitor())->visitTime('10:00:00', $type, '')); $this->assertInstanceOf(Time::class, (new TypeVisitor())->visitTime('10:00:00+02:00', $type, '')); } public function testVisitTimeInvalidFormat() { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Must be valid time format'); $type = TypeFactory::getTime(); (new TypeVisitor())->visitTime('foo', $type, ''); } public function testVisitTimeValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function (Time $data) { return $data->format('H') == 11; }]) ]); $type = TypeFactory::getTime(); (new TypeVisitor($validator))->visitTime('10:00:00', $type, '/foo/bar'); } public function testVisitUri() { $type = TypeFactory::getUri(); $this->assertInstanceOf(Uri::class, (new TypeVisitor())->visitUri('/foo', $type, '')); $this->assertInstanceOf(Uri::class, (new TypeVisitor())->visitUri('http://foo.com?foo=bar', $type, '')); } public function testVisitUriValidate() { $this->expectException(ValidationException::class); $validator = new Validator([ new Field('/foo/bar', [function (Uri $data) { return $data->getAuthority() == 'bar.com'; }]) ]); $type = TypeFactory::getUri(); (new TypeVisitor($validator))->visitUri('http://foo.com?foo=bar', $type, '/foo/bar'); } } __halt_compiler();----SIGNATURE:----MW/+OyYVnoIz44iQDwyh5Y0irD0m+Hiy+O0wihfu9J0sI0yAqcvV7godyPYbQLNN/Daem5SC9nRuYFkZ6llEL/dPPO0RtYfZH0zX2OTPU6eIpuPyyLm5Em/rWYuR6tGU+Z2910hJ2S79eRSqM/1gpSceNxguJhOSvkIVU1XtgSA+DfQ9RPopvFfX6JnQFAcRGVLldh6WHDMPHwlAkFY5sF4oUXinNuZsNeNwYUqHiUkMjTM8hREM+3woPBrOWfu1SewJz4tN3w2qXSDlDi9QyXGQYz7lmk39EvE5BiSdsAtfL/cwqORbJsnjpZP0KPePDUhnYWBlA+hVNpLXXxmulBhT2XHqX5YO2cnPpi8Al0vAUjoqsObCK0PvlN1gY5YXy/lEL9fy5rXpzV2mx1i4qd9ouv5MQN3TSsQkHOrg68l8hZGKF+oia1Grcwyl/nOkZUcbKtV8nPG/YSwinBweMGfyTpyYmXK4pQ0NGX/YEyoOJwgd0ySpArJI+4ODrj5GJi7Xg+XOWiMtcmBBA/f0KSibW5RVPTM9XKPDQjeU+hmqi002ay6H7xBC1QMBoUmj9KScg5dxepEj31oq3P2jlHyBQgEmVg2+o7V7EZsIb5ewypJa55Xw4CVQEe0PiDjnvQqjfzYO/Hi/FB1O5DmSUVbRyjl0h6J9Q0Es0lBqHEU=----ATTACHMENT:----ODgwMTUwNzgwODQ3NDMyMCA1NTQ0NDQwMjI0MDk4NTkyIDExMDA1NTY0MTI0MTgyMTg=