Skip to content
Browse files

documentation cleanup and additional tests

  • Loading branch information...
1 parent 8bbc199 commit 62de08d379e6f6f46403710a16a379c5e55615c3 Jonathan Moss committed
View
22 build.xml
@@ -29,10 +29,12 @@
<phpdoc title="${package.title} Documentation" destdir="docs/api" sourcecode="yes"
output="HTML:frames:DOM/default" quiet="true" defaultpackagename="${package.default}">
<fileset refid="source" />
+ <!--
<fileset dir="src/tutorials">
<include name="**/*.pkg" />
<include name="**/*.cls" />
</fileset>
+ -->
</phpdoc>
</target>
@@ -54,13 +56,7 @@
<target name="coverage-report" description="generates unit test coverage report">
<delete dir="docs/coverage" quiet="true" includeemptydirs="true" verbose="false" failonerror="false" />
<mkdir dir="docs/coverage" />
- <exec command="phpunit --coverage-html docs/coverage/ tests/unit-tests/UnitTests.php" checkreturn="false" />
- </target>
-
- <target name="bdd-report" description="generates bdd testdox report">
- <delete dir="docs/bdd_report" quiet="true" includeemptydirs="true" verbose="false" failonerror="false" />
- <mkdir dir="docs/bdd_report" />
- <exec command="phpunit --testdox-html docs/bdd_report/index.html tests/bdd/BDDTests.php" checkreturn="false" />
+ <exec command="phpunit --coverage-html docs/coverage/ tests/unit/UnitTests.php" checkreturn="false" />
</target>
<target name="integration-report" description="Integration test report">
@@ -95,6 +91,16 @@
<mkdir dir="export/tests/unit" />
<copy todir="export/tests/unit">
<fileset dir="tests/unit">
+ <include name="**/*.php" />
+ </fileset>
+ <filterchain>
+ <stripwhitespace />
+ </filterchain>
+ </copy>
+
+ <mkdir dir="export/tests/fixtures" />
+ <copy todir="export/tests/fixtures">
+ <fileset dir="tests/fixtures">
<include name="**/*" />
</fileset>
<filterchain>
@@ -126,6 +132,6 @@
<doxygen config="doxygen.conf"/>
</target>
- <target name="build-all" depends="lint-source, unit-report, bdd-report, coverage-report, api-docs, package, integration-report"></target>
+ <target name="build-all" depends="lint-source, unit-report, coverage-report, api-docs, package, integration-report"></target>
</project>
View
40 src/freebase/DomFactory.php
@@ -13,28 +13,35 @@
class DomFactory
{
- /**
+ /**
* @param string $json
* @return \freebase\Node
*/
public static function createDomFromJson($json, $id = null)
{
- $node = null;
+ $factory = new self;
+ return $factory->jsonToDom($json, $id);
+ }
+
+ /**
+ * @param string $json
+ * @param string $id
+ * @return Node
+ */
+ public function jsonToDom($json, $id = null)
+ {
+ $root = null;
$data = \json_decode($json, true);
if (null === $data) {
throw new \freebase\exception\InvalidJson($json);
}
- if (self::getResponseCode($data, $id) == Constants::API_RESPONSE_CODE_OK ) {
- if (null === $id) {
- $node = self::createNode(self::getResultNode($data), 'root');
- } else {
- $node = self::createNode(self::getResultNode($data, $id), $id);
- }
-
+ if ($this->getResponseCode($data, $id) == Constants::API_RESPONSE_CODE_OK ) {
+ $root = $this->createNode($this->getResultNode($data, $id), $id);
} else {
+ //@todo error parsing requires a little more work methinks
throw new \freebase\exception\ApiError("");
}
- return $node;
+ return $root;
}
/**
@@ -43,7 +50,7 @@ public static function createDomFromJson($json, $id = null)
* @param string $id
* @return string
*/
- protected static function getResponseCode(array $data, $id = null)
+ protected function getResponseCode(array $data, $id = null)
{
$code = null;
if (null == $id) {
@@ -54,7 +61,12 @@ protected static function getResponseCode(array $data, $id = null)
return $code;
}
- protected static function getResultNode(array $data, $id = null)
+ /**
+ * @param array $data
+ * @param string $id
+ * @return array
+ */
+ protected function getResultNode(array $data, $id = null)
{
$result = null;
if (null == $id) {
@@ -70,14 +82,14 @@ protected static function getResultNode(array $data, $id = null)
* @param string $name
* @return \freebase\Node
*/
- protected static function createNode(array $data, $name = null)
+ protected function createNode(array $data, $name = null)
{
$node = new Node($name);
foreach ($data as $key => $value) {
if (\is_scalar($value)) {
$node->setAttributeValue($key, $value);
} else {
- $node->addChild(self::createNode($value, $key));
+ $node->addChild($this->createNode($value, $key));
}
}
return $node;
View
7 src/freebase/Node.php
@@ -168,6 +168,10 @@ public function getChildByName($name)
return $node;
}
+ /**
+ * @param string $path Names should be separated by '.' characters
+ * @return \freebase\Node
+ */
public function getChildByPath($path)
{
$nodeList = \explode(".", $path);
@@ -188,6 +192,9 @@ public function getChildByPath($path)
// IteratorAggregate INTERFACE //
/////////////////////////////////
+ /**
+ * @return \ArrayIterator
+ */
public function getIterator()
{
return new \ArrayIterator($this->children);
View
5 tests/unit/UnitTests.php
@@ -1,11 +1,16 @@
<?php
require_once dirname(__FILE__).'/freebase/TestQuery.php';
+require_once dirname(__FILE__).'/freebase/TestNode.php';
+require_once dirname(__FILE__).'/freebase/TestDomFactory.php';
+
class UnitTests
{
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('Freebase tests');
$suite->addTestSuite('freebase\\TestQuery');
+ $suite->addTestSuite('freebase\\TestNode');
+ $suite->addTestSuite('freebase\\TestDomFactory');
return $suite;
}
}
View
64 tests/unit/freebase/TestDomFactory.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * @package
+ * @copyright 2011 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+
+require_once \dirname(__FILE__) . '/../../../src/freebase/Exception.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/exception/InvalidJson.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/exception/InvalidPath.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/Constants.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/Node.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/DomFactory.php';
+
+/**
+ * Description of TestQuery
+ *
+ * @package
+ */
+class TestDomFactory extends \PHPUnit_Framework_TestCase
+{
+
+ public function testValidJsonIsParsedIntoNodes()
+ {
+ $factory = new DomFactory();
+ $json = \file_get_contents(\dirname(__FILE__) . '/../../fixtures/topic.json');
+ $root = $factory->jsonToDom($json);
+ $this->assertInstanceOf('freebase\\Node', $root);
+ }
+
+ public function testValidJsonIsParsedIntoCorrectDom()
+ {
+ $factory = new DomFactory();
+ $json = \file_get_contents(\dirname(__FILE__) . '/../../fixtures/topic.json');
+ $root = $factory->jsonToDom($json);
+ $this->assertSame('Along came a spider', $root->text);
+ $this->assertSame('properties', $root->getChildByName('properties')->getName());
+ $this->assertSame('Along Came a Spider', $root->getChildByPath('properties./book/book_edition/book.values.0')->text);
+ }
+
+ public function testStaticMethodWorks()
+ {
+ $factory = new DomFactory();
+ $json = \file_get_contents(\dirname(__FILE__) . '/../../fixtures/topic.json');
+ $root = DomFactory::createDomFromJson($json);
+ $this->assertSame('Along came a spider', $root->text);
+ $this->assertSame('properties', $root->getChildByName('properties')->getName());
+ $this->assertSame('Along Came a Spider', $root->getChildByPath('properties./book/book_edition/book.values.0')->text);
+ }
+
+ public function testInvalidJsonThrowsAnException()
+ {
+ $json = 'a load of crap';
+ $factory = new DomFactory();
+ $this->setExpectedException('freebase\\exception\\InvalidJson');
+ $factory->jsonToDom($json);
+ }
+
+
+
+
+}
View
11 tests/unit/freebase/TestNode.php
@@ -7,16 +7,16 @@
*/
namespace freebase;
-require \dirname(__FILE__) . '/../../../src/freebase/Exception.php';
-require \dirname(__FILE__) . '/../../../src/freebase/exception/InvalidPath.php';
-require \dirname(__FILE__) . '/../../../src/freebase/Node.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/Exception.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/exception/InvalidPath.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/Node.php';
/**
* Description of TestQuery
*
* @package
*/
-class TestQuery extends \PHPUnit_Framework_TestCase
+class TestNode extends \PHPUnit_Framework_TestCase
{
public function testNewNodeHasNoChildren()
@@ -93,6 +93,9 @@ public function testRetrievingChildByPath()
$node->addChild($c1);
$this->assertSame($c2, $node->getChildByPath("one.two"));
+
+ $this->setExpectedException('\\freebase\\exception\\InvalidPath');
+ $node->getChildByPath("one.zombies.eat.brains");
}
public function testThatICanGetANodesParent()
View
6 tests/unit/freebase/TestQuery.php
@@ -7,9 +7,9 @@
*/
namespace freebase;
-require \dirname(__FILE__) . '/../../../src/freebase/Exception.php';
-require \dirname(__FILE__) . '/../../../src/freebase/exception/InvalidQuery.php';
-require \dirname(__FILE__) . '/../../../src/freebase/Query.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/Exception.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/exception/InvalidQuery.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/Query.php';
/**
* Description of TestQuery

0 comments on commit 62de08d

Please sign in to comment.
Something went wrong with that request. Please try again.