You are here

class CookieMonsterTest in Bakery Single Sign-On System 8.2

@coversDefaultClass \Drupal\bakery\EventSubscriber\CookieMonster

Hierarchy

Expanded class hierarchy of CookieMonsterTest

File

tests/src/Unit/EventSubscriber/CookieMonsterTest.php, line 24

Namespace

Drupal\Tests\bakery\Unit\EventSubscriber
View source
class CookieMonsterTest extends UnitTestCase {

  /**
   * @var \Drupal\bakery\EventSubscriber\CookieMonster
   */
  private $sot;

  /**
   * @var \Symfony\Component\HttpFoundation\ParameterBag
   */
  private $cookieJar;

  /**
   * @var \Drupal\Core\Http\RequestStack
   */
  private $request;

  /**
   * @var \Drupal\Component\Datetime\Time
   */
  private $time;
  public function setUp() {
    parent::setUp();
    $this->request = new RequestStack();
    $this->request
      ->push(new Request());
    $this->time = new Time($this->request);
    $config = new Config('config.test', new MemoryStorage(), new EventDispatcher(), $this
      ->prophesize(TypedConfigManagerInterface::class)
      ->reveal());
    $config
      ->set('bakery_domain', '.example.com');
    $config
      ->set('bakery_freshness', 3600);
    $cf = $this
      ->prophesize(ConfigFactoryInterface::class);
    $cf
      ->get('bakery.settings')
      ->willReturn($config);
    $this->cookieJar = new ParameterBag();
    $this->sot = new CookieMonster($this->time, $cf
      ->reveal(), $this->cookieJar);
  }

  /**
   * @covers ::meWantCookie
   */
  public function testFoo() {
    $response = new Response();
    $event = new ResponseEvent($this
      ->prophesize(HttpKernelInterface::class)
      ->reveal(), $this->request
      ->getCurrentRequest(), HttpKernelInterface::MASTER_REQUEST, $response);
    $this->sot
      ->meWantCookie($event);
    $this
      ->assertCount(0, $response->headers
      ->getCookies());
    $this->cookieJar
      ->set('cookie1', 'foo');
    $this->cookieJar
      ->set('cookie2', 'bar');
    $this->cookieJar
      ->set('cookie3', '');
    $this->sot
      ->meWantCookie($event);
    $this
      ->assertCount(4, $response->headers
      ->getCookies());
    $expire = $this->time
      ->getRequestTime() + 3600;
    $secure = !empty(ini_get('session.cookie_secure'));
    $this
      ->assertEquals([
      Cookie::create('cookie1', 'foo', $expire, '/', '.example.com', $secure),
      Cookie::create('cookie2', 'bar', $expire, '/', '.example.com', $secure),
      // This might cause problems with Symfony 5?
      new Cookie('cookie3', NULL, 1, '/', '.example.com', $secure, TRUE, FALSE, NULL),
      new Cookie('cookie3', NULL, 1, '/', '', $secure, TRUE, FALSE, NULL),
    ], $response->headers
      ->getCookies());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CookieMonsterTest::$cookieJar private property
CookieMonsterTest::$request private property
CookieMonsterTest::$sot private property
CookieMonsterTest::$time private property
CookieMonsterTest::setUp public function Overrides UnitTestCase::setUp
CookieMonsterTest::testFoo public function @covers ::meWantCookie
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.