Skip to content
This repository was archived by the owner on Jan 1, 2020. It is now read-only.

[WIP] Twig extension testing, fix nontype collections #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"keywords": ["rdfa"],
"version": "0.0.1",
"license": "LGPL",
"minimum-stability": "dev",
"authors": [
{
"name": "Andreas Flack",
Expand All @@ -19,5 +20,8 @@
"psr-0": {
"Midgard\\CreatePHP": "src"
}
},
"require": {
"doctrine/phpcr-odm": "1.0.*"
}
}
9 changes: 6 additions & 3 deletions src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Test/Midgard/CreatePHP/Child.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Test\Midgard\CreatePHP;

class Child
{
public function getTitle()
{
return 'child title';
}
public function getContent()
{
return 'child content';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Test\Midgard\CreatePHP\Extension\Twig;

use Doctrine\Common\Annotations\AnnotationRegistry;

use Midgard\CreatePHP\Extension\Twig\CreatephpExtension;
use Midgard\CreatePHP\Extension\Twig\CreatephpNode;

use Midgard\CreatePHP\Metadata\RdfDriverXml;
use Midgard\CreatePHP\Metadata\RdfTypeFactory;

use Test\Midgard\CreatePHP\Model;

class CreatephpExtensionTest extends \PHPUnit_Framework_TestCase
Expand All @@ -14,20 +19,46 @@ class CreatephpExtensionTest extends \PHPUnit_Framework_TestCase
*/
private $mapper;
/**
* @var \Midgard\CreatePHP\Metadata\RdfTypeFactory
* @var RdfTypeFactory
*/
private $factory;
/** @var \Twig_Environment */
private $twig;

/**
* @var RdfDriverXml
*/
private $driver;

protected function setUp()
{
global $autoload;
if (!class_exists('Twig_Environment')) {
$this->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'));
Expand All @@ -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'))
Expand All @@ -50,7 +82,7 @@ public function testNode()
->method('createSubject')
->will($this->returnValue('/the/subject'))
;

*/
$xml = $this->renderXml('node.twig');

$this->assertCompiledCorrectly($xml);
Expand All @@ -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'))
Expand All @@ -72,6 +105,7 @@ public function testNodeAs()
->method('createSubject')
->will($this->returnValue('/the/subject'))
;
*/

$xml = $this->renderXml('node_as.twig');

Expand All @@ -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'))
Expand All @@ -95,6 +129,7 @@ public function testFunctions()
->method('createSubject')
->will($this->returnValue('/the/subject'))
;
*/

$xml = $this->renderXml('functions.twig');

Expand Down
41 changes: 0 additions & 41 deletions tests/Test/Midgard/CreatePHP/Extension/Twig/RdfTypeFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Test\Midgard\CreatePHP\Metadata;

use Midgard\CreatePHP\RdfMapperInterface;
use Midgard\CreatePHP\Metadata\RdfDriverXml;
use Midgard\CreatePHP\Metadata\RdfTypeFactory;
use Midgard\CreatePHP\Entity\Controller;

use Test\Midgard\CreatePHP\Model;

class FunctionalRdfDriverXmlTest extends RdfDriverBase
{
/**
* @var \Midgard\CreatePHP\Metadata\RdfDriverInterface
*/
private $driver;

private $mapper;

/**
* @var RdfTypeFactory
*/
private $factory;

public function setUp()
{
$this->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);
}
}
3 changes: 2 additions & 1 deletion tests/Test/Midgard/CreatePHP/Metadata/RdfDriverXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<type
xmlns:sioc="http://rdfs.org/sioc/ns#"
xmlns:dcterms="http://purl.org/dc/terms/"
typeof="sioc:Item"
>
<children>
<property property="dcterms:title" identifier="title" tag-name="h2"/>
</children>

</type>
10 changes: 10 additions & 0 deletions tests/Test/Midgard/CreatePHP/Metadata/rdf/string.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<type
xmlns:sioc="http://rdfs.org/sioc/ns#"
xmlns:dcterms="http://purl.org/dc/terms/"
typeof="sioc:Post"
tag-name="li"
>
<children>
<property property="dcterms:tag" tag-name="span"/>
</children>
</type>
8 changes: 8 additions & 0 deletions tests/Test/Midgard/CreatePHP/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ public function getContent()
{
return 'the content';
}
public function getTags()
{
return array('test', 'php');
}
public function getChildren()
{
return array(new Child());
}
}
6 changes: 5 additions & 1 deletion tests/__files/MockMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']))
{
Expand Down