You are here

class ResponsiveImageTest in Image Lazyloader 8

@coversDefaultClass \Drupal\lazyloader\ResponsiveImage @group lazyloader

Hierarchy

Expanded class hierarchy of ResponsiveImageTest

File

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

Namespace

Drupal\Tests\lazyloader\Unit
View source
class ResponsiveImageTest extends TestCase {

  /**
   * @covers ::count
   * @covers ::parse
   */
  public function testSingleImage() {
    $string = 'mall.jpg 500w';
    $image = ResponsiveImage::parse($string);
    $this
      ->assertCount(1, $image);
    $this
      ->assertEquals('mall.jpg', $image
      ->get(0)->uri);
    $this
      ->assertEquals('500', $image
      ->get(0)->width);
    $this
      ->assertNull($image
      ->get(0)->density);
    return $image;
  }

  /**
   * Tests the to string of a single image.
   *
   * @depends testSingleImage
   */
  public function testSingleImageToString(ResponsiveImage $image) {
    $string = 'mall.jpg 500w';
    $this
      ->assertEquals($string, $image
      ->__toString());
  }

  /**
   * @covers ::count
   * @covers ::parse
   */
  public function testSingleImageWithDensity() {
    $string = 'mall.jpg 2x';
    $image = ResponsiveImage::parse($string);
    $this
      ->assertCount(1, $image);
    $this
      ->assertEquals('mall.jpg', $image
      ->get(0)->uri);
    $this
      ->assertNull($image
      ->get(0)->width);
    $this
      ->assertEquals('2', $image
      ->get(0)->density);
    $this
      ->assertEquals($string, $image
      ->__toString());
    return $image;
  }

  /**
   * Tests single images with density to string.
   *
   * @depends testSingleImageWithDensity
   */
  public function testSingleImageWithDensityToString(ResponsiveImage $image) {
    $string = 'mall.jpg 2x';
    $this
      ->assertEquals($string, $image
      ->__toString());
  }

  /**
   * Tests multiple images.
   *
   * @covers ::count
   * @covers ::parse
   */
  public function testMultipleImages() {
    $string = 'small.jpg 500w,
		medium.jpg 3x,

  big.jpg 1024w';
    $image = ResponsiveImage::parse($string);
    $this
      ->assertCount(3, $image);
    $this
      ->assertEquals('small.jpg', $image
      ->get(0)->uri);
    $this
      ->assertEquals('medium.jpg', $image
      ->get(1)->uri);
    $this
      ->assertEquals('big.jpg', $image
      ->get(2)->uri);
    $this
      ->assertEquals('500', $image
      ->get(0)->width);
    $this
      ->assertNull($image
      ->get(1)->width);
    $this
      ->assertEquals('1024', $image
      ->get(2)->width);
    $this
      ->assertNull($image
      ->get(0)->density);
    $this
      ->assertEquals('3', $image
      ->get(1)->density);
    $this
      ->assertNull($image
      ->get(2)->density);
    return $image;
  }

  /**
   * Tests the to string function for multiple images.
   *
   * @depends testMultipleImages
   */
  public function testMultipleImagesToString(ResponsiveImage $image) {
    $string = 'small.jpg 500w, medium.jpg 3x, big.jpg 1024w';
    $this
      ->assertEquals($string, $image
      ->__toString());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResponsiveImageTest::testMultipleImages public function Tests multiple images.
ResponsiveImageTest::testMultipleImagesToString public function Tests the to string function for multiple images.
ResponsiveImageTest::testSingleImage public function @covers ::count @covers ::parse
ResponsiveImageTest::testSingleImageToString public function Tests the to string of a single image.
ResponsiveImageTest::testSingleImageWithDensity public function @covers ::count @covers ::parse
ResponsiveImageTest::testSingleImageWithDensityToString public function Tests single images with density to string.