Browse Source

adding unit tests for ioc changes

Kelt Dockins 11 years ago
parent
commit
1ed7ec98ba
1 changed files with 79 additions and 0 deletions
  1. 79 0
      laravel/tests/cases/ioc.test.php

+ 79 - 0
laravel/tests/cases/ioc.test.php

@@ -1,5 +1,34 @@
 <?php
 
+/**
+ * Testing Optional Parameters in classes' Dependency Injection
+ */
+class TestOptionalParamClassForIoC
+{
+	public function __construct($optional_param = 42) {}
+}
+
+/**
+ * Testing Dependency Injection with this class
+ */
+class TestClassOneForIoC
+{
+	public $_variable;
+}
+
+/**
+ * Testing Dependency Injection of ClassOne
+ */
+class TestClassTwoForIoC
+{
+	public $class_one;
+	public function __construct(TestClassOneForIoC $class_one)
+	{
+		$this->class_one = $class_one;
+	}
+}
+
+
 class IoCTest extends PHPUnit_Framework_TestCase {
 
 	/**
@@ -71,4 +100,54 @@ class IoCTest extends PHPUnit_Framework_TestCase {
 		$this->assertTrue(IoC::registered('controller: ioc.test'));
 	}
 
+	/**
+	 * Test that classes with optional parameters can resolve
+	 */
+	public function testOptionalParamClassResolves()
+	{
+		$test = IoC::resolve('TestOptionalParamClassForIoC');
+		$this->assertInstanceOf('TestOptionalParamClassForIoC', $test);
+	}
+
+	/**
+	 * Test that we can resolve TestClassOneForIoC using IoC
+	 */
+	public function testClassOneForIoCResolves()
+	{
+		$test = IoC::resolve('TestClassOneForIoC');
+		$this->assertInstanceOf('TestClassOneForIoC', $test);
+	}
+
+	/**
+	 * Test that we can resolve TestClassTwoForIoC
+	 */
+	public function testClassTwoForIoCResolves()
+	{
+		$test = IoC::resolve('TestClassTwoForIoC');
+		$this->assertInstanceOf('TestClassTwoForIoC', $test);
+	}
+
+	/**
+	 * Test that when we resolve TestClassTwoForIoC we auto resolve
+	 * the dependency for TestClassOneForIoC
+	 */
+	public function testClassTwoResolvesClassOneDependency()
+	{
+		$test = IoC::resolve('TestClassTwoForIoC');
+		$this->assertInstanceOf('TestClassOneForIoC', $test->TestClassOneForIoC);
+	}
+
+	/**
+	 * Test that when we resolve TestClassTwoForIoC with a parameter
+	 * that it actually uses that instead of a blank class TestClassOneForIoC
+	 */
+	public function testClassTwoResolvesClassOneWithArgument()
+	{
+		$class_one = IoC::resolve('TestClassOneForIoC');
+		$class_one->test_variable = 42;
+
+		$class_two = IoC::resolve('TestClassTwoForIoC', [$class_one]);
+		$this->assertEquals(42, $class_two->class_one->test_variable);
+	}
+
 }