Commit 50eb47ea authored by webchick's avatar webchick

Issue #1938068 by sun, Berdir: Convert UnitTestBase to PHPUnit.

parent e023a689
......@@ -170,8 +170,12 @@ protected function setUp() {
$this->container = $this->kernel->getContainer();
$this->container->get('request_stack')->push($request);
$this->container->get('state')->set('system.module.files', $this->moduleFiles);
$this->container->get('state')->set('system.theme.files', $this->themeFiles);
// Re-inject extension file listings into state, unless the key/value
// service was overridden (in which case its storage does not exist yet).
if ($this->container->get('keyvalue') instanceof KeyValueMemoryFactory) {
$this->container->get('state')->set('system.module.files', $this->moduleFiles);
$this->container->get('state')->set('system.theme.files', $this->themeFiles);
}
// Create a minimal core.extension configuration object so that the list of
// enabled modules can be maintained allowing
......
......@@ -7,7 +7,7 @@
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\UnitTestBase;
use Drupal\simpletest\KernelTestBase;
/**
* This test should not load since it requires a module that is not found.
......@@ -15,7 +15,8 @@
* @group simpletest
* @requires module simpletest_missing_module
*/
class MissingDependentModuleUnitTest extends UnitTestBase {
class MissingDependentModuleUnitTest extends KernelTestBase {
/**
* Ensure that this test will not be loaded despite its dependency.
*/
......
......@@ -7,14 +7,20 @@
namespace Drupal\system\Tests\Bootstrap;
use Drupal\simpletest\UnitTestBase;
use Drupal\simpletest\KernelTestBase;
/**
* Tests that drupal_get_filename() works correctly.
*
* @group Bootstrap
*/
class GetFilenameUnitTest extends UnitTestBase {
class GetFilenameUnitTest extends KernelTestBase {
protected function setUp() {
parent::setUp();
$this->container = NULL;
\Drupal::setContainer(NULL);
}
/**
* Tests that drupal_get_filename() works when the file is not in database.
......@@ -25,9 +31,10 @@ function testDrupalGetFilename() {
global $install_state;
$install_state['parameters']['profile'] = 'testing';
// Assert that the test is meaningful by making sure the keyvalue service
// does not exist.
$this->assertFalse(\Drupal::hasService('keyvalue'), 'The container has no keyvalue service.');
// Assert that this test is meaningful.
$this->assertNull($this->container);
$this->assertNull(\Drupal::getContainer());
// Retrieving the location of a module.
$this->assertIdentical(drupal_get_filename('module', 'system'), 'core/modules/system/system.info.yml');
......
......@@ -7,14 +7,14 @@
namespace Drupal\system\Tests\Bootstrap;
use Drupal\simpletest\UnitTestBase;
use Drupal\simpletest\KernelTestBase;
/**
* Tests that drupal_static() and drupal_static_reset() work.
*
* @group Bootstrap
*/
class ResettableStaticUnitTest extends UnitTestBase {
class ResettableStaticUnitTest extends KernelTestBase {
/**
* Tests drupal_static() function.
......
......@@ -8,14 +8,14 @@
namespace Drupal\system\Tests\Database;
use Drupal\Core\Database\Database;
use Drupal\simpletest\UnitTestBase;
use Drupal\simpletest\KernelTestBase;
/**
* Tests management of database connections.
*
* @group Database
*/
class ConnectionUnitTest extends UnitTestBase {
class ConnectionUnitTest extends KernelTestBase {
protected $key;
protected $target;
......@@ -240,6 +240,12 @@ public function testConnectionSerialization() {
$unserialized_reflection = new \ReflectionObject($unserialized);
foreach ($db_reflection->getProperties() as $value) {
$value->setAccessible(TRUE);
// Skip properties that are lazily populated on access.
if ($value->getName() === 'driverClasses' || $value->getName() === 'schema') {
continue;
}
$unserialized_property = $unserialized_reflection->getProperty($value->getName());
$unserialized_property->setAccessible(TRUE);
// For the PDO object, just check the statement class attribute.
......@@ -252,7 +258,13 @@ public function testConnectionSerialization() {
$this->assertEqual(get_class($unserialized_statement_class[1][0]), get_class($db_statement_class[1][0]));
}
else {
$this->assertEqual($unserialized_property->getValue($unserialized), $value->getValue($db));
$actual = $unserialized_property->getValue($unserialized);
$expected = $value->getValue($db);
$this->assertEqual($actual, $expected, vsprintf('Unserialized Connection property %s value %s is equal to expected %s', array(
var_export($value->getName(), TRUE),
is_object($actual) ? print_r($actual, TRUE) : var_export($actual, TRUE),
is_object($expected) ? print_r($expected, TRUE) : var_export($expected, TRUE),
)));
}
}
......
......@@ -9,14 +9,15 @@
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Database\Database;
use Drupal\simpletest\UnitTestBase;
use Drupal\simpletest\KernelTestBase;
/**
* Tests exceptions thrown by queries.
*
* @group Database
*/
class DatabaseExceptionWrapperTest extends UnitTestBase {
class DatabaseExceptionWrapperTest extends KernelTestBase {
function testDatabaseExceptionWrapper() {
$connection = Database::getConnection();
$query = $connection->prepare('bananas');
......
......@@ -10,14 +10,14 @@
use Drupal\Core\Database\Database;
use Drupal\Core\Database\SchemaObjectDoesNotExistException;
use Drupal\Core\Database\SchemaObjectExistsException;
use Drupal\simpletest\UnitTestBase;
use Drupal\simpletest\KernelTestBase;
/**
* Tests table creation and modification via the schema API.
*
* @group Database
*/
class SchemaTest extends UnitTestBase {
class SchemaTest extends KernelTestBase {
/**
* A global counter for table and field creation.
......
......@@ -7,8 +7,8 @@
namespace Drupal\system\Tests\KeyValueStore;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\KeyValueStore\KeyValueFactory;
use Symfony\Component\DependencyInjection\Reference;
/**
* Tests the key-value database storage.
......@@ -17,35 +17,27 @@
*/
class DatabaseStorageExpirableTest extends StorageTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system');
protected function setUp() {
parent::setUp();
$this->factory = 'keyvalue.expirable';
module_load_install('system');
$schema = system_schema();
db_create_table('key_value_expire', $schema['key_value_expire']);
$this->container
->register('database', 'Drupal\Core\Database\Connection')
->setFactoryClass('Drupal\Core\Database\Database')
->setFactoryMethod('getConnection')
->addArgument('default');
$this->container
->register('keyvalue.expirable.database', 'Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory')
->addArgument(new Reference('serialization.phpserialize'))
->addArgument(new Reference('database'));
$this->container
->register('serialization.phpserialize', 'Drupal\Component\Serialization\PhpSerialize');
$parameter = array();
$parameter[KeyValueFactory::DEFAULT_SETTING] = 'keyvalue.expirable.database';
$this->container->setParameter('factory.keyvalue.expirable', $parameter);
$this->installSchema('system', array('key_value_expire'));
}
protected function tearDown() {
// The DatabaseExpirableStorage class has a destructor which deletes rows
// from the key_value_expire table. We need to make sure the destructor
// runs before the table is deleted.
$this->container->set('keyvalue.expirable', NULL);
db_drop_table('key_value_expire');
parent::tearDown();
/**
* {@inheritdoc}
*/
public function containerBuild(ContainerBuilder $container) {
parent::containerBuild($container);
$parameter[KeyValueFactory::DEFAULT_SETTING] = 'keyvalue.expirable.database';
$container->setParameter('factory.keyvalue.expirable', $parameter);
}
/**
......
......@@ -7,8 +7,8 @@
namespace Drupal\system\Tests\KeyValueStore;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\KeyValueStore\KeyValueFactory;
use Symfony\Component\DependencyInjection\Reference;
/**
* Tests the key-value database storage.
......@@ -17,30 +17,26 @@
*/
class DatabaseStorageTest extends StorageTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system');
protected function setUp() {
parent::setUp();
module_load_install('system');
$schema = system_schema();
db_create_table('key_value', $schema['key_value']);
$this->container
->register('database', 'Drupal\Core\Database\Connection')
->setFactoryClass('Drupal\Core\Database\Database')
->setFactoryMethod('getConnection')
->addArgument('default');
$this->container
->register('keyvalue.database', 'Drupal\Core\KeyValueStore\KeyValueDatabaseFactory')
->addArgument(new Reference('serialization.phpserialize'))
->addArgument(new Reference('database'));
$this->container
->register('serialization.phpserialize', 'Drupal\Component\Serialization\PhpSerialize');
$parameter = array();
$parameter[KeyValueFactory::DEFAULT_SETTING] = 'keyvalue.database';
$this->container->setParameter('factory.keyvalue', $parameter);
$this->installSchema('system', array('key_value'));
}
protected function tearDown() {
db_drop_table('key_value');
parent::tearDown();
/**
* {@inheritdoc}
*/
public function containerBuild(ContainerBuilder $container) {
parent::containerBuild($container);
$parameter[KeyValueFactory::DEFAULT_SETTING] = 'keyvalue.database';
$container->setParameter('factory.keyvalue', $parameter);
}
}
......@@ -10,26 +10,27 @@
use Drupal\Component\Serialization\PhpSerialize;
use Drupal\Core\Database\Database;
use Drupal\Core\KeyValueStore\DatabaseStorageExpirable;
use Drupal\simpletest\UnitTestBase;
use Drupal\simpletest\KernelTestBase;
/**
* Tests garbage collection for the the expirable key-value database storage.
*
* @group KeyValueStore
*/
class GarbageCollectionTest extends UnitTestBase {
class GarbageCollectionTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system');
protected function setUp() {
parent::setUp();
module_load_install('system');
$schema = system_schema();
db_create_table('key_value_expire', $schema['key_value_expire']);
$this->installSchema('system', array('key_value_expire'));
}
protected function tearDown() {
db_drop_table('key_value_expire');
parent::tearDown();
}
/**
* Tests garbage collection.
......
......@@ -6,6 +6,8 @@
*/
namespace Drupal\system\Tests\KeyValueStore;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\KeyValueStore\KeyValueFactory;
/**
......@@ -16,19 +18,14 @@
class MemoryStorageTest extends StorageTestBase {
/**
* Holds the original default key/value service name.
*
* @var String
* {@inheritdoc}
*/
protected $originalKeyValue = NULL;
public function containerBuild(ContainerBuilder $container) {
parent::containerBuild($container);
protected function setUp() {
parent::setUp();
$this->container
->register('keyvalue.memory', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory');
$parameter = array();
$container->register('keyvalue.memory', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory');
$parameter[KeyValueFactory::DEFAULT_SETTING] = 'keyvalue.memory';
$this->container->setParameter('factory.keyvalue', $parameter);
$container->setParameter('factory.keyvalue', $parameter);
}
}
......@@ -7,14 +7,12 @@
namespace Drupal\system\Tests\KeyValueStore;
use Drupal\simpletest\UnitTestBase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Drupal\simpletest\KernelTestBase;
/**
* Base class for testing key-value storages.
*/
abstract class StorageTestBase extends UnitTestBase {
abstract class StorageTestBase extends KernelTestBase {
/**
* An array of random stdClass objects.
......@@ -37,32 +35,9 @@ abstract class StorageTestBase extends UnitTestBase {
*/
protected $factory = 'keyvalue';
/**
* A container for the services needed in these tests.
*
* @var ContainerBuilder
*/
protected $container;
protected function setUp() {
parent::setUp();
$this->container = new ContainerBuilder();
$this->container
->register('service_container', 'Symfony\Component\DependencyInjection\ContainerBuilder')
->setSynthetic(TRUE);
$this->container->set('service_container', $this->container);
$this->container->register('settings', 'Drupal\Core\Site\Settings')
->setFactoryClass('Drupal\Core\Site\Settings')
->setFactoryMethod('getInstance');
$this->container
->register('keyvalue', 'Drupal\Core\KeyValueStore\KeyValueFactory')
->addArgument(new Reference('service_container'))
->addArgument('%factory.keyvalue%');
$this->container
->register('keyvalue.expirable', 'Drupal\Core\KeyValueStore\KeyValueExpirableFactory')
->addArgument(new Reference('service_container'))
->addArgument('%factory.keyvalue.expirable%');
// Define two data collections,
$this->collections = array(0 => 'zero', 1 => 'one');
......
......@@ -9,10 +9,9 @@
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\State\State;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Drupal\simpletest\UnitTestBase;
use Drupal\Core\Database\Database;
use Drupal\Core\Routing\MatcherDumper;
use Drupal\Tests\Core\Routing\RoutingFixtures;
......@@ -22,7 +21,7 @@
*
* @group Routing
*/
class MatcherDumperTest extends UnitTestBase {
class MatcherDumperTest extends KernelTestBase {
/**
* A collection of shared fixture data for tests.
......@@ -38,17 +37,13 @@ class MatcherDumperTest extends UnitTestBase {
*/
protected $state;
function __construct($test_id = NULL) {
parent::__construct($test_id);
protected function setUp() {
parent::setUp();
$this->fixtures = new RoutingFixtures();
$this->state = new State(new KeyValueMemoryFactory());
}
protected function setUp() {
parent::setUp();
}
/**
* Confirms that the dumper can be instantiated successfully.
*/
......
......@@ -9,13 +9,12 @@
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\State\State;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Drupal\simpletest\UnitTestBase;
use Drupal\Core\Routing\RouteProvider;
use Drupal\Core\Database\Database;
use Drupal\Core\Routing\MatcherDumper;
......@@ -27,7 +26,7 @@
*
* @group Routing
*/
class RouteProviderTest extends UnitTestBase {
class RouteProviderTest extends KernelTestBase {
/**
* A collection of shared fixture data for tests.
......
......@@ -9,7 +9,7 @@
use Drupal\Core\Database\Database;
use Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber;
use Drupal\Core\DrupalKernel;
use Drupal\simpletest\UnitTestBase;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
......@@ -19,7 +19,7 @@
*
* @group system
*/
class IgnoreReplicaSubscriberTest extends UnitTestBase {
class IgnoreReplicaSubscriberTest extends KernelTestBase {
/**
* Tests \Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber::checkReplicaServer().
......
......@@ -8,7 +8,7 @@
namespace Drupal\user\Tests;
use Drupal\Component\Serialization\PhpSerialize;
use Drupal\simpletest\UnitTestBase;
use Drupal\simpletest\KernelTestBase;
use Drupal\user\TempStoreFactory;
use Drupal\Core\Lock\DatabaseLockBackend;
use Drupal\Core\Database\Database;
......@@ -19,7 +19,14 @@
* @group user
* @see \Drupal\Core\TempStore\TempStore.
*/
class TempStoreDatabaseTest extends UnitTestBase {
class TempStoreDatabaseTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system', 'user');
/**
* A key/value store factory.
......@@ -54,10 +61,7 @@ protected function setUp() {
// Install system tables to test the key/value storage without installing a
// full Drupal environment.
module_load_install('system');
$schema = system_schema();
db_create_table('semaphore', $schema['semaphore']);
db_create_table('key_value_expire', $schema['key_value_expire']);
$this->installSchema('system', array('semaphore', 'key_value_expire'));
// Create several objects for testing.
for ($i = 0; $i <= 3; $i++) {
......@@ -66,12 +70,6 @@ protected function setUp() {
}
protected function tearDown() {
db_drop_table('key_value_expire');
db_drop_table('semaphore');
parent::tearDown();
}
/**
* Tests the UserTempStore API.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment