trait MockSessionTrait in Examples for Developers 3.x
Same name and namespace in other branches
- 8 stream_wrapper_example/tests/src/Traits/MockSessionTrait.php \Drupal\Tests\stream_wrapper_example\Traits\MockSessionTrait
A trait to expose a mock session type to PHPUnit tests.
Hierarchy
- trait \Drupal\Tests\stream_wrapper_example\Traits\MockSessionTrait
2 files declare their use of MockSessionTrait
- SessionHelperTest.php in modules/
stream_wrapper_example/ tests/ src/ Unit/ SessionHelperTest.php - StreamWrapperTest.php in modules/
stream_wrapper_example/ tests/ src/ Kernel/ StreamWrapperTest.php
File
- modules/
stream_wrapper_example/ tests/ src/ Traits/ MockSessionTrait.php, line 14
Namespace
Drupal\Tests\stream_wrapper_example\TraitsView source
trait MockSessionTrait {
/**
* We'll use this array to back our mock session.
*
* @var array
*/
protected $sessionStore;
/**
* A representation of the HTTP request.
*
* @var \Symfony\Component\HttpFoundation\RequestStack|\Prophecy\Prophecy\ProphecyInterface
*/
protected $requestStack;
/**
* Create a mock session object.
*
* @return \Symfony\Component\HttpFoundation\RequestStack|\Prophecy\Prophecy\ProphecyInterface
* A test double, or mock, of a RequestStack object
* that can be used to return a mock Session object.
*/
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();
}
/**
* Get a session helper.
*/
public function getSessionHelper() {
return new SessionHelper($this->requestStack);
}
/**
* Helper for mocks.
*/
public function getSessionStore() {
return $this->sessionStore;
}
/**
* Helper for our mocks.
*/
public function setSessionStore($data) {
$this->sessionStore = $data;
}
/**
* Helper for our mocks.
*/
public function resetSessionStore() {
$this->sessionStore = [];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MockSessionTrait:: |
protected | property | A representation of the HTTP request. | |
MockSessionTrait:: |
protected | property | We'll use this array to back our mock session. | |
MockSessionTrait:: |
protected | function | Create a mock session object. | |
MockSessionTrait:: |
public | function | Get a session helper. | |
MockSessionTrait:: |
public | function | Helper for mocks. | |
MockSessionTrait:: |
public | function | Helper for our mocks. | |
MockSessionTrait:: |
public | function | Helper for our mocks. |