Namespace
Drupal\Tests\stream_wrapper_example\Traits
File
-
modules/stream_wrapper_example/tests/src/Traits/MockSessionTrait.php
View source
<?php
namespace Drupal\Tests\stream_wrapper_example\Traits;
use Drupal\stream_wrapper_example\SessionHelper;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
trait MockSessionTrait {
use ProphecyTrait;
protected $sessionStore;
protected $requestStack;
protected function createSessionMock() {
$this->sessionStore = [];
$session = $this->prophesize(SessionInterface::class);
$test = $this;
$session->get('stream_wrapper_example', [])
->will(function ($args) use ($test) {
return $test->getSessionStore();
});
$session->set('stream_wrapper_example', Argument::any())
->will(function ($args) use ($test) {
$test->setSessionStore($args[1]);
});
$session->remove('stream_wrapper_example')
->will(function ($args) use ($test) {
$test->resetSessionStore();
});
$request = $this->prophesize(Request::class);
$request->getSession()
->willReturn($session->reveal());
$request_stack = $this->prophesize(RequestStack::class);
$request_stack->getCurrentRequest()
->willReturn($request->reveal());
return $this->requestStack = $request_stack->reveal();
}
public function getSessionHelper() {
return new SessionHelper($this->requestStack);
}
public function getSessionStore() {
return $this->sessionStore;
}
public function setSessionStore($data) {
$this->sessionStore = $data;
}
public function resetSessionStore() {
$this->sessionStore = [];
}
}
Traits
| Title |
Deprecated |
Summary |
| MockSessionTrait |
|
A trait to expose a mock session type to PHPUnit tests. |