PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/core/ObjectStaticTest.php

http://github.com/silverstripe/sapphire
PHP | 132 lines | 79 code | 24 blank | 29 comment | 0 complexity | fc8fb932393bea989fe021b69e521ddc MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Tests various static getter and setter methods on {@link Object}
  4. *
  5. * @package sapphire
  6. * @subpackage tests
  7. */
  8. class ObjectStaticTest extends SapphireTest {
  9. /**
  10. * Tests {@link Object::get_static()}
  11. */
  12. public function testGetStatic() {
  13. $this->assertEquals(Object::get_static('ObjectStaticTest_First', 'first'), array('test_1'));
  14. $this->assertEquals(Object::get_static('ObjectStaticTest_Second', 'first'), array('test_2'));
  15. $this->assertEquals(Object::get_static('ObjectStaticTest_Third', 'first'), array('test_3'));
  16. Object::addStaticVars('ObjectStaticTest_First', array('first' => array('test_1_2')));
  17. Object::addStaticVars('ObjectStaticTest_Third', array('first' => array('test_3_2')));
  18. Object::addStaticVars('ObjectStaticTest_Fourth', array('first' => array('test_4')));
  19. $this->assertEquals(Object::get_static('ObjectStaticTest_First', 'first', true), array('test_1_2', 'test_1'));
  20. $this->assertEquals(Object::get_static('ObjectStaticTest_Second', 'first', true), array('test_1_2', 'test_2'));
  21. $this->assertEquals(Object::get_static('ObjectStaticTest_Third', 'first', true), array('test_1_2', 'test_3_2', 'test_3'));
  22. }
  23. /**
  24. * Test {@link Object::addStaticVar()} correctly replaces static vars
  25. */
  26. public function testAddStaticReplace() {
  27. Object::addStaticVars('ObjectStaticTest_Fourth', array('second' => array('test_4')), true);
  28. Object::addStaticVars('ObjectStaticTest_Third', array('second' => array('test_3_2')));
  29. $this->assertEquals(Object::get_static('ObjectStaticTest_Fourth', 'second', true), array('test_4'));
  30. $this->assertEquals(Object::get_static('ObjectStaticTest_Third', 'second', true), array('test_3_2', 'test_3'));
  31. Object::addStaticVars('ObjectStaticTest_Third', array('second' => array('test_3_2')), true);
  32. $this->assertEquals(Object::get_static('ObjectStaticTest_Third', 'second', true), array('test_3_2'));
  33. Object::add_static_var('ObjectStaticTest_Third', 'fourth', array('test_3_2'));
  34. $this->assertEquals(Object::get_static('ObjectStaticTest_Fourth', 'fourth', true), array('test_3_2', 'test_4'));
  35. Object::add_static_var('ObjectStaticTest_Third', 'fourth', array('test_3_2'), true);
  36. $this->assertEquals(Object::get_static('ObjectStaticTest_Fourth', 'fourth', true), array('test_4', 'test_3_2'));
  37. }
  38. /**
  39. * Tests {@link Object::uninherited_static()}
  40. */
  41. public function testUninherited() {
  42. $this->assertEquals(Object::uninherited_static('ObjectStaticTest_First', 'third', true), 'test_1');
  43. $this->assertEquals(Object::uninherited_static('ObjectStaticTest_Fourth', 'third', true), null);
  44. }
  45. public function testCombinedStatic() {
  46. // test basic operation
  47. $this->assertEquals (
  48. array('test_1', 'test_2', 'test_3'), Object::combined_static('ObjectStaticTest_Combined3', 'first')
  49. );
  50. // test that null values are ignored, but values on either side are still merged
  51. $this->assertEquals (
  52. array('test_1', 'test_3'), Object::combined_static('ObjectStaticTest_Combined3', 'second')
  53. );
  54. // test the $ceiling param
  55. $this->assertEquals (
  56. array('test_2', 'test_3'), Object::combined_static('ObjectStaticTest_Combined3', 'first', 'ObjectStaticTest_Combined2')
  57. );
  58. }
  59. /**
  60. * Checks that Object::add_static_var() also works for uninherited stats
  61. */
  62. public function testAddStaticVarWorksForUninheritedStatics() {
  63. Object::add_static_var('ObjectStaticTest_First', 'first', array('test_1b'));
  64. Object::add_static_var('ObjectStaticTest_Second', 'first', array('test_2b'));
  65. // Check that it can be applied to parent and subclasses, and queried directly
  66. $this->assertContains('test_1b', Object::uninherited_static('ObjectStaticTest_First', 'first'));
  67. $this->assertContains('test_2b', Object::uninherited_static('ObjectStaticTest_Second', 'first'));
  68. // But it won't affect subclasses - this is *uninherited* static
  69. $this->assertNotContains('test_2b', Object::uninherited_static('ObjectStaticTest_Third', 'first'));
  70. $this->assertNotContains('test_2b', Object::uninherited_static('ObjectStaticTest_Fourth', 'first'));
  71. // Subclasses that don't have the static explicitly defined should allow definition, also
  72. // This also checks that add_static_var can be called after the first uninherited_static()
  73. // call (which can be buggy due to caching)
  74. Object::add_static_var('ObjectStaticTest_Fourth', 'first', array('test_4b'));
  75. $this->assertContains('test_4b', Object::uninherited_static('ObjectStaticTest_Fourth', 'first'));
  76. }
  77. }
  78. /**#@+
  79. * @ignore
  80. */
  81. class ObjectStaticTest_First extends Object {
  82. public static $first = array('test_1');
  83. public static $second = array('test_1');
  84. public static $third = 'test_1';
  85. }
  86. class ObjectStaticTest_Second extends ObjectStaticTest_First {
  87. public static $first = array('test_2');
  88. }
  89. class ObjectStaticTest_Third extends ObjectStaticTest_Second {
  90. public static $first = array('test_3');
  91. public static $second = array('test_3');
  92. public static $fourth = array('test_3');
  93. }
  94. class ObjectStaticTest_Fourth extends ObjectStaticTest_Third {
  95. public static $fourth = array('test_4');
  96. }
  97. class ObjectStaticTest_Combined1 extends Object {
  98. public static $first = array('test_1');
  99. public static $second = array('test_1');
  100. }
  101. class ObjectStaticTest_Combined2 extends ObjectStaticTest_Combined1 {
  102. public static $first = array('test_2');
  103. public static $second = null;
  104. }
  105. class ObjectStaticTest_Combined3 extends ObjectStaticTest_Combined2 {
  106. public static $first = array('test_3');
  107. public static $second = array('test_3');
  108. }