You are here

class WebpTest in WebP 8

Simple test to ensure that asserts pass.

@group phpunit_example

Hierarchy

Expanded class hierarchy of WebpTest

File

tests/src/Unit/WebpTest.php, line 12

Namespace

Drupal\Tests\webp\Unit
View source
class WebpTest extends UnitTestCase {

  /**
   * Before a test method is run, setUp() is invoked.
   *
   * Create new unit object.
   */
  public function setUp() {

    // Mock the class to avoid the constructor.
    $this->webp = $this
      ->getMockBuilder('\\Drupal\\webp\\Webp')
      ->disableOriginalConstructor()
      ->setMethods(NULL)
      ->getMock();
  }

  /**
   * @covers Drupal\webp\Webp::getWebpSrcset
   */
  public function testgetWebpSrcset() {
    $this
      ->assertEquals("testimage.webp", $this->webp
      ->getWebpSrcset("testimage.jpg"));
    $this
      ->assertEquals("testimage2.webp", $this->webp
      ->getWebpSrcset("testimage2.png"));
    $this
      ->assertEquals("testimage2.webp", $this->webp
      ->getWebpSrcset("testimage2.jpeg"));
    $this
      ->assertEquals("testimage2.webp", $this->webp
      ->getWebpSrcset("testimage2.jpg"));
    $this
      ->assertEquals("testimage2.ext.webp", $this->webp
      ->getWebpSrcset("testimage2.ext.jpg"));
    $this
      ->assertEquals("testimage2.ext.ext.webp", $this->webp
      ->getWebpSrcset("testimage2.ext.ext.jpg"));

    // Test that double extensions are handled properly.
    $this
      ->assertEquals("testimage2.png.webp", $this->webp
      ->getWebpSrcset("testimage2.png.jpg"));
    $this
      ->assertEquals("testimage2.jpeg.png.webp", $this->webp
      ->getWebpSrcset("testimage2.jpeg.png.jpg"));

    // Test source sets with width descriptor/pixel density and multiple images.
    $this
      ->assertEquals("some/path/image.webp?itok=vOpRgtYZ 1x", $this->webp
      ->getWebpSrcset("some/path/image.JPG?itok=vOpRgtYZ 1x"));
    $this
      ->assertEquals("some/path/image.webp?itok=vOpRgtYZ 1x, some/path/image.webp?itok=vOpRgtYZ 2x", $this->webp
      ->getWebpSrcset("some/path/image.JPG?itok=vOpRgtYZ 1x, some/path/image.JPG?itok=vOpRgtYZ 2x"));

    // Test source sets with multiple images but without width descriptor/pixel density.
    $this
      ->assertEquals("some/path/image.webp?itok=vOpRgtYZ, some/path/image.webp?itok=vOpRgtYZ", $this->webp
      ->getWebpSrcset("some/path/image.JPG?itok=vOpRgtYZ, some/path/image.JPG?itok=vOpRgtYZ"));

    // And multiple source sets with multiple images.
    $this
      ->assertEquals("some/path/image.png.webp?itok=vOpRgtYZ 1x, some/path/image.jpg.ext.webp?itok=vOpRgtYZ 2x", $this->webp
      ->getWebpSrcset("some/path/image.png.JPG?itok=vOpRgtYZ 1x, some/path/image.jpg.ext.JPG?itok=vOpRgtYZ 2x"));
    $this
      ->assertEquals("some/path/image.png.webp?itok=vOpRgtYZ, some/path/image.jpg.ext.webp?itok=vOpRgtYZ", $this->webp
      ->getWebpSrcset("some/path/image.png.JPG?itok=vOpRgtYZ, some/path/image.jpg.ext.JPG?itok=vOpRgtYZ"));
  }

}

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.
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.
WebpTest::setUp public function Before a test method is run, setUp() is invoked. Overrides UnitTestCase::setUp
WebpTest::testgetWebpSrcset public function @covers Drupal\webp\Webp::getWebpSrcset