summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnold Daniels <arnold@jasny.net>2016-11-20 01:12:36 +0100
committerArnold Daniels <arnold@jasny.net>2016-11-20 01:12:36 +0100
commit0a704b2edea2115c39294fab9bf29d822332efe3 (patch)
tree4de88bd4ccd4b464e0ef0e3ecd071f78bc5f300c
parent52b3eeb0f8be535d76aa76abe182c01279a45efd (diff)
downloadcontroller-0a704b2edea2115c39294fab9bf29d822332efe3.zip
controller-0a704b2edea2115c39294fab9bf29d822332efe3.tar.gz
controller-0a704b2edea2115c39294fab9bf29d822332efe3.tar.bz2
Session trait tests and fixes
-rw-r--r--src/Controller/Session.php2
-rw-r--r--src/Controller/Session/Flash.php2
-rw-r--r--tests/Controller/SessionTest.php94
-rw-r--r--tests/support/TestHelper.php18
4 files changed, 77 insertions, 39 deletions
diff --git a/src/Controller/Session.php b/src/Controller/Session.php
index 3318932..cba83b7 100644
--- a/src/Controller/Session.php
+++ b/src/Controller/Session.php
@@ -33,7 +33,7 @@ trait Session
/**
* Link the session to the session property in the controller
*/
- protected function useSession()
+ public function useSession()
{
$this->session = $this->getRequest()->getAttribute('session');
diff --git a/src/Controller/Session/Flash.php b/src/Controller/Session/Flash.php
index be72e7c..5aa677d 100644
--- a/src/Controller/Session/Flash.php
+++ b/src/Controller/Session/Flash.php
@@ -71,7 +71,7 @@ class Flash
unset($this->session[$this->key]);
}
- return (object)$this->data;
+ return $this->data ? (object)$this->data : null;
}
/**
diff --git a/tests/Controller/SessionTest.php b/tests/Controller/SessionTest.php
index 869681b..a5fb5a6 100644
--- a/tests/Controller/SessionTest.php
+++ b/tests/Controller/SessionTest.php
@@ -2,22 +2,19 @@
namespace Jasny\Controller;
+use Jasny\Controller\Session;
use Jasny\Controller\Session\Flash;
-use Jasny\Controller\SessionController;
+use Psr\Http\Message\ServerRequestInterface;
use Jasny\Controller\TestHelper;
/**
- * @covers Jasny\Controller
+ * @covers Jasny\Controller\Session
+ * @internal There is some tight coupling between the Session trait and Flash class.
*/
class SessionTest extends \PHPUnit_Framework_TestCase
{
use TestHelper;
- public function setUp()
- {
- $this->markTestIncomplete();
- }
-
/**
* Get the controller class
*
@@ -25,43 +22,66 @@ class SessionTest extends \PHPUnit_Framework_TestCase
*/
protected function getControllerClass()
{
- return SessionController::class;
+ return Session::class;
}
- /**
- * Test setting flash
- *
- * @return array
- */
- public function flashProvider()
+
+ public function testUseSession()
{
- return [
- [(object)['type' => 'test_type', 'message' => 'Test message']]
- ];
+ $session = new \ArrayObject();
+
+ $request = $this->createMock(ServerRequestInterface::class);
+ $request->expects($this->once())->method('getAttribute')->with('session')->willReturn($session);
+
+ $controller = $this->getController();
+ $controller->expects($this->once())->method('getRequest')->willReturn($request);
+
+ $controller->useSession();
+
+ $this->assertAttributeSame($session, 'session', $controller);
}
- /**
- * Test setting flash
- *
- * @dataProvider flashProvider
- * @param object $data
- */
- public function testFlash($data)
+ public function testUseGlobalSession()
{
+ $request = $this->createMock(ServerRequestInterface::class);
+ $request->expects($this->once())->method('getAttribute')->with('session')->willReturn(null);
+
$controller = $this->getController();
-
- $flash = $controller->flash();
- $this->assertInstanceOf(Flash::class, $flash, "Flash is not set");
- $this->assertEmpty($flash->get(), "Flash data should be empty");
-
- $flash = $controller->flash($data->type, $data->message);
- $this->assertInstanceOf(Flash::class, $flash, "Flash is not set");
- $this->assertEquals($data, $flash->get(), "Flash data is incorrect");
-
+ $controller->expects($this->once())->method('getRequest')->willReturn($request);
+
+ $controller->useSession();
+
+ $_SESSION['foo'] = 'bar'; // Change the global session to make sure it's set by reference
+
+ $this->assertAttributeEquals(['foo' => 'bar'], 'session', $controller);
+ }
+
+ public function testGetFlash()
+ {
+ $session = new \ArrayObject();
+
+ $controller = $this->getController();
+ $this->setPrivateProperty($controller, 'session', $session);
+
$flash = $controller->flash();
- $this->assertInstanceOf(Flash::class, $flash, "Flash is not set");
- $this->assertEquals($data, $flash->get(), "Flash data is incorrect");
-
- $flash->clear();
+ $this->assertInstanceOf(Flash::class, $flash);
+ $this->assertEmpty($flash->get());
+
+ $this->assertAttributeSame($session, 'session', $flash);
+
+ $this->assertSame($flash, $controller->flash());
+ }
+
+ public function testSetFlash()
+ {
+ $flash = $this->createMock(Flash::class);
+ $flash->expects($this->once())->method('set')->with('notice', 'hello world');
+
+ $controller = $this->getController();
+ $this->setPrivateProperty($controller, 'flash', $flash);
+
+ $result = $controller->flash('notice', 'hello world');
+
+ $this->assertSame($flash, $result);
}
}
diff --git a/tests/support/TestHelper.php b/tests/support/TestHelper.php
index 8f7dff4..3a87863 100644
--- a/tests/support/TestHelper.php
+++ b/tests/support/TestHelper.php
@@ -50,4 +50,22 @@ trait TestHelper
$getMock = trait_exists($class) ? 'getMockForTrait' : 'getMockForAbstractClass';
return $builder->$getMock();
}
+
+ /**
+ * Set a private or protected property of the given object
+ *
+ * @param object $object
+ * @param string $property
+ * @param mixed $value
+ */
+ protected function setPrivateProperty($object, $property, $value)
+ {
+ if (!is_object($object)) {
+ throw new \InvalidArgumentException("Excpected an object, got a " . gettype($object));
+ }
+
+ $refl = new \ReflectionProperty($object, $property);
+ $refl->setAccessible(true);
+ $refl->setValue($object, $value);
+ }
}