1<?php
2
3namespace Drupal\Tests\Core\Test;
4
5use Drupal\Core\Test\TestDatabase;
6use Drupal\Tests\UnitTestCase;
7
8/**
9 * @coversDefaultClass \Drupal\Core\Test\TestDatabase
10 *
11 * @group Test
12 * @group simpletest
13 * @group Template
14 */
15class TestDatabaseTest extends UnitTestCase {
16
17  /**
18   * @covers ::__construct
19   */
20  public function testConstructorException() {
21    $this->expectException(\InvalidArgumentException::class);
22    $this->expectExceptionMessage("Invalid database prefix: blah1253");
23    new TestDatabase('blah1253');
24  }
25
26  /**
27   * @covers ::__construct
28   * @covers ::getDatabasePrefix
29   * @covers ::getTestSitePath
30   *
31   * @dataProvider providerTestConstructor
32   */
33  public function testConstructor($db_prefix, $expected_db_prefix, $expected_site_path) {
34    $test_db = new TestDatabase($db_prefix);
35    $this->assertEquals($expected_db_prefix, $test_db->getDatabasePrefix());
36    $this->assertEquals($expected_site_path, $test_db->getTestSitePath());
37  }
38
39  /**
40   * Data provider for self::testConstructor()
41   */
42  public function providerTestConstructor() {
43    return [
44      ['test1234', 'test1234', 'sites/simpletest/1234'],
45      ['test123456test234567', 'test123456test234567', 'sites/simpletest/234567'],
46    ];
47  }
48
49  /**
50   * Verify that a test lock is generated if there is no provided prefix.
51   *
52   * @covers ::__construct
53   */
54  public function testConstructorNullPrefix() {
55    // We use a stub class here because we can't mock getTestLock() so that it's
56    // available before the constructor is called.
57    $test_db = new TestTestDatabase(NULL);
58
59    $this->assertEquals('test23', $test_db->getDatabasePrefix());
60    $this->assertEquals('sites/simpletest/23', $test_db->getTestSitePath());
61  }
62
63}
64
65/**
66 * Stub class supports TestDatabaseTest::testConstructorNullPrefix().
67 */
68class TestTestDatabase extends TestDatabase {
69
70  protected function getTestLock($create_lock = FALSE) {
71    return 23;
72  }
73
74}
75