ContextTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Elgg;
  3. class ContextTest extends \PHPUnit_Framework_TestCase {
  4. public function testPeekAndPopReturnNullByDefault() {
  5. $context = new Context();
  6. $this->assertNull($context->peek());
  7. $this->assertNull($context->pop());
  8. // TODO: remove once global state is fully deprecated (2.0)
  9. _elgg_services()->setValue('context', new Context());
  10. $this->assertNull(elgg_get_context());
  11. $this->assertNull(elgg_pop_context());
  12. }
  13. public function testPopReturnsAndRemovesTheMostRecentlyPushedContext() {
  14. $context = new Context();
  15. $context->push('foo');
  16. $context->push('bar');
  17. $this->assertEquals('bar', $context->pop());
  18. $this->assertEquals('foo', $context->pop());
  19. // TODO: remove once global state is fully deprecated (2.0)
  20. _elgg_services()->setValue('context', new Context());
  21. elgg_push_context('foo');
  22. elgg_push_context('bar');
  23. $this->assertEquals('bar', elgg_pop_context());
  24. $this->assertEquals('foo', elgg_pop_context());
  25. }
  26. public function testSetReplacesTheMostRecentlyPushedContext() {
  27. $context = new Context();
  28. $context->push('foo');
  29. $context->set('bar');
  30. $this->assertNotTrue($context->contains('foo'));
  31. $this->assertEquals('bar', $context->pop());
  32. $this->assertNotEquals('foo', $context->pop());
  33. // TODO: remove once global state is fully deprecated (2.0)
  34. _elgg_services()->setValue('context', new Context());
  35. elgg_push_context('foo');
  36. elgg_set_context('bar');
  37. $this->assertNotTrue(elgg_in_context('foo'));
  38. $this->assertEquals('bar', elgg_pop_context());
  39. $this->assertNotEquals('foo', elgg_pop_context());
  40. }
  41. public function testPeekReturnsTheMostRecentlyPushedContext() {
  42. $context = new Context();
  43. $context->push('foo');
  44. $this->assertEquals('foo', $context->peek());
  45. $context->push('bar');
  46. $this->assertEquals('bar', $context->peek());
  47. $context->pop();
  48. $this->assertEquals('foo', $context->peek());
  49. // TODO: remove once global state is fully deprecated (2.0)
  50. _elgg_services()->setValue('context', new Context());
  51. elgg_push_context('foo');
  52. $this->assertEquals('foo', elgg_get_context());
  53. elgg_push_context('bar');
  54. $this->assertEquals('bar', elgg_get_context());
  55. elgg_pop_context();
  56. $this->assertEquals('foo', elgg_get_context());
  57. }
  58. public function testContainsTellsYouIfAGivenContextIsInTheCurrentStack() {
  59. $context = new Context();
  60. $context->push('foo');
  61. $context->push('bar');
  62. $context->push('baz');
  63. $this->assertTrue($context->contains('foo'));
  64. $this->assertTrue($context->contains('bar'));
  65. $this->assertTrue($context->contains('baz'));
  66. $popped = $context->pop();
  67. $this->assertFalse($context->contains($popped));
  68. // TODO: remove once global state is fully deprecated (2.0)
  69. _elgg_services()->setValue('context', new Context());
  70. elgg_push_context('foo');
  71. elgg_push_context('bar');
  72. elgg_push_context('baz');
  73. $this->assertTrue(elgg_in_context('foo'));
  74. $this->assertTrue(elgg_in_context('bar'));
  75. $this->assertTrue(elgg_in_context('baz'));
  76. $popped = elgg_pop_context();
  77. $this->assertFalse(elgg_in_context($popped));
  78. }
  79. public function testFailToSetEmptyContext() {
  80. $context = new Context();
  81. $context->set(" ");
  82. $this->assertNull($context->peek());
  83. $this->assertNull($context->pop());
  84. $context->push(" ");
  85. $this->assertEquals(" ", $context->peek());
  86. $this->assertEquals(" ", $context->pop());
  87. }
  88. public function testCanGetStack() {
  89. $context = new Context();
  90. $context->push("123");
  91. $context->push("hello");
  92. $this->assertEquals(["123", "hello"], $context->toArray());
  93. }
  94. public function testCanSetStack() {
  95. $context = new Context();
  96. $context->fromArray([123, "hello", true]);
  97. $this->assertEquals(["123", "hello", "1"], $context->toArray());
  98. }
  99. public function testStackAlwaysContainsStrings() {
  100. $context = new Context();
  101. $context->set(123);
  102. $context->push(true);
  103. $this->assertEquals(["123", "1"], $context->toArray());
  104. $context->fromArray([123, true]);
  105. $this->assertEquals(["123", "1"], $context->toArray());
  106. }
  107. public function testSetLowersCase() {
  108. $context = new Context();
  109. $context->set("HELLO");
  110. $this->assertEquals("hello", $context->peek());
  111. }
  112. public function testPushDoesNotAlterCase() {
  113. $context = new Context();
  114. $context->push("HELLO");
  115. $this->assertEquals("HELLO", $context->peek());
  116. }
  117. }