You are here

class SubrequestsManagerTest in Subrequests 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Unit/SubrequestsManagerTest.php \Drupal\Tests\subrequests\Unit\SubrequestsManagerTest

@coversDefaultClass \Drupal\subrequests\SubrequestsManager @group subrequests

Hierarchy

Expanded class hierarchy of SubrequestsManagerTest

File

tests/src/Unit/SubrequestsManagerTest.php, line 22

Namespace

Drupal\Tests\subrequests\Unit
View source
class SubrequestsManagerTest extends UnitTestCase {

  /**
   * @var \Drupal\subrequests\SubrequestsManager
   */
  protected $sut;
  protected function setUp() {
    parent::setUp();
    $http_kernel = $this
      ->prophesize(HttpKernelInterface::class);
    $http_kernel
      ->handle(Argument::type(Request::class), HttpKernelInterface::MASTER_REQUEST)
      ->will(function ($args) {
      return Response::create($args[0]
        ->getPathInfo());
    });
    $denormalizer = $this
      ->prophesize(JsonSubrequestDenormalizer::class);
    $denormalizer
      ->denormalize(Argument::type(Subrequest::class), Request::class, NULL, Argument::type('array'))
      ->will(function ($args) {
      return Request::create($args[0]->uri);
    });
    $denormalizer
      ->supportsDenormalization(Argument::type(Subrequest::class), Request::class, NULL)
      ->willReturn([])
      ->willReturn(TRUE);
    $denormalizer
      ->supportsDenormalization(Argument::type(Subrequest::class), Request::class, NULL, Argument::type('array'))
      ->willReturn([])
      ->willReturn(TRUE);
    $serializer = new Serializer([
      $denormalizer
        ->reveal(),
    ], [
      new JsonDecode(),
    ]);
    $this->sut = new SubrequestsManager($http_kernel
      ->reveal(), $serializer, new JsonPathReplacer());
  }

  /**
   * @covers ::request
   */
  public function testRequest() {

    // Create and populate a tree.
    $tree = new SubrequestsTree();
    $subrequests[] = new Subrequest([
      'uri' => 'lorem',
      'action' => 'view',
      'requestId' => 'foo',
      'headers' => [],
      'waitFor' => [
        '<ROOT>',
      ],
      '_resolved' => FALSE,
      'body' => 'bar',
    ]);
    $subrequests[] = new Subrequest([
      'uri' => 'ipsum',
      'action' => 'sing',
      'requestId' => 'oop',
      'headers' => [],
      '_resolved' => FALSE,
      'body' => [],
      'waitFor' => [
        'foo',
      ],
    ]);
    $subrequests[] = new Subrequest([
      'uri' => 'dolor',
      'action' => 'create',
      'requestId' => 'oof',
      'headers' => [],
      '_resolved' => FALSE,
      'body' => 'bar',
      'waitFor' => [
        'foo',
      ],
    ]);
    $tree
      ->stack([
      $subrequests[0],
    ]);
    $tree
      ->stack([
      $subrequests[1],
      $subrequests[2],
    ]);
    $tree
      ->setMasterRequest(new Request());
    $actual = $this->sut
      ->request($tree);
    $this
      ->assertSame('<foo>', $actual[0]->headers
      ->get('Content-ID'));
    $this
      ->assertSame('<oop>', $actual[1]->headers
      ->get('Content-ID'));
    $this
      ->assertSame('<oof>', $actual[2]->headers
      ->get('Content-ID'));
    $this
      ->assertSame('/lorem', $actual[0]
      ->getContent());
    $this
      ->assertSame('/ipsum', $actual[1]
      ->getContent());
    $this
      ->assertSame('/dolor', $actual[2]
      ->getContent());
  }

}

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.
SubrequestsManagerTest::$sut protected property
SubrequestsManagerTest::setUp protected function Overrides UnitTestCase::setUp
SubrequestsManagerTest::testRequest public function @covers ::request
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.