DiContainerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Elgg\Di;
  3. namespace Elgg\Di;
  4. class DiContainerTest extends \PHPUnit_Framework_TestCase {
  5. const TEST_CLASS = '\Elgg\Di\DiContainerTestObject';
  6. public function getFoo(\Elgg\Di\DiContainer $di) {
  7. return new \Elgg\Di\DiContainerTestObject($di);
  8. }
  9. public function testEmptyContainer() {
  10. $di = new \Elgg\Di\DiContainer();
  11. $this->assertFalse($di->has('foo'));
  12. }
  13. public function testSetValue() {
  14. $di = new \Elgg\Di\DiContainer();
  15. $this->assertSame($di, $di->setValue('foo', 'Foo'));
  16. $this->assertTrue($di->has('foo'));
  17. $this->assertEquals('Foo', $di->foo);
  18. }
  19. public function testSetFactoryShared() {
  20. $di = new \Elgg\Di\DiContainer();
  21. $this->assertSame($di, $di->setFactory('foo', array($this, 'getFoo')));
  22. $this->assertTrue($di->has('foo'));
  23. $foo1 = $di->foo;
  24. $foo2 = $di->foo;
  25. $this->assertInstanceOf(self::TEST_CLASS, $foo1);
  26. $this->assertSame($foo1, $foo2);
  27. }
  28. public function testSetFactoryUnshared() {
  29. $di = new \Elgg\Di\DiContainer();
  30. $this->assertSame($di, $di->setFactory('foo', array($this, 'getFoo'), false));
  31. $this->assertTrue($di->has('foo'));
  32. $foo1 = $di->foo;
  33. $foo2 = $di->foo;
  34. $this->assertInstanceOf(self::TEST_CLASS, $foo1);
  35. $this->assertNotSame($foo1, $foo2);
  36. }
  37. public function testContainerIsPassedToFactory() {
  38. $di = new \Elgg\Di\DiContainer();
  39. $di->setFactory('foo', array($this, 'getFoo'));
  40. $foo = $di->foo;
  41. $this->assertSame($di, $foo->di);
  42. }
  43. public function testSetFactoryLooksUncallable() {
  44. $di = new \Elgg\Di\DiContainer();
  45. $this->setExpectedException('InvalidArgumentException', '$factory must appear callable');
  46. $di->setFactory('foo', new \stdClass());
  47. }
  48. public function testGetFactoryUncallable() {
  49. $di = new \Elgg\Di\DiContainer();
  50. $di->setFactory('foo', 'not-a-real-callable');
  51. $this->setExpectedException(
  52. '\Elgg\Di\FactoryUncallableException',
  53. "Factory for 'foo' was uncallable: 'not-a-real-callable'");
  54. $di->foo;
  55. }
  56. public function testGetFactoryUncallableArray() {
  57. $di = new \Elgg\Di\DiContainer();
  58. $di->setFactory('foo', array('fakeClass', 'not-a-real-callable'));
  59. $this->setExpectedException(
  60. '\Elgg\Di\FactoryUncallableException',
  61. "Factory for 'foo' was uncallable: 'fakeClass::not-a-real-callable'");
  62. $di->foo;
  63. }
  64. public function testGetFactoryUncallableArrayObject() {
  65. $di = new \Elgg\Di\DiContainer();
  66. $di->setFactory('foo', array($this, 'not-a-real-callable'));
  67. $this->setExpectedException(
  68. '\Elgg\Di\FactoryUncallableException',
  69. "Factory for 'foo' was uncallable: Elgg\\Di\\DiContainerTest->not-a-real-callable");
  70. $di->foo;
  71. }
  72. public function testGetMissingValue() {
  73. $di = new \Elgg\Di\DiContainer();
  74. $this->setExpectedException('\Elgg\Di\MissingValueException', "Value or factory was not set for: foo");
  75. $di->foo;
  76. }
  77. public function testRemove() {
  78. $di = new \Elgg\Di\DiContainer();
  79. $di->setValue('foo', 'Foo');
  80. $this->assertSame($di, $di->remove('foo'));
  81. $this->assertFalse($di->has('foo'));
  82. }
  83. public function testSetClassNames() {
  84. $di = new \Elgg\Di\DiContainer();
  85. $di->setClassName('foo', self::TEST_CLASS);
  86. $this->assertInstanceOf(self::TEST_CLASS, $di->foo);
  87. $this->setExpectedException('InvalidArgumentException', 'Class names must be valid PHP class names');
  88. $di->setClassName('foo', array());
  89. }
  90. public function testSettingInvalidClassNameThrows() {
  91. $di = new \Elgg\Di\DiContainer();
  92. $euro = "\xE2\x82\xAC";
  93. $di->setClassName('foo1', "Foo2{$euro}3");
  94. $di->setClassName('foo2', "\\Foo2{$euro}3");
  95. $di->setClassName('foo3', "Foo2{$euro}3\\Foo2{$euro}3");
  96. $this->setExpectedException('InvalidArgumentException', 'Class names must be valid PHP class names');
  97. $di->setClassName('foo', 'Not Valid');
  98. }
  99. public function testAccessRemovedValue() {
  100. $di = new \Elgg\Di\DiContainer();
  101. $di->setValue('foo', 'Foo');
  102. $di->remove('foo');
  103. $this->setExpectedException('\Elgg\Di\MissingValueException');
  104. $di->foo;
  105. }
  106. }
  107. class DiContainerTestObject {
  108. public $di;
  109. public function __construct($di = null) {
  110. $this->di = $di;
  111. }
  112. }