You are here

class FixtureTest in Mini site 8

Class FixtureTest.

Tests for fixture creation trait (tests for test helpers).

@group minisite

@package Drupal\testmode\Tests

Hierarchy

Expanded class hierarchy of FixtureTest

File

tests/src/Unit/FixtureTest.php, line 19

Namespace

Drupal\Tests\minisite\Unit
View source
class FixtureTest extends UnitTestCase {
  use FixtureTrait;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->fixtureSetUp();
  }

  /**
   * {@inheritdoc}
   */
  protected function tearDown() {
    parent::tearDown();
    $this
      ->fixtureTearDown();
  }

  /**
   * Test SetUp and TearDown methods for the trait.
   */
  public function testSetupTeardown() {

    // $fixtureDir is already populated from the setUp() of this test,
    // so we need to remove it.
    $fs = new Filesystem();
    $fs
      ->remove($this->fixtureDir);
    $this
      ->fixtureSetUp();
    $first_dir = $this->fixtureDir;
    $this
      ->assertNotEmpty($first_dir, 'fixtureSetUp() populates $fixtureDir variable');
    $this
      ->assertDirectoryExists($first_dir, 'fixtureSetUp() creates directory');
    $this
      ->fixtureSetUp();
    $second_dir = $this->fixtureDir;
    $this
      ->assertNotEmpty($second_dir, 'fixtureSetUp() populates $fixtureDir variable');
    $this
      ->assertDirectoryExists($second_dir, 'fixtureSetUp() creates directory');
    $this
      ->assertNotEquals($first_dir, $second_dir, 'fixtureSetUp() creates new directory on every call');
    $this
      ->fixtureTearDown();
    $this
      ->assertDirectoryNotExists($second_dir, 'fixtureTearDown() removes directory');
    $this
      ->assertEmpty($this->fixtureDir, 'fixtureTearDown() removes the value from $fixtureDir');
  }

  /**
   * Test fixtureCreateFiles() method.
   */
  public function testFixtureCreateFiles() {

    // @codingStandardsIgnoreStart
    $files = [
      'dir1',
      'file1' => 'content1',
      'dir2/file21' => 'content21',
      'dir2/file22' => 'content22',
      'dir3/dir31/dir/311',
    ];

    // @codingStandardsIgnoreEnd
    $expected_files = [
      $this->fixtureDir . \DIRECTORY_SEPARATOR . 'dir1' => 'dir1',
      $this->fixtureDir . \DIRECTORY_SEPARATOR . 'file1' => 'file1',
      $this->fixtureDir . \DIRECTORY_SEPARATOR . 'dir2/file21' => 'dir2/file21',
      $this->fixtureDir . \DIRECTORY_SEPARATOR . 'dir2/file22' => 'dir2/file22',
      $this->fixtureDir . \DIRECTORY_SEPARATOR . 'dir3/dir31/dir/311' => 'dir3/dir31/dir/311',
    ];
    $actual_files = $this
      ->fixtureCreateFiles($files);
    $this
      ->assertEquals($expected_files, $actual_files);
    $this
      ->assertDirectoryExists($this->fixtureDir . \DIRECTORY_SEPARATOR . 'dir1');
    $this
      ->assertFileExists($this->fixtureDir . \DIRECTORY_SEPARATOR . 'file1');
    $this
      ->assertStringEqualsFile($this->fixtureDir . \DIRECTORY_SEPARATOR . 'file1', 'content1');
    $this
      ->assertStringEqualsFile($this->fixtureDir . \DIRECTORY_SEPARATOR . 'dir2/file21', 'content21');
    $this
      ->assertStringEqualsFile($this->fixtureDir . \DIRECTORY_SEPARATOR . 'dir2/file22', 'content22');
    $this
      ->assertDirectoryExists($this->fixtureDir . \DIRECTORY_SEPARATOR . 'dir3/dir31/dir/311');
  }

  /**
   * Test fixtureCreateArchive() method.
   */
  public function testFixtureCreateArchive() {

    // @codingStandardsIgnoreStart
    $files = [
      'dir1',
      'file1' => 'content1',
      'dir2/file21' => 'content21',
      'dir2/file22' => 'content22',
      'dir3/dir31/dir/311',
    ];

    // @codingStandardsIgnoreEnd
    $expected_files = [
      'dir1/',
      'file1',
      'dir2/file21',
      'dir2/file22',
      'dir3/dir31/dir/311/',
    ];
    $archive_filename = $this
      ->fixtureCreateArchive($files, 'zip');
    $this
      ->assertFileExists($archive_filename);
    $archive = new Zip($archive_filename);
    $actual_files = $archive
      ->listContents();
    sort($expected_files);
    sort($actual_files);
    $this
      ->assertEquals($expected_files, $actual_files);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FixtureTest::setUp protected function Overrides UnitTestCase::setUp
FixtureTest::tearDown protected function
FixtureTest::testFixtureCreateArchive public function Test fixtureCreateArchive() method.
FixtureTest::testFixtureCreateFiles public function Test fixtureCreateFiles() method.
FixtureTest::testSetupTeardown public function Test SetUp and TearDown methods for the trait.
FixtureTrait::$fixtureDir protected property The directory where fixture files reside for this test.
FixtureTrait::fixtureCreateArchive public function Create archive from an array of specified files.
FixtureTrait::fixtureCreateFile public function Create a single file with content.
FixtureTrait::fixtureCreateFiles public function Create directories and files with content.
FixtureTrait::fixtureHtmlPage public function Create a fixture HTML page.
FixtureTrait::fixtureLink public function Create a fixture link.
FixtureTrait::fixtureSetUp protected function Set up functionality.
FixtureTrait::fixtureTearDown protected function Tear down functionality.
FixtureTrait::getFixtureFileDir public function Get the path to the directory with fixture files.
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.