diff --git a/composer.json b/composer.json index 25e3f48..052b252 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "keywords": ["rdfa"], "version": "0.0.1", "license": "LGPL", + "minimum-stability": "dev", "authors": [ { "name": "Andreas Flack", @@ -19,5 +20,8 @@ "psr-0": { "Midgard\\CreatePHP": "src" } + }, + "require": { + "doctrine/phpcr-odm": "1.0.*" } } diff --git a/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php b/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php index ae2dd99..ce9fc6e 100644 --- a/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php +++ b/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php @@ -43,9 +43,12 @@ public function __construct(RdfMapperInterface $mapper, RdfDriverInterface $driv public function getTypeByObject($object) { - return $this->getTypeByName( - $this->driver->objectToName($object, $this->mapper) - ); + if (is_object($object)) { + $name = $this->driver->objectToName($object, $this->mapper); + } else { + $name = 'string'; + } + return $this->getTypeByName($name); } /** diff --git a/tests/Test/Midgard/CreatePHP/Child.php b/tests/Test/Midgard/CreatePHP/Child.php new file mode 100644 index 0000000..1dd4041 --- /dev/null +++ b/tests/Test/Midgard/CreatePHP/Child.php @@ -0,0 +1,15 @@ +markTestSkipped('Twig is not installed.'); } - $this->mapper = $this->getMock('Midgard\CreatePHP\RdfMapperInterface'); - $this->factory = new RdfTypeFactory($this->mapper); + AnnotationRegistry::registerLoader(function($class) use ($autoload) { + $autoload->loadClass($class); + return class_exists($class, false); + }); + AnnotationRegistry::registerFile(__DIR__.'/../../../../../../vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Mapping/Annotations/DoctrineAnnotations.php'); + + $reader = new \Doctrine\Common\Annotations\AnnotationReader(); + $driver = new \Doctrine\ODM\PHPCR\Mapping\Driver\AnnotationDriver($reader, array('../../')); + + + $this->driver = new RdfDriverXml(array(__DIR__ . '/../../Metadata/rdf')); + $documentManager = \Doctrine\ODM\PHPCR\DocumentManager::create($this->getMock('PHPCR\\SessionInterface'), new \Doctrine\ODM\PHPCR\Configuration()); + $registry = $this->getMock('Doctrine\\Common\\Persistence\\ManagerRegistry'); + $registry + ->expects($this->any()) + ->method('getManager') + ->will($this->returnValue($documentManager)) + ; + + $this->mapper = new \Midgard\CreatePHP\Mapper\DoctrinePhpcrOdmMapper($this->driver->getAllNames(), $registry); + + $this->factory = new RdfTypeFactory($this->mapper, $this->driver); $this->twig = new \Twig_Environment(); $this->twig->setLoader(new \Twig_Loader_Filesystem(__DIR__.'/templates')); @@ -38,6 +69,7 @@ public function testNode() { $this->twig->addGlobal('mymodel', new Model); + /* $this->mapper->expects($this->any()) ->method('getPropertyValue') ->will($this->returnValue('content text')) @@ -50,7 +82,7 @@ public function testNode() ->method('createSubject') ->will($this->returnValue('/the/subject')) ; - + */ $xml = $this->renderXml('node.twig'); $this->assertCompiledCorrectly($xml); @@ -60,6 +92,7 @@ public function testNode() public function testNodeAs() { $this->twig->addGlobal('mymodel', new Model); + /* $this->mapper->expects($this->any()) ->method('getPropertyValue') ->will($this->returnValue('content text')) @@ -72,6 +105,7 @@ public function testNodeAs() ->method('createSubject') ->will($this->returnValue('/the/subject')) ; + */ $xml = $this->renderXml('node_as.twig'); @@ -82,7 +116,7 @@ public function testFunctions() { $this->twig->addGlobal('mymodel', new Model); - $this->twig->addGlobal('mymodel', new Model); + /* $this->mapper->expects($this->any()) ->method('getPropertyValue') ->will($this->returnValue('content text')) @@ -95,6 +129,7 @@ public function testFunctions() ->method('createSubject') ->will($this->returnValue('/the/subject')) ; + */ $xml = $this->renderXml('functions.twig'); diff --git a/tests/Test/Midgard/CreatePHP/Extension/Twig/RdfTypeFactory.php b/tests/Test/Midgard/CreatePHP/Extension/Twig/RdfTypeFactory.php deleted file mode 100644 index 1702157..0000000 --- a/tests/Test/Midgard/CreatePHP/Extension/Twig/RdfTypeFactory.php +++ /dev/null @@ -1,41 +0,0 @@ -mapper = $mapper; - } - - /** - * Get the type if this is the expected model class - */ - public function getTypeByObject($class) { - if ($class instanceof Model) { - $type = new Type($this->mapper); - $type->setVocabulary('dcterms', 'http://purl.org/dc/terms/'); - $prop = new \Midgard\CreatePHP\Entity\Property('title', array()); - $prop->setAttributes(array('property' => 'dcterms:title')); - $type->title = $prop; - return $type; - } - - throw new \Exception('No type found for ' . get_class($class)); - } - -} \ No newline at end of file diff --git a/tests/Test/Midgard/CreatePHP/Metadata/FunctionalRdfDriverXmlTest.php b/tests/Test/Midgard/CreatePHP/Metadata/FunctionalRdfDriverXmlTest.php new file mode 100644 index 0000000..487355e --- /dev/null +++ b/tests/Test/Midgard/CreatePHP/Metadata/FunctionalRdfDriverXmlTest.php @@ -0,0 +1,46 @@ +driver = new RdfDriverXml(array(__DIR__ . DIRECTORY_SEPARATOR . 'rdf')); + $this->mapper = new \Midgard\CreatePHP\tests\MockMapper(); + + $this->factory = new RdfTypeFactory($this->mapper, $this->driver); + } + + public function testBind() + { + $type = $this->factory->getTypeByRdf('http://rdfs.org/sioc/ns#Post'); + $this->assertInstanceOf('Midgard\\CreatePHP\\Entity\\EntityInterface', $type); + $object = array( + 'title' => 'title', + 'content' => 'content', + 'tags' => array('tee', 'too'), + 'children' => array('title' => 'child', 'content' => 'childcontent'), + ); + $type->createWithObject($object); + } +} diff --git a/tests/Test/Midgard/CreatePHP/Metadata/RdfDriverXmlTest.php b/tests/Test/Midgard/CreatePHP/Metadata/RdfDriverXmlTest.php index 2724c51..35972e1 100644 --- a/tests/Test/Midgard/CreatePHP/Metadata/RdfDriverXmlTest.php +++ b/tests/Test/Midgard/CreatePHP/Metadata/RdfDriverXmlTest.php @@ -53,9 +53,10 @@ public function testLoadTypeForClassNodefinition() public function testGetAllNames() { $map = $this->driver->getAllNames(); - $this->assertCount(1, $map); + $this->assertCount(2, $map); $types = array( 'http://rdfs.org/sioc/ns#Post' => 'Test\\Midgard\\CreatePHP\\Model', + 'http://rdfs.org/sioc/ns#Item' => 'Test\\Midgard\\CreatePHP\\Child', ); $this->assertEquals($types, $map); } diff --git a/tests/Test/Midgard/CreatePHP/Metadata/rdf/Test.Midgard.CreatePHP.Child.xml b/tests/Test/Midgard/CreatePHP/Metadata/rdf/Test.Midgard.CreatePHP.Child.xml new file mode 100644 index 0000000..55014bc --- /dev/null +++ b/tests/Test/Midgard/CreatePHP/Metadata/rdf/Test.Midgard.CreatePHP.Child.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/tests/Test/Midgard/CreatePHP/Metadata/rdf/string.xml b/tests/Test/Midgard/CreatePHP/Metadata/rdf/string.xml new file mode 100644 index 0000000..bde9c46 --- /dev/null +++ b/tests/Test/Midgard/CreatePHP/Metadata/rdf/string.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/tests/Test/Midgard/CreatePHP/Model.php b/tests/Test/Midgard/CreatePHP/Model.php index d1b582f..051c587 100644 --- a/tests/Test/Midgard/CreatePHP/Model.php +++ b/tests/Test/Midgard/CreatePHP/Model.php @@ -12,4 +12,12 @@ public function getContent() { return 'the content'; } + public function getTags() + { + return array('test', 'php'); + } + public function getChildren() + { + return array(new Child()); + } } \ No newline at end of file diff --git a/tests/__files/MockMapper.php b/tests/__files/MockMapper.php index c5a8e0b..90be8ef 100644 --- a/tests/__files/MockMapper.php +++ b/tests/__files/MockMapper.php @@ -33,10 +33,14 @@ public function isEditable($object) public function getChildren($object, CollectionInterface $collection) { + if (isset($object[$collection->getIdentifier()])) + { + return $object[$collection->getIdentifier()]; + } $config = $collection->getConfig(); if (empty($config['is_child'])) { - throw new \Exception('wrong config'); + throw new \Exception('Wrong configuration or missing data in object array for field "' . $collection->getIdentifier() . '"'); } if (isset($object['children'])) {