123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace Elgg;
- class ContextTest extends \PHPUnit_Framework_TestCase {
- public function testPeekAndPopReturnNullByDefault() {
- $context = new Context();
-
- $this->assertNull($context->peek());
- $this->assertNull($context->pop());
-
- // TODO: remove once global state is fully deprecated (2.0)
- _elgg_services()->setValue('context', new Context());
-
- $this->assertNull(elgg_get_context());
- $this->assertNull(elgg_pop_context());
- }
-
- public function testPopReturnsAndRemovesTheMostRecentlyPushedContext() {
- $context = new Context();
- $context->push('foo');
- $context->push('bar');
-
- $this->assertEquals('bar', $context->pop());
- $this->assertEquals('foo', $context->pop());
-
- // TODO: remove once global state is fully deprecated (2.0)
- _elgg_services()->setValue('context', new Context());
- elgg_push_context('foo');
- elgg_push_context('bar');
-
- $this->assertEquals('bar', elgg_pop_context());
- $this->assertEquals('foo', elgg_pop_context());
- }
-
- public function testSetReplacesTheMostRecentlyPushedContext() {
- $context = new Context();
-
- $context->push('foo');
- $context->set('bar');
-
- $this->assertNotTrue($context->contains('foo'));
- $this->assertEquals('bar', $context->pop());
- $this->assertNotEquals('foo', $context->pop());
-
- // TODO: remove once global state is fully deprecated (2.0)
- _elgg_services()->setValue('context', new Context());
- elgg_push_context('foo');
- elgg_set_context('bar');
-
- $this->assertNotTrue(elgg_in_context('foo'));
- $this->assertEquals('bar', elgg_pop_context());
- $this->assertNotEquals('foo', elgg_pop_context());
- }
-
- public function testPeekReturnsTheMostRecentlyPushedContext() {
- $context = new Context();
-
- $context->push('foo');
- $this->assertEquals('foo', $context->peek());
- $context->push('bar');
- $this->assertEquals('bar', $context->peek());
- $context->pop();
- $this->assertEquals('foo', $context->peek());
- // TODO: remove once global state is fully deprecated (2.0)
- _elgg_services()->setValue('context', new Context());
-
- elgg_push_context('foo');
- $this->assertEquals('foo', elgg_get_context());
- elgg_push_context('bar');
- $this->assertEquals('bar', elgg_get_context());
- elgg_pop_context();
- $this->assertEquals('foo', elgg_get_context());
- }
-
- public function testContainsTellsYouIfAGivenContextIsInTheCurrentStack() {
- $context = new Context();
-
- $context->push('foo');
- $context->push('bar');
- $context->push('baz');
-
- $this->assertTrue($context->contains('foo'));
- $this->assertTrue($context->contains('bar'));
- $this->assertTrue($context->contains('baz'));
-
- $popped = $context->pop();
-
- $this->assertFalse($context->contains($popped));
- // TODO: remove once global state is fully deprecated (2.0)
- _elgg_services()->setValue('context', new Context());
-
- elgg_push_context('foo');
- elgg_push_context('bar');
- elgg_push_context('baz');
-
- $this->assertTrue(elgg_in_context('foo'));
- $this->assertTrue(elgg_in_context('bar'));
- $this->assertTrue(elgg_in_context('baz'));
-
- $popped = elgg_pop_context();
-
- $this->assertFalse(elgg_in_context($popped));
- }
- public function testFailToSetEmptyContext() {
- $context = new Context();
- $context->set(" ");
- $this->assertNull($context->peek());
- $this->assertNull($context->pop());
- $context->push(" ");
- $this->assertEquals(" ", $context->peek());
- $this->assertEquals(" ", $context->pop());
- }
- public function testCanGetStack() {
- $context = new Context();
- $context->push("123");
- $context->push("hello");
- $this->assertEquals(["123", "hello"], $context->toArray());
- }
- public function testCanSetStack() {
- $context = new Context();
- $context->fromArray([123, "hello", true]);
- $this->assertEquals(["123", "hello", "1"], $context->toArray());
- }
- public function testStackAlwaysContainsStrings() {
- $context = new Context();
- $context->set(123);
- $context->push(true);
- $this->assertEquals(["123", "1"], $context->toArray());
- $context->fromArray([123, true]);
- $this->assertEquals(["123", "1"], $context->toArray());
- }
- public function testSetLowersCase() {
- $context = new Context();
- $context->set("HELLO");
- $this->assertEquals("hello", $context->peek());
- }
- public function testPushDoesNotAlterCase() {
- $context = new Context();
- $context->push("HELLO");
- $this->assertEquals("HELLO", $context->peek());
- }
- }
|