Commit 673f5b79 authored by alexpott's avatar alexpott

Issue #2445497 by Mile23, dawehner: Decouple ContainerBuilderTest from Symfony's tests

parent e82c6f2a
......@@ -9,10 +9,9 @@
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\Core\DependencyInjection\Fixture\BazClass;
use Drupal\Tests\Core\DependencyInjection\Fixture\BarClass;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass;
require_once __DIR__ . '../../../../../../vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php';
/**
* @coversDefaultClass \Drupal\Core\DependencyInjection\ContainerBuilder
......@@ -22,40 +21,38 @@ class ContainerBuilderTest extends UnitTestCase {
/**
* Tests set with a synchronized service.
*
* @covers ::set
*/
public function testSetOnSynchronizedService() {
$container = new ContainerBuilder();
$container->register('baz', 'BazClass')
$container->register('baz', '\Drupal\Tests\Core\DependencyInjection\Fixture\BazClass')
->setSynchronized(TRUE);
$container->register('bar', 'BarClass')
$container->register('bar', '\Drupal\Tests\Core\DependencyInjection\Fixture\BarClass')
->addMethodCall('setBaz', array(new Reference('baz')));
// Ensure that we can set services on a compiled container.
$container->compile();
$container->set('baz', $baz = new \BazClass());
$container->set('baz', $baz = new BazClass());
$this->assertSame($baz, $container->get('bar')->getBaz());
$container->set('baz', $baz = new \BazClass());
$container->set('baz', $baz = new BazClass());
$this->assertSame($baz, $container->get('bar')->getBaz());
}
/**
* Tests the get method.
*
* @see \Drupal\Core\DependencyInjection\Container::get()
* @covers ::get
*/
public function testGet() {
$container = new ContainerBuilder();
$container->register('bar', 'BarClass');
$container->register('bar', 'Drupal\Tests\Core\DependencyInjection\Fixture\BarClass');
$result = $container->get('bar');
$this->assertTrue($result instanceof \BarClass);
$this->assertTrue($result instanceof BarClass);
}
/**
* Tests the set() method.
*
* @covers ::set
*/
public function testSet() {
......
......@@ -9,7 +9,7 @@
use Drupal\Core\DependencyInjection\Container;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass;
use Drupal\Tests\Core\DependencyInjection\Fixture\BarClass;
/**
* @coversDefaultClass \Drupal\Core\DependencyInjection\Container
......@@ -17,37 +17,24 @@
*/
class ContainerTest extends UnitTestCase {
/**
* The tested container.
*
* @var \Drupal\Core\DependencyInjection\Container
*/
public $container;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->container = new Container();
}
/**
* Tests serialization.
*
* @expectedException \PHPUnit_Framework_Error
*/
public function testSerialize() {
serialize($this->container);
$container = new Container();
serialize($container);
}
/**
* Tests the set() method.
*
* @covers ::set
*/
public function testSet() {
$container = new Container();
$class = new BarClass();
$this->container->set('bar', $class);
$container->set('bar', $class);
// Ensure that _serviceId is set on the object.
$this->assertEquals('bar', $class->_serviceId);
}
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\DependencyInjection\Fixture\BarClass.
*/
namespace Drupal\Tests\Core\DependencyInjection\Fixture;
/**
* Stub class which acts as a service to test the container.
*
* @see \Drupal\Tests\Core\DependencyInjection\ContainerBuilderTest
* @see \Drupal\Tests\Core\DependencyInjection\Fixture\BazClass
*/
class BarClass {
/**
* Storage for a protected BazClass object.
*
* @var Drupal\Tests\Core\DependencyInjection\Fixture\BazClass
*/
protected $baz;
/**
* Setter for our BazClass object.
*
* This method is called during the service initialization.
*
* @param \Drupal\Tests\Core\DependencyInjection\Fixture\BazClass $baz
* A BazClass object to store.
*/
public function setBaz(BazClass $baz) {
$this->baz = $baz;
}
/**
* Getter for our BazClass object.
*
* @return \Drupal\Tests\Core\DependencyInjection\Fixture\BazClass
* The stored BazClass object.
*/
public function getBaz() {
return $this->baz;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Core\DependencyInjection\Fixture\BazClass.
*/
namespace Drupal\Tests\Core\DependencyInjection\Fixture;
/**
* Stub class which acts as a service dependency, to test the container.
*
* @see \Drupal\Tests\Core\DependencyInjection\ContainerBuilderTest
* @see \Drupal\Tests\Core\DependencyInjection\Fixture\BarClass
*/
class BazClass {
}
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