You are here

class MultiresponseNormalizerTest in Subrequests 8.2

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

@coversDefaultClass \Drupal\subrequests\Normalizer\MultiresponseNormalizer @group subrequests

Hierarchy

Expanded class hierarchy of MultiresponseNormalizerTest

File

tests/src/Unit/Normalizer/MultiresponseNormalizerTest.php, line 13

Namespace

Drupal\Tests\subrequests\Normalizer
View source
class MultiresponseNormalizerTest extends UnitTestCase {

  /**
   * @var \Drupal\subrequests\Normalizer\MultiresponseNormalizer
   */
  protected $sut;
  protected function setUp() {
    parent::setUp();
    $this->sut = new MultiresponseNormalizer();
  }

  /**
   * @dataProvider dataProviderSupportsNormalization
   * @covers ::supportsNormalization
   */
  public function testSupportsNormalization($data, $format, $is_supported) {
    $actual = $this->sut
      ->supportsNormalization($data, $format);
    $this
      ->assertSame($is_supported, $actual);
  }
  public function dataProviderSupportsNormalization() {
    return [
      [
        [
          Response::create(''),
        ],
        'multipart-related',
        TRUE,
      ],
      [
        [],
        'multipart-related',
        TRUE,
      ],
      [
        [
          Response::create(''),
        ],
        'fail',
        FALSE,
      ],
      [
        NULL,
        'multipart-related',
        FALSE,
      ],
      [
        [
          Response::create(''),
          NULL,
        ],
        'multipart-related',
        FALSE,
      ],
    ];
  }

  /**
   * @covers ::normalize
   */
  public function testNormalize() {
    $sub_content_type = $this
      ->getRandomGenerator()
      ->string();
    $data = [
      Response::create('Foo!'),
      Response::create('Bar'),
    ];
    $actual = $this->sut
      ->normalize($data, NULL, [
      'sub-content-type' => $sub_content_type,
    ]);
    $parts = explode(', ', $actual['headers']['Content-Type']);
    $parts = explode('; ', $parts[0]);
    parse_str($parts[1], $parts);
    $delimiter = substr($parts['boundary'], 1, strlen($parts['boundary']) - 2);
    $this
      ->assertStringStartsWith('--' . $delimiter, $actual['content']);
    $this
      ->assertStringEndsWith('--' . $delimiter . '--', $actual['content']);
    $this
      ->assertRegExp("/\r\nFoo!\r\n/", $actual['content']);
    $this
      ->assertRegExp("/\r\nBar\r\n/", $actual['content']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MultiresponseNormalizerTest::$sut protected property
MultiresponseNormalizerTest::dataProviderSupportsNormalization public function
MultiresponseNormalizerTest::setUp protected function Overrides UnitTestCase::setUp
MultiresponseNormalizerTest::testNormalize public function @covers ::normalize
MultiresponseNormalizerTest::testSupportsNormalization public function @dataProvider dataProviderSupportsNormalization @covers ::supportsNormalization
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.