You are here

class OpenIdConnectSessionTest in OpenID Connect / OAuth client 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/OpenIdConnectSessionTest.php \Drupal\Tests\openid_connect\Unit\OpenIdConnectSessionTest

@coversDefaultClass \Drupal\openid_connect\OpenIDConnectSession @group openid_connect

Hierarchy

Expanded class hierarchy of OpenIdConnectSessionTest

File

tests/src/Unit/OpenIdConnectSessionTest.php, line 17

Namespace

Drupal\Tests\openid_connect\Unit
View 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

Namesort descending Modifiers Type Description Overrides
OpenIdConnectSessionTest::$currentPath protected property A mock of the current_path service.
OpenIdConnectSessionTest::$requestStack protected property A mock of the requestStack method for testing.
OpenIdConnectSessionTest::getExpectedSessionArray private function Get the expected session array to compare.
OpenIdConnectSessionTest::setUp protected function Overrides UnitTestCase::setUp
OpenIdConnectSessionTest::testSaveDestination public function Test the save destination method.
OpenIdConnectSessionTest::testSaveDestinationUserPath public function Test the saveDestination() method with the /user/login path.
OpenIdConnectSessionTest::TEST_PATH constant Create a test path for testing.
OpenIdConnectSessionTest::TEST_QUERY constant A query string to test with.
OpenIdConnectSessionTest::TEST_USER_PATH constant The user login path for testing.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.