class SimpletestTestRunResultsStorageTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Test/SimpletestTestRunResultsStorageTest.php \Drupal\KernelTests\Core\Test\SimpletestTestRunResultsStorageTest

@coversDefaultClass \Drupal\Core\Test\SimpletestTestRunResultsStorage
@group Test

Hierarchy

Expanded class hierarchy of SimpletestTestRunResultsStorageTest

File

core/tests/Drupal/KernelTests/Core/Test/SimpletestTestRunResultsStorageTest.php, line 16

Namespace

Drupal\KernelTests\Core\Test
View source
class SimpletestTestRunResultsStorageTest extends KernelTestBase {
  
  /**
   * The database connection for testing.
   *
   * NOTE: this is the connection to the fixture database to allow testing the
   * storage class, NOT the database where actual tests results are stored.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;
  
  /**
   * The test run results storage.
   *
   * @var \Drupal\Core\Test\TestRunResultsStorageInterface
   */
  protected $testRunResultsStorage;
  
  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    parent::setUp();
    $this->connection = Database::getConnection();
    $this->testRunResultsStorage = new SimpletestTestRunResultsStorage($this->connection);
  }
  
  /**
   * @covers ::buildTestingResultsEnvironment
   * @covers ::validateTestingResultsEnvironment
   */
  public function testBuildNewEnvironment() : void {
    $schema = $this->connection
      ->schema();
    $this->assertFalse($schema->tableExists('simpletest'));
    $this->assertFalse($schema->tableExists('simpletest_test_id'));
    $this->assertFalse($this->testRunResultsStorage
      ->validateTestingResultsEnvironment());
    $this->testRunResultsStorage
      ->buildTestingResultsEnvironment(FALSE);
    $this->assertTrue($schema->tableExists('simpletest'));
    $this->assertTrue($schema->tableExists('simpletest_test_id'));
    $this->assertTrue($this->testRunResultsStorage
      ->validateTestingResultsEnvironment());
  }
  
  /**
   * @covers ::buildTestingResultsEnvironment
   * @covers ::validateTestingResultsEnvironment
   * @covers ::createNew
   * @covers ::insertLogEntry
   * @covers ::cleanUp
   */
  public function testBuildEnvironmentKeepingExistingResults() : void {
    $schema = $this->connection
      ->schema();
    // Initial build of the environment.
    $this->testRunResultsStorage
      ->buildTestingResultsEnvironment(FALSE);
    $this->assertEquals(1, $this->testRunResultsStorage
      ->createNew());
    $test_run = TestRun::get($this->testRunResultsStorage, 1);
    $this->assertEquals(1, $this->testRunResultsStorage
      ->insertLogEntry($test_run, $this->getTestLogEntry('Test\\GroundControl')));
    $this->assertEquals(1, $this->connection
      ->select('simpletest')
      ->countQuery()
      ->execute()
      ->fetchField());
    $this->assertEquals(1, $this->connection
      ->select('simpletest_test_id')
      ->countQuery()
      ->execute()
      ->fetchField());
    // Build the environment again, keeping results. Results should be kept.
    $this->testRunResultsStorage
      ->buildTestingResultsEnvironment(TRUE);
    $this->assertTrue($schema->tableExists('simpletest'));
    $this->assertTrue($schema->tableExists('simpletest_test_id'));
    $this->assertTrue($this->testRunResultsStorage
      ->validateTestingResultsEnvironment());
    $this->assertEquals(1, $this->connection
      ->select('simpletest')
      ->countQuery()
      ->execute()
      ->fetchField());
    $this->assertEquals(1, $this->connection
      ->select('simpletest_test_id')
      ->countQuery()
      ->execute()
      ->fetchField());
    $this->assertEquals(2, $this->testRunResultsStorage
      ->createNew());
    $test_run = TestRun::get($this->testRunResultsStorage, 2);
    $this->assertEquals(2, $this->testRunResultsStorage
      ->insertLogEntry($test_run, $this->getTestLogEntry('Test\\GroundControl')));
    $this->assertEquals(2, $this->connection
      ->select('simpletest')
      ->countQuery()
      ->execute()
      ->fetchField());
    $this->assertEquals(2, $this->connection
      ->select('simpletest_test_id')
      ->countQuery()
      ->execute()
      ->fetchField());
    // Cleanup the environment.
    $this->assertEquals(2, $this->testRunResultsStorage
      ->cleanUp());
    $this->assertEquals(0, $this->connection
      ->select('simpletest')
      ->countQuery()
      ->execute()
      ->fetchField());
    $this->assertEquals(0, $this->connection
      ->select('simpletest_test_id')
      ->countQuery()
      ->execute()
      ->fetchField());
  }
  
  /**
   * @covers ::buildTestingResultsEnvironment
   * @covers ::createNew
   * @covers ::insertLogEntry
   * @covers ::setDatabasePrefix
   * @covers ::removeResults
   */
  public function testGetCurrentTestRunState() : void {
    $this->testRunResultsStorage
      ->buildTestingResultsEnvironment(FALSE);
    $this->assertEquals(1, $this->testRunResultsStorage
      ->createNew());
    $test_run_1 = TestRun::get($this->testRunResultsStorage, 1);
    $this->testRunResultsStorage
      ->setDatabasePrefix($test_run_1, 'oddity1234');
    $this->assertEquals(1, $this->testRunResultsStorage
      ->insertLogEntry($test_run_1, $this->getTestLogEntry('Test\\GroundControl')));
    $this->assertEquals([
      'db_prefix' => 'oddity1234',
      'test_class' => 'Test\\GroundControl',
    ], $this->testRunResultsStorage
      ->getCurrentTestRunState($test_run_1));
    // Add another test run.
    $this->assertEquals(2, $this->testRunResultsStorage
      ->createNew());
    $test_run_2 = TestRun::get($this->testRunResultsStorage, 2);
    $this->assertEquals(2, $this->testRunResultsStorage
      ->insertLogEntry($test_run_2, $this->getTestLogEntry('Test\\GroundControl')));
    // Remove test run 1 results.
    $this->assertEquals(1, $this->testRunResultsStorage
      ->removeResults($test_run_1));
    $this->assertEquals(1, $this->connection
      ->select('simpletest')
      ->countQuery()
      ->execute()
      ->fetchField());
    $this->assertEquals(1, $this->connection
      ->select('simpletest_test_id')
      ->countQuery()
      ->execute()
      ->fetchField());
  }
  
  /**
   * @covers ::buildTestingResultsEnvironment
   * @covers ::createNew
   * @covers ::insertLogEntry
   * @covers ::setDatabasePrefix
   * @covers ::getLogEntriesByTestClass
   */
  public function testGetLogEntriesByTestClass() : void {
    $this->testRunResultsStorage
      ->buildTestingResultsEnvironment(FALSE);
    $this->assertEquals(1, $this->testRunResultsStorage
      ->createNew());
    $test_run = TestRun::get($this->testRunResultsStorage, 1);
    $this->testRunResultsStorage
      ->setDatabasePrefix($test_run, 'oddity1234');
    $this->assertEquals(1, $this->testRunResultsStorage
      ->insertLogEntry($test_run, $this->getTestLogEntry('Test\\PlanetEarth')));
    $this->assertEquals(2, $this->testRunResultsStorage
      ->insertLogEntry($test_run, $this->getTestLogEntry('Test\\GroundControl')));
    $this->assertEquals([
      0 => (object) [
        'message_id' => 2,
        'test_id' => 1,
        'test_class' => 'Test\\GroundControl',
        'status' => 'pass',
        'message' => 'Major Tom',
        'message_group' => 'other',
        'function' => 'Unknown',
        'line' => 0,
        'file' => 'Unknown',
      ],
      1 => (object) [
        'message_id' => 1,
        'test_id' => 1,
        'test_class' => 'Test\\PlanetEarth',
        'status' => 'pass',
        'message' => 'Major Tom',
        'message_group' => 'other',
        'function' => 'Unknown',
        'line' => 0,
        'file' => 'Unknown',
      ],
    ], $this->testRunResultsStorage
      ->getLogEntriesByTestClass($test_run));
  }
  
  /**
   * Returns a sample test run log entry.
   *
   * @param string $test_class
   *   The test class.
   *
   * @return string[]
   *   An array with the elements to be logged.
   */
  protected function getTestLogEntry(string $test_class) : array {
    return [
      'test_class' => $test_class,
      'status' => 'pass',
      'message' => 'Major Tom',
      'message_group' => 'other',
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
SimpletestTestRunResultsStorageTest::$connection protected property The database connection for testing.
SimpletestTestRunResultsStorageTest::$testRunResultsStorage protected property The test run results storage.
SimpletestTestRunResultsStorageTest::getTestLogEntry protected function Returns a sample test run log entry.
SimpletestTestRunResultsStorageTest::setUp public function
SimpletestTestRunResultsStorageTest::testBuildEnvironmentKeepingExistingResults public function @covers ::buildTestingResultsEnvironment[[api-linebreak]]
@covers ::validateTestingResultsEnvironment[[api-linebreak]]
@covers ::createNew[[api-linebreak]]
@covers ::insertLogEntry[[api-linebreak]]
@covers ::cleanUp[[api-linebreak]]
SimpletestTestRunResultsStorageTest::testBuildNewEnvironment public function @covers ::buildTestingResultsEnvironment[[api-linebreak]]
@covers ::validateTestingResultsEnvironment[[api-linebreak]]
SimpletestTestRunResultsStorageTest::testGetCurrentTestRunState public function @covers ::buildTestingResultsEnvironment[[api-linebreak]]
@covers ::createNew[[api-linebreak]]
@covers ::insertLogEntry[[api-linebreak]]
@covers ::setDatabasePrefix[[api-linebreak]]
@covers ::removeResults[[api-linebreak]]
SimpletestTestRunResultsStorageTest::testGetLogEntriesByTestClass public function @covers ::buildTestingResultsEnvironment[[api-linebreak]]
@covers ::createNew[[api-linebreak]]
@covers ::insertLogEntry[[api-linebreak]]
@covers ::setDatabasePrefix[[api-linebreak]]
@covers ::getLogEntriesByTestClass[[api-linebreak]]
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.

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