class BuildTestTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/BuildTests/Framework/Tests/BuildTestTest.php \Drupal\BuildTests\Framework\Tests\BuildTestTest
  2. 8.9.x core/tests/Drupal/BuildTests/Framework/Tests/BuildTestTest.php \Drupal\BuildTests\Framework\Tests\BuildTestTest
  3. 11.x core/tests/Drupal/BuildTests/Framework/Tests/BuildTestTest.php \Drupal\BuildTests\Framework\Tests\BuildTestTest

@coversDefaultClass \Drupal\BuildTests\Framework\BuildTestBase
@group Build

Hierarchy

Expanded class hierarchy of BuildTestTest

File

core/tests/Drupal/BuildTests/Framework/Tests/BuildTestTest.php, line 16

Namespace

Drupal\BuildTests\Framework\Tests
View source
class BuildTestTest extends BuildTestBase {
  
  /**
   * Ensure that workspaces work.
   */
  public function testWorkspace() : void {
    $test_directory = 'test_directory';
    // Execute an empty command through the shell to build out a working
    // directory.
    $process = $this->executeCommand('', $test_directory);
    $this->assertCommandSuccessful();
    // Assert that our working directory exists and is in use by the process.
    $workspace = $this->getWorkspaceDirectory();
    $working_path = $workspace . '/' . $test_directory;
    $this->assertDirectoryExists($working_path);
    $this->assertEquals($working_path, $process->getWorkingDirectory());
  }
  
  /**
   * @covers ::copyCodebase
   */
  public function testCopyCodebase() : void {
    $test_directory = 'copied_codebase';
    $this->copyCodebase(NULL, $test_directory);
    $full_path = $this->getWorkspaceDirectory() . '/' . $test_directory;
    $files = [
      'autoload.php',
      'composer.json',
      'index.php',
      'README.md',
      '.git',
      '.ht.router.php',
    ];
    foreach ($files as $file) {
      $this->assertFileExists($full_path . '/' . $file);
    }
  }
  
  /**
   * Ensure we're not copying directories we wish to exclude.
   *
   * @covers ::copyCodebase
   */
  public function testCopyCodebaseExclude() : void {
    // Create a virtual file system containing items that should be
    // excluded. Exception being modules directory.
    vfsStream::setup('drupal', NULL, [
      'sites' => [
        'default' => [
          'files' => [
            'a_file.txt' => 'some file.',
          ],
          'settings.php' => '<?php $settings = stuff;',
          'settings.local.php' => '<?php $settings = override;',
        ],
        'simpletest' => [
          'simpletest_hash' => [
            'some_results.xml' => '<xml/>',
          ],
        ],
      ],
      'modules' => [
        'my_module' => [
          'vendor' => [
            'my_vendor' => [
              'composer.json' => "{\n}",
            ],
          ],
        ],
      ],
    ]);
    // Mock BuildTestBase so that it thinks our VFS is the Composer and Drupal
    // roots.
    /** @var \PHPUnit\Framework\MockObject\MockBuilder|\Drupal\BuildTests\Framework\BuildTestBase $base */
    $base = $this->getMockBuilder(BuildTestBase::class)
      ->onlyMethods([
      'getDrupalRoot',
      'getComposerRoot',
    ])
      ->getMockForAbstractClass();
    $base->expects($this->exactly(1))
      ->method('getDrupalRoot')
      ->willReturn(vfsStream::url('drupal'));
    $base->expects($this->exactly(3))
      ->method('getComposerRoot')
      ->willReturn(vfsStream::url('drupal'));
    $base->setUp();
    // Perform the copy.
    $test_directory = 'copied_codebase';
    $base->copyCodebase(NULL, $test_directory);
    $full_path = $base->getWorkspaceDirectory() . '/' . $test_directory;
    $this->assertDirectoryExists($full_path);
    // Verify nested vendor directory was not excluded. Then remove it for next
    // validation.
    $this->assertFileExists($full_path . DIRECTORY_SEPARATOR . 'modules/my_module/vendor/my_vendor/composer.json');
    $file_system = new Filesystem();
    $file_system->remove($full_path . DIRECTORY_SEPARATOR . 'modules');
    // Use scandir() to determine if our target directory is empty. It should
    // only contain the system dot directories.
    $this->assertTrue(($files = @scandir($full_path)) && count($files) <= 2, 'Directory is not empty: ' . implode(', ', $files));
    $base->tearDown();
  }
  
  /**
   * Tests copying codebase when Drupal and Composer roots are different.
   *
   * @covers ::copyCodebase
   */
  public function testCopyCodebaseDocRoot() : void {
    // Create a virtual file system containing items that should be
    // excluded. Exception being modules directory.
    vfsStream::setup('drupal', NULL, [
      'docroot' => [
        'sites' => [
          'default' => [
            'files' => [
              'a_file.txt' => 'some file.',
            ],
            'settings.php' => '<?php $settings = "stuff";',
            'settings.local.php' => '<?php $settings = "override";',
            'default.settings.php' => '<?php $settings = "default";',
          ],
          'simpletest' => [
            'simpletest_hash' => [
              'some_results.xml' => '<xml/>',
            ],
          ],
        ],
        'modules' => [
          'my_module' => [
            'vendor' => [
              'my_vendor' => [
                'composer.json' => "{\n}",
              ],
            ],
          ],
        ],
      ],
      'vendor' => [
        'test.txt' => 'File exists',
      ],
    ]);
    // Mock BuildTestBase so that it thinks our VFS is the Composer and Drupal
    // roots.
    /** @var \PHPUnit\Framework\MockObject\MockBuilder|\Drupal\BuildTests\Framework\BuildTestBase $base */
    $base = $this->getMockBuilder(BuildTestBase::class)
      ->onlyMethods([
      'getDrupalRoot',
      'getComposerRoot',
    ])
      ->getMockForAbstractClass();
    $base->expects($this->exactly(3))
      ->method('getDrupalRoot')
      ->willReturn(vfsStream::url('drupal/docroot'));
    $base->expects($this->exactly(5))
      ->method('getComposerRoot')
      ->willReturn(vfsStream::url('drupal'));
    $base->setUp();
    // Perform the copy.
    $base->copyCodebase();
    $full_path = $base->getWorkspaceDirectory();
    $this->assertDirectoryExists($full_path . '/docroot');
    // Verify expected files exist.
    $this->assertFileExists($full_path . DIRECTORY_SEPARATOR . 'docroot/modules/my_module/vendor/my_vendor/composer.json');
    $this->assertFileExists($full_path . DIRECTORY_SEPARATOR . 'docroot/sites/default/default.settings.php');
    $this->assertFileExists($full_path . DIRECTORY_SEPARATOR . 'vendor');
    // Verify expected files do not exist
    $this->assertFileDoesNotExist($full_path . DIRECTORY_SEPARATOR . 'docroot/sites/default/settings.php');
    $this->assertFileDoesNotExist($full_path . DIRECTORY_SEPARATOR . 'docroot/sites/default/settings.local.php');
    $this->assertFileDoesNotExist($full_path . DIRECTORY_SEPARATOR . 'docroot/sites/default/files');
    // Ensure that the workspace Drupal root is calculated correctly.
    $this->assertSame($full_path . '/docroot/', $base->getWorkspaceDrupalRoot());
    $this->assertSame('docroot/', $base->getWorkingPathDrupalRoot());
    $base->tearDown();
  }
  
  /**
   * @covers ::findAvailablePort
   */
  public function testPortMany() : void {
    $iterator = (new Finder())->in($this->getDrupalRoot())
      ->ignoreDotFiles(FALSE)
      ->exclude([
      'sites/simpletest',
    ])
      ->path('/^.ht.router.php$/')
      ->getIterator();
    $this->copyCodebase($iterator);
    /** @var \Symfony\Component\Process\Process[] $processes */
    $processes = [];
    $count = 15;
    for ($i = 0; $i <= $count; $i++) {
      $port = $this->findAvailablePort();
      $this->assertArrayNotHasKey($port, $processes, 'Port ' . $port . ' was already in use by a process.');
      $processes[$port] = $this->instantiateServer($port);
      $this->assertNotEmpty($processes[$port]);
      $this->assertTrue($processes[$port]->isRunning(), 'Process on port ' . $port . ' is not still running.');
      $this->assertFalse($this->checkPortIsAvailable($port));
    }
    // Clean up after ourselves.
    foreach ($processes as $process) {
      $process->stop();
    }
  }
  
  /**
   * @covers ::standUpServer
   */
  public function testStandUpServer() : void {
    // Stand up a server with working directory 'first'.
    $this->standUpServer('first');
    // Get the process object for the server.
    $ref_process = new \ReflectionProperty(parent::class, 'serverProcess');
    $first_process = $ref_process->getValue($this);
    // Standing up the server again should not change the server process.
    $this->standUpServer('first');
    $this->assertSame($first_process, $ref_process->getValue($this));
    // Standing up the server with working directory 'second' should give us a
    // new server process.
    $this->standUpServer('second');
    $this->assertNotSame($first_process, $second_process = $ref_process->getValue($this));
    // And even with the original working directory name, we should get a new
    // server process.
    $this->standUpServer('first');
    $this->assertNotSame($first_process, $ref_process->getValue($this));
    $this->assertNotSame($second_process, $ref_process->getValue($this));
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
BuildTestBase::$commandProcess private property The most recent command process.
BuildTestBase::$destroyBuild protected property Default to destroying build artifacts after a test finishes.
BuildTestBase::$hostName private static property Our native host name, used by PHP when it starts up the server.
BuildTestBase::$hostPort private property Port that will be tested.
BuildTestBase::$mink private property The Mink session manager.
BuildTestBase::$phpFinder private property The PHP executable finder.
BuildTestBase::$portLocks private property A list of ports used by the test.
BuildTestBase::$serverDocroot private property The docroot for the server process.
BuildTestBase::$serverProcess private property The process that&#039;s running the HTTP server.
BuildTestBase::$workspaceDir private property The working directory where this test will manipulate files.
BuildTestBase::assertCommandExitCode public function Asserts that the last command returned the specified exit code.
BuildTestBase::assertCommandOutputContains public function Assert that text is present in the output of the most recent command.
BuildTestBase::assertCommandSuccessful public function Asserts that the last command ran without error.
BuildTestBase::assertDrupalVisit public function Helper function to assert that the last visit was a Drupal site.
BuildTestBase::assertErrorOutputContains public function Assert that text is present in the error output of the most recent command.
BuildTestBase::assertErrorOutputNotContains public function Assert text is not present in the error output of the most recent command.
BuildTestBase::checkPortIsAvailable protected function Checks whether a port is available.
BuildTestBase::copyCodebase public function Copy the current working codebase into a workspace.
BuildTestBase::executeCommand public function Run a command.
BuildTestBase::findAvailablePort protected function Discover an available port number.
BuildTestBase::getCodebaseFinder public function Get a default Finder object for a Drupal codebase.
BuildTestBase::getComposerRoot public function Gets the path to the Composer root directory.
BuildTestBase::getDrupalRoot public function Get the root path of this Drupal codebase.
BuildTestBase::getDrupalRootStatic public static function Get the root path of this Drupal codebase.
BuildTestBase::getMink public function Get the Mink instance.
BuildTestBase::getPortNumber protected function Get the port number for requests.
BuildTestBase::getWorkingPath protected function Get the working directory within the workspace, creating if necessary.
BuildTestBase::getWorkingPathDrupalRoot public function Gets the working path for Drupal core.
BuildTestBase::getWorkspaceDirectory public function Full path to the workspace where this test can build.
BuildTestBase::getWorkspaceDrupalRoot public function Gets the path to Drupal root in the workspace directory.
BuildTestBase::initMink protected function Set up the Mink session manager.
BuildTestBase::instantiateServer protected function Do the work of making a server process.
BuildTestBase::setUp protected function 3
BuildTestBase::standUpServer protected function Makes a local test server using PHP&#039;s internal HTTP server.
BuildTestBase::stopServer protected function Stop the HTTP server, zero out all necessary variables.
BuildTestBase::tearDown protected function 1
BuildTestBase::visit public function Visit a URI on the HTTP server.
BuildTestTest::testCopyCodebase public function @covers ::copyCodebase[[api-linebreak]]
BuildTestTest::testCopyCodebaseDocRoot public function Tests copying codebase when Drupal and Composer roots are different.
BuildTestTest::testCopyCodebaseExclude public function Ensure we&#039;re not copying directories we wish to exclude.
BuildTestTest::testPortMany public function @covers ::findAvailablePort[[api-linebreak]]
BuildTestTest::testStandUpServer public function @covers ::standUpServer[[api-linebreak]]
BuildTestTest::testWorkspace public function Ensure that workspaces work.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RequiresComposerTrait::requiresComposer public static function @beforeClass

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.