class OpenIdConnectSessionTest in OpenID Connect / OAuth client 8
Same name and namespace in other branches
- 2.x tests/src/Unit/OpenIdConnectSessionTest.php \Drupal\Tests\openid_connect\Unit\OpenIdConnectSessionTest
@coversDefaultClass \Drupal\openid_connect\OpenIDConnectSession @group openid_connect
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\openid_connect\Unit\OpenIdConnectSessionTest
Expanded class hierarchy of OpenIdConnectSessionTest
File
- tests/
src/ Unit/ OpenIdConnectSessionTest.php, line 17
Namespace
Drupal\Tests\openid_connect\UnitView source
class OpenIdConnectSessionTest extends UnitTestCase {
/**
* Create a test path for testing.
*/
const TEST_PATH = '/test/path/1';
/**
* The user login path for testing.
*/
const TEST_USER_PATH = '/user/login';
/**
* A query string to test with.
*/
const TEST_QUERY = 'sport=baseball&team=reds';
/**
* A mock of the current_path service.
*
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $currentPath;
/**
* A mock of the requestStack method for testing.
*
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $requestStack;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Mock the currentPath service.
$this->currentPath = $this
->createMock(CurrentPathStack::class);
// Mock the Request class that is returned by RequestStack class.
$request = $this
->createMock(Request::class);
$request
->expects($this
->once())
->method('getQueryString')
->willReturn('sport=baseball&team=reds');
// Mock the requestStack service.
$this->requestStack = $this
->createMock(RequestStack::class);
$this->requestStack
->expects($this
->once())
->method('getCurrentRequest')
->willReturn($request);
}
/**
* Test the save destination method.
*/
public function testSaveDestination() : void {
// Get the expected session array.
$expectedSession = $this
->getExpectedSessionArray(self::TEST_PATH, self::TEST_QUERY);
// Mock the getPath method for the current path service.
$this->currentPath
->expects($this
->once())
->method('getPath')
->willReturn(self::TEST_PATH);
// Create a new OpenIDConnectSession class.
$session = new OpenIDConnectSession($this->currentPath, $this->requestStack);
// Call the saveDestination() method.
$session
->saveDestination();
// Assert the $_SESSOIN global matches our expectation.
$this
->assertArrayEquals($expectedSession, $_SESSION);
}
/**
* Test the saveDestination() method with the /user/login path.
*/
public function testSaveDestinationUserPath() : void {
// Setup our expected results.
$expectedSession = $this
->getExpectedSessionArray('/user', self::TEST_QUERY);
// Mock the getPath method with the user path.
$this->currentPath
->expects($this
->once())
->method('getPath')
->willReturn(self::TEST_USER_PATH);
// Create a class to test with.
$session = new OpenIDConnectSession($this->currentPath, $this->requestStack);
// Call the saveDestination method.
$session
->saveDestination();
// Assert the $_SESSION matches our expectations.
$this
->assertArrayEquals($expectedSession, $_SESSION);
}
/**
* Get the expected session array to compare.
*
* @param string $path
* The path that is expected in the session global.
* @param string $queryString
* The query string that is expected in the session global.
*
* @return array
* The expected session array.
*/
private function getExpectedSessionArray(string $path, string $queryString) : array {
return [
'openid_connect_destination' => [
$path,
[
'query' => $queryString,
],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OpenIdConnectSessionTest:: |
protected | property | A mock of the current_path service. | |
OpenIdConnectSessionTest:: |
protected | property | A mock of the requestStack method for testing. | |
OpenIdConnectSessionTest:: |
private | function | Get the expected session array to compare. | |
OpenIdConnectSessionTest:: |
protected | function |
Overrides UnitTestCase:: |
|
OpenIdConnectSessionTest:: |
public | function | Test the save destination method. | |
OpenIdConnectSessionTest:: |
public | function | Test the saveDestination() method with the /user/login path. | |
OpenIdConnectSessionTest:: |
constant | Create a test path for testing. | ||
OpenIdConnectSessionTest:: |
constant | A query string to test with. | ||
OpenIdConnectSessionTest:: |
constant | The user login path for testing. | ||
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |