-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11060 from zeopix/2.0.x_session_flash_fix
Flash\Session::getMessages with type and removal fix
- Loading branch information
Showing
4 changed files
with
330 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* FlashBase.php | ||
* \Phalcon\Flash\Direct\Helper\FlashBase | ||
* | ||
* Tests the \Phalcon\Tag component | ||
* Tests the \Phalcon\Flash\Direct component | ||
* | ||
* PhalconPHP Framework | ||
* | ||
|
@@ -40,9 +40,9 @@ class FlashBase extends TBase | |
|
||
/** | ||
* Sets the custom classes for the tests | ||
* | ||
* | ||
* @param $classes | ||
* | ||
* | ||
* @author Nikolaos Dimopoulos <[email protected]> | ||
* @since 2014-10-04 | ||
*/ | ||
|
134 changes: 134 additions & 0 deletions
134
tests/unit/Phalcon/Flash/Session/FlashSessionDefaultTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
/** | ||
* InMemorySession.php | ||
* \Phalcon\Flash\Session\FlashSessionDefaultTest | ||
* | ||
* Tests Phalcon\Flash\Session | ||
* | ||
* PhalconPHP Framework | ||
* | ||
* @author Iván Guillén <[email protected]> | ||
* | ||
* The contents of this file are subject to the New BSD License that is | ||
* bundled with this package in the file docs/LICENSE.txt | ||
* | ||
* If you did not receive a copy of the license and are unable to obtain it | ||
* through the world-wide-web, please send an email to [email protected] | ||
* so that we can send you a copy immediately. | ||
*/ | ||
|
||
namespace Phalcon\Tests\unit\Phalcon\Flash\Session; | ||
|
||
|
||
use \PhalconTest\Flash\Session as PhFlash; | ||
|
||
use \Phalcon\Tests\unit\Phalcon\_Helper\TestsBase as TBase; | ||
|
||
use \Phalcon\DI as PhDI; | ||
|
||
|
||
class FlashSessionDefaultTest extends TBase | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $classes = [ | ||
'success' => 'successMessage', | ||
'notice' => 'noticeMessage', | ||
'warning' => 'warningMessage', | ||
'error' => 'errorMessage', | ||
]; | ||
|
||
/** | ||
* Return flash instance | ||
*/ | ||
protected function getFlash() | ||
{ | ||
$flash = new PhFlash($this->classes); | ||
$di = new PhDI(); | ||
$di->set('session', new Helper\InMemorySession(), true); | ||
$flash->setDI($di); | ||
return $flash; | ||
} | ||
|
||
/** | ||
* Test getMessages with specified type and removal | ||
* activated, only removes the retreived messages. | ||
*/ | ||
public function testGetMessagesTypeRemoveMessages() | ||
{ | ||
$flash = $this->getFlash(); | ||
|
||
$flash->success('sample success'); | ||
$flash->error('sample error'); | ||
|
||
$expectedSuccessMessages = ['sample success']; | ||
$actualSuccessMessages = $flash->getMessages('success'); | ||
expect($actualSuccessMessages)->equals($expectedSuccessMessages); | ||
|
||
$expectedErrorMessages = ['sample error']; | ||
$actualErrorMessages = $flash->getMessages('error'); | ||
expect($actualErrorMessages)->equals($expectedErrorMessages); | ||
|
||
expect(null)->equals($flash->getMessages()); | ||
} | ||
|
||
/** | ||
* Tests clear method | ||
*/ | ||
public function testClear() | ||
{ | ||
$flash = $this->getFlash(); | ||
|
||
ob_start(); | ||
$flash->output(); | ||
$flash->success('sample message'); | ||
$flash->clear(); | ||
$actual = ob_get_contents(); | ||
ob_end_clean(); | ||
$expected = ''; | ||
|
||
expect($actual)->equals($expected); | ||
} | ||
|
||
/** | ||
* @dataProvider testStringDataProvider | ||
*/ | ||
public function testString($function) | ||
{ | ||
$flash = $this->getFlash(); | ||
$template = ' class="%s"'; | ||
$class = sprintf($template, $this->classes[$function]); | ||
|
||
$template = '<div%s>%s</div>' . PHP_EOL; | ||
$message = 'sample message'; | ||
|
||
$expected = sprintf($template, $class, $message); | ||
ob_start(); | ||
$flash->$function($message); | ||
$flash->output(); | ||
$actual = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
expect($actual)->equals($expected); | ||
} | ||
|
||
|
||
/** | ||
* testString dataProvider | ||
*/ | ||
public function testStringDataProvider() | ||
{ | ||
return array( | ||
'error' => array( | ||
'function' => 'error' | ||
), | ||
'success' => array( | ||
'function' => 'success' | ||
), | ||
'notice' => array( | ||
'function' => 'notice' | ||
) | ||
); | ||
} | ||
} |
179 changes: 179 additions & 0 deletions
179
tests/unit/Phalcon/Flash/Session/Helper/InMemorySession.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
/** | ||
* InMemorySession.php | ||
* \Phalcon\Flash\Session\Helper\InMemorySession | ||
* | ||
* in memory implementation for Phalcon\Session\Adapter | ||
* | ||
* PhalconPHP Framework | ||
* | ||
* @author Iván Guillén <[email protected]> | ||
* | ||
* The contents of this file are subject to the New BSD License that is | ||
* bundled with this package in the file docs/LICENSE.txt | ||
* | ||
* If you did not receive a copy of the license and are unable to obtain it | ||
* through the world-wide-web, please send an email to [email protected] | ||
* so that we can send you a copy immediately. | ||
*/ | ||
|
||
namespace Phalcon\Tests\unit\Phalcon\Flash\Session\Helper; | ||
|
||
use Phalcon\Session\AdapterInterface; | ||
|
||
class InMemorySession implements AdapterInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $memory; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $options; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $sessionId; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $started; | ||
|
||
public function __construct() | ||
{ | ||
$this->sessionId = $this->generateId(); | ||
$this->started = false; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function start() | ||
{ | ||
$this->memory = []; | ||
$this->started = true; | ||
} | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setOptions(array $options) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getOptions() | ||
{ | ||
return $this->options; | ||
} | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function get($index, $defaultValue = null) | ||
{ | ||
return $this->memory[$index] ?: $defaultValue; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function set($index, $value) | ||
{ | ||
$this->memory[$index] = $value; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function has($index) | ||
{ | ||
return isset($this->memory[$index]); | ||
} | ||
/** | ||
* Removes a session variable from an application context | ||
* | ||
* @param string $index | ||
*/ | ||
public function remove($index) | ||
{ | ||
if (isset($this->memory[$index])) { | ||
unset($this->memory[$index]); | ||
} | ||
} | ||
/** | ||
* Returns active session id | ||
* | ||
* @return string | ||
*/ | ||
public function getId() | ||
{ | ||
return $this->sessionId; | ||
} | ||
/** | ||
* Check whether the session has been started | ||
* | ||
* @return bool | ||
*/ | ||
public function isStarted() | ||
{ | ||
return $this->started; | ||
} | ||
|
||
/** | ||
* Destroys the active session | ||
* | ||
* @inheritdoc | ||
*/ | ||
public function destroy($removeData = NULL) | ||
{ | ||
if ($removeData) { | ||
$this->memory = []; | ||
} | ||
$this->started = false; | ||
} | ||
|
||
/** | ||
* | ||
* @inheritdoc | ||
*/ | ||
public function regenerateId($deleteOldSession = true) | ||
{ | ||
$this->sessionId = $this->generateId(); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private function generateId() | ||
{ | ||
return md5(time()); | ||
} | ||
} |