Commit df44266e authored by alexpott's avatar alexpott

Issue #2363341 by tim.plunkett, donquixote, adci_contributor: Throw exception...

Issue #2363341 by tim.plunkett, donquixote, adci_contributor: Throw exception in Drupal::service() and friends, if container not initialized yet
parent 69e817c9
This diff is collapsed.
<?php
/**
* @file
* Contains \Drupal\Core\DependencyInjection\ContainerNotInitializedException.
*/
namespace Drupal\Core\DependencyInjection;
/**
* Exception thrown when a method is called that requires a container, but the
* container is not initialized yet.
*
* @see \Drupal
*/
class ContainerNotInitializedException extends \RuntimeException {
}
......@@ -1187,7 +1187,7 @@ private function prepareEnvironment() {
// Ensure there is no service container.
$this->container = NULL;
\Drupal::setContainer(NULL);
\Drupal::unsetContainer();
// Unset globals.
unset($GLOBALS['config_directories']);
......
......@@ -27,7 +27,7 @@ class KernelTestBaseTest extends KernelTestBase {
* {@inheritdoc}
*/
protected function setUp() {
$original_container = \Drupal::getContainer();
$original_container = $this->originalContainer;
parent::setUp();
$this->assertNotIdentical(\Drupal::getContainer(), $original_container, 'KernelTestBase test creates a new container.');
}
......
......@@ -97,7 +97,7 @@ class: Drupal\Core\Cache\MemoryBackendFactory
EOD;
file_put_contents($this->siteDirectory . '/testing.services.yml', $yaml);
$original_container = \Drupal::getContainer();
$original_container = $this->originalContainer;
parent::setUp();
$this->assertNotIdentical(\Drupal::getContainer(), $original_container, 'WebTestBase test creates a new container.');
// Create and log in an admin user.
......
......@@ -1030,7 +1030,7 @@ protected function installParameters() {
protected function getDatabaseTypes() {
\Drupal::setContainer($this->originalContainer);
$database_types = drupal_get_database_types();
\Drupal::setContainer(NULL);
\Drupal::unsetContainer();
return $database_types;
}
......
......@@ -31,7 +31,7 @@ protected function setUp() {
// Store the previous container.
$this->previousContainer = $this->container;
$this->container = NULL;
\Drupal::setContainer(NULL);
\Drupal::unsetContainer();
}
/**
......@@ -55,7 +55,7 @@ function testDrupalGetFilename() {
// Assert that this test is meaningful.
$this->assertNull($this->container);
$this->assertNull(\Drupal::getContainer());
$this->assertFalse(\Drupal::hasContainer());
// Retrieving the location of a module.
$this->assertIdentical(drupal_get_filename('module', 'system'), 'core/modules/system/system.info.yml');
......
......@@ -41,7 +41,7 @@ protected function setUp() {
protected function prepareConfigDirectories() {
\Drupal::setContainer($this->originalContainer);
parent::prepareConfigDirectories();
\Drupal::setContainer(NULL);
\Drupal::unsetContainer();
}
/**
......
......@@ -26,7 +26,11 @@ class DrupalTest extends UnitTestCase {
*/
protected $container;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
->setMethods(array('get'))
->getMock();
......@@ -42,6 +46,16 @@ public function testSetContainer() {
$this->assertSame($this->container, \Drupal::getContainer());
}
/**
* @covers ::getContainer
*
* @expectedException \Drupal\Core\DependencyInjection\ContainerNotInitializedException
* @expectedExceptionMessage \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.
*/
public function testGetContainerException() {
\Drupal::getContainer();
}
/**
* Tests the service() method.
*
......
......@@ -39,7 +39,7 @@ protected function setUp() {
parent::setUp();
// Ensure that an instantiated container in the global state of \Drupal from
// a previous test does not leak into this test.
\Drupal::setContainer(NULL);
\Drupal::unsetContainer();
$this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
}
......
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