Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

testing factory, removing inconsistency and adjusting NodeTypeXmlConv…

…ert to conform to the standard
  • Loading branch information...
commit 9a44b942a721714c4df018f0cf16e71add8c90bc 1 parent 5c373cd
@dbu dbu authored
View
28 src/Jackalope/Factory.php
@@ -8,6 +8,11 @@
* them easily replaceable with dummies for the unit and functional testing.
* It is injected in the constructor of every class of the framework.
*
+ * The Jackalope namespace is automatically prepended in order to allow for
+ * other factories to instantiate objects in a different namspace.
+ * If no class with the requested name exists in the Jackalope namespace, the
+ * factory tries to use a class in global namespace.
+ *
* It should be used in the code like this:
* <pre>
* $this->factory->get('Node', array(...));
@@ -15,27 +20,30 @@
* //note the \ for sub namespaces. the name is relative to the jackalope namespace
* </pre>
*
- * The result will be an object for jackalope with the given named params.
+ * The result will be an object for jackalope with the factory and given paramaters.
*
- * Note that the factory passes itself to every object it creates, to give them
- * a reference to itselves.
+ * Note that the factory passes itself to every object it creates as the first
+ * argument, to give them a reference to itselves.
*/
class Factory
{
/**
- * Factory
+ * Get the object with the class name created with the factory plus any
+ * passed parameters.
+ *
+ * @param $name string class name with sub-namespace inside the Jackalope
+ * namespace
+ * @param $params array Parameters in order of their appearance in the
+ * constructor. The factory will prepend itself to this list.
*
- * @param $name string: Model name.
- * @param $params array: Parameters in order of their appearance in the constructor.
* @return object
*/
- public function get($name, $params = array())
+ public function get($name, array $params = array())
{
if (class_exists('Jackalope\\' . $name)) {
$name = 'Jackalope\\' . $name;
- }
- if (count($params) == 0) {
- return new $name;
+ } elseif (! class_exists($name)) {
+ throw new \InvalidArgumentException("Neither class Jackalope\\$name nor class $name found. Please check your autoloader and the spelling of $name");
}
$class = new \ReflectionClass($name);
View
10 src/Jackalope/NodeType/NodeTypeXmlConverter.php
@@ -28,6 +28,16 @@ class NodeTypeXmlConverter
const DEFAULT_PRIMARY_NODE = 'nt:base';
/**
+ * Empty constructor.
+ *
+ * Everything inside jackalope has to accept the factory in the
+ * constructor. We define the constructor but to nothing at all.
+ */
+ public function __construct($factory)
+ {
+ }
+
+ /**
* @param \DOMElement $node
*
* @return array
View
37 tests/Jackalope/FactoryTest.php
@@ -4,8 +4,41 @@
class FactoryTest extends TestCase
{
- public function testDummy()
+ protected $factory;
+
+ public function setUp()
+ {
+ $this->factory = new \Jackalope\Factory;
+ }
+
+ public function testJackalope()
+ {
+ $reg = $this->factory->get('NamespaceRegistry', array($this->getMock('Jackalope\TransportInterface')));
+ $this->assertInstanceOf('Jackalope\NamespaceRegistry', $reg);
+ }
+
+ public function testOutside()
{
- $this->markTestSkipped('No tests for this class yet');
+ $dummy = $this->factory->get('Other\TestDummy');
+ $this->assertInstanceOf('Other\TestDummy', $dummy);
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testNotexisting()
+ {
+ $this->factory->get('ClassNotExisting');
}
}
+
+namespace Other;
+class TestDummy
+{
+ public function __construct($factory)
+ {
+ if (! $factory instanceof \Jackalope\Factory) {
+ throw new \Exception('not a valid factory as first argument');
+ }
+ }
+}
View
3  tests/Jackalope/NodeType/NodeTypeXmlConverterTest.php
@@ -10,7 +10,8 @@ class NodeTypeXmlConverterDefinitionTest extends TestCase
public function setUp()
{
- $this->converter = new NodeTypeXmlConverter();
+ $factory = new \Jackalope\Factory;
+ $this->converter = new NodeTypeXmlConverter($factory);
}
public function testConvertNtBase()
View
2  tests/Jackalope/TestCase.php
@@ -92,7 +92,7 @@ protected function getNodeTypeManager()
$factory = new \Jackalope\Factory;
$dom = new \DOMDocument();
$dom->load(dirname(__FILE__) . '/../fixtures/nodetypes.xml');
- $converter = new \Jackalope\NodeType\NodeTypeXmlConverter;
+ $converter = new \Jackalope\NodeType\NodeTypeXmlConverter($factory);
$om = $this->getObjectManagerMock();
$om->expects($this->any())
->method('getNodeTypes')
Please sign in to comment.
Something went wrong with that request. Please try again.