You are here

class ResponseTest in Payment 8.2

@coversDefaultClass \Drupal\payment\Response\Response

@group Payment

Hierarchy

Expanded class hierarchy of ResponseTest

File

tests/src/Unit/Response/ResponseTest.php, line 19

Namespace

Drupal\Tests\payment\Unit\Response
View source
class ResponseTest extends UnitTestCase {

  /**
   * The redirect URL.
   *
   * @var \Drupal\Core\Url
   */
  protected $redirectUrl;

  /**
   * The response.
   *
   * @var \Symfony\Component\HttpFoundation\Response|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $symfonyResponse;

  /**
   * The route name to test with.
   *
   * @var string
   */
  protected $routeName;

  /**
   * The class under test.
   *
   * @var \Drupal\payment\Response\Response
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->routeName = $this
      ->randomMachineName();
    $this->redirectUrl = new Url($this->routeName);
    $this->symfonyResponse = $this
      ->getMockBuilder(SymfonyResponse::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->sut = new Response($this->redirectUrl, $this->symfonyResponse);
  }

  /**
   * @covers ::getRedirectUrl
   */
  function testGetRedirectUrl() {
    $this
      ->assertSame($this->redirectUrl, $this->sut
      ->getRedirectUrl());
  }

  /**
   * @covers ::getResponse
   */
  function testGetResponse() {
    $this
      ->assertSame($this->symfonyResponse, $this->sut
      ->getResponse());
  }

  /**
   * @covers ::getResponse
   */
  function testGetResponseWithoutResponse() {
    $generated_url = new GeneratedUrl();
    $generated_url
      ->setGeneratedUrl($this
      ->randomMachineName());
    $generated_url
      ->addCacheTags([
      'node:1',
    ]);
    $url_generator = $this
      ->createMock(UrlGeneratorInterface::class);
    $url_generator
      ->expects($this
      ->atLeastOnce())
      ->method('generateFromRoute')
      ->with($this->routeName)
      ->willReturn($generated_url);
    $container = new ContainerBuilder();
    $container
      ->set('url_generator', $url_generator);
    \Drupal::setContainer($container);
    $this->sut = new Response($this->redirectUrl);
    $response = $this->sut
      ->getResponse();
    $this
      ->assertInstanceOf(TrustedRedirectResponse::class, $response);
    $this
      ->assertEquals([
      'node:1',
    ], $response
      ->getCacheableMetadata()
      ->getCacheTags());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ResponseTest::$redirectUrl protected property The redirect URL.
ResponseTest::$routeName protected property The route name to test with.
ResponseTest::$sut protected property The class under test.
ResponseTest::$symfonyResponse protected property The response.
ResponseTest::setUp public function Overrides UnitTestCase::setUp
ResponseTest::testGetRedirectUrl function @covers ::getRedirectUrl
ResponseTest::testGetResponse function @covers ::getResponse
ResponseTest::testGetResponseWithoutResponse function @covers ::getResponse
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.