Commit c04b1d23 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch
Browse files

Remove unneeded craft

parent 6298ac04
......@@ -75,10 +75,8 @@ class TypedEntityUnitTestCase extends \DrupalUnitTestCase {
// When creating the EMW the entity in the fixture will be used regardless
// of the passed in entity.
xautoload()
->getServiceContainer()
->set('entity_wrapper_fixture_path', __DIR__ . '/fixtures/article.inc');
$wrapper_service = new MockEntityWrapperService();
$wrapper_service->setFixturePath(__DIR__ . '/fixtures/article.inc');
xautoload()
->getServiceContainer()
->set('entity_wrapper', $wrapper_service);
......@@ -101,23 +99,6 @@ class TypedEntityUnitTestCase extends \DrupalUnitTestCase {
$this->assertEqual($entity->{$random_name}, $random_value);
}
/**
* Helper function to set up the mocked Drupal instance.
*/
protected function setUpMockDrupalSystem() {
// Set up the system with the following modules enabled:
// - system
// - xautoload
// - typed_entity
// - typed_entity_example
$example_modules = new TypedEntityModules();
$components = new DrupalComponentContainer($example_modules);
$system = new MockDrupalSystem($components);
xautoload()
->getServiceContainer()
->set('system', $system);
}
/**
* Loads a serialized object from a file.
*
......
......@@ -9,7 +9,9 @@ namespace Drupal\typed_entity\TypedEntity\Tests;
use Drupal\typed_entity\Exception\TypedEntityException;
class MockEntityDrupalWrapper implements MockEntityDrupalWrapperInterface {
// @TODO: Fix this. If we use EntityDrupalWrapperInterface then we need Drupal's registry, therefore the DB, …
// class MockEntityDrupalWrapper implements \EntityDrupalWrapperInterface {
class MockEntityDrupalWrapper {
/**
* Entity type.
......@@ -46,37 +48,13 @@ class MockEntityDrupalWrapper implements MockEntityDrupalWrapperInterface {
$this->entityType = $type;
// When using this class for unit testing, set the fixture class in the
// service container.
$fixture_path = xautoload()
/** @var MockEntityWrapperServiceInterface $mock_service */
$mock_service = xautoload()
->getServiceContainer()
->get('entity_wrapper_fixture_path');
if (empty($fixture_path)) {
throw new TypedEntityException('You need to set the fixture path in the service container to mock an entity.');
}
$this->loadFixture($fixture_path);
->get('entity_wrapper');
$this->initFixture($mock_service->getFixture());
}
/**
* Load a fixture from a file with a serialized entity.
*
* @param string|array $fixture
* The fixture array or the path of a file containing a serialized fixture.
*
* @throws TypedEntityException
*/
public function loadFixture($fixture) {
if (!is_array($fixture)) {
$fixture_path = $fixture;
$fixture = NULL;
if (!file_exists($fixture_path)) {
throw new TypedEntityException('The provided fixture file does not exist.');
}
require $fixture_path;
if (empty($fixture)) {
throw new TypedEntityException('The contents of the fixture is not valid.');
}
}
$this->initFixture($fixture);
}
/**
* Generates a fixture for easier mocking.
......
<?php
/**
* @file
* Contains \Drupal\typed_entity\TypedEntity\Tests\MockEntityDrupalWrapperInterface.
*/
namespace Drupal\typed_entity\TypedEntity\Tests;
use Drupal\typed_entity\Exception\TypedEntityException;
// @TODO: Fix this. If we use EntityDrupalWrapperInterface then we need Drupal's registry, therefore the DB, …
//interface MockEntityDrupalWrapperInterface extends \EntityDrupalWrapperInterface {
interface MockEntityDrupalWrapperInterface {
/**
* Load a fixture from a file with a serialized entity.
*
* @param string $fixture_path
* The path to the file containing the serialized version of the entity. Or
* the object itself.
*
* @throws TypedEntityException
*/
public function loadFixture($fixture_path);
}
......@@ -7,9 +7,23 @@
namespace Drupal\typed_entity\TypedEntity\Tests;
use Drupal\typed_entity\Entity\EntityWrapperServiceInterface;
use Drupal\typed_entity\Exception\TypedEntityException;
class MockEntityWrapperService implements EntityWrapperServiceInterface {
class MockEntityWrapperService implements MockEntityWrapperServiceInterface {
/**
* Fixture array.
*
* @var array
*/
protected $fixture;
/**
* Fixture path.
*
* @var string
*/
protected $fixturePath;
/**
* {@inheritdoc}
......@@ -18,4 +32,47 @@ class MockEntityWrapperService implements EntityWrapperServiceInterface {
return new MockEntityDrupalWrapper($entity_type, $entity);
}
/**
* {@inheritdoc}
*/
public function getFixture() {
if ($this->fixture) {
return $this->fixture;
}
$this->fixture = $this->loadFixture($this->fixturePath);
return $this->fixture;
}
/**
* {@inheritdoc}
*/
public function setFixturePath($fixturePath) {
$this->fixturePath = $fixturePath;
}
/**
* Load a fixture from a file with a serialized entity.
*
* @param string $fixture_path
* The path to a file containing a serialized fixture.
*
* @return array
* The loaded fixture.
*
* @throws TypedEntityException
*/
protected function loadFixture($fixture_path) {
$fixture = NULL;
if (!file_exists($fixture_path)) {
throw new TypedEntityException('The provided fixture file does not exist.');
}
require $fixture_path;
if (empty($fixture) || !is_array($fixture)) {
throw new TypedEntityException('The contents of the fixture is not valid.');
}
return $fixture;
}
}
<?php
/**
* @file
* Contains \Drupal\typed_entity\TypedEntity\Tests\EntityWrapperServiceInterface.
*/
namespace Drupal\typed_entity\TypedEntity\Tests;
use Drupal\typed_entity\Entity\EntityWrapperServiceInterface;
interface MockEntityWrapperServiceInterface extends EntityWrapperServiceInterface {
/**
* Get the loaded fixture.
*
* @return array
*/
public function getFixture();
/**
* Sets the fixture path.
*
* @param string $fixturePath
*/
public function setFixturePath($fixturePath);
}
<?php
/**
* @file
* Contains \Drupal\typed_entity\TypedEntity\Tests\TypedEntityModules.
*/
namespace Drupal\typed_entity\TypedEntity\Tests;
use Drupal\xautoload\Tests\Example\AbstractExampleModules;
class TypedEntityModules extends AbstractExampleModules {
/**
* @return string[]
*/
public function getAvailableExtensions() {
return array_fill_keys(array(
'system',
'xautoload',
'entity',
'typed_entity',
'typed_entity_example',
), 'module');
}
/**
* @return string[]
*/
public function getExampleClasses() {
return array(
'typed_entity' => array(
'\\Drupal\\typed_entity\\TypedEntity\\TypedEntity',
'\\Drupal\\typed_entity\\TypedEntity\\TypedEntityInterface',
'\\Drupal\\typed_entity\\TypedEntity\\TypedEntityManager',
'\\Drupal\\typed_entity\\TypedEntity\\TypedEntityManagerInterface',
'\\Drupal\\typed_entity\\TypedEntity\\Tests\\MockEntityDrupalWrapper',
'\\Drupal\\typed_entity\\TypedEntity\\Tests\\MockEntityDrupalWrapperInterface',
'\\Drupal\\typed_entity\\TypedEntity\\Tests\\TypedEntityModules',
'\\Drupal\\typed_entity\\Tests\\TypedEntityUnitTestCase',
),
'typed_entity_example' => array(
'\\Drupal\\typed_entity_example\\TypedEntity\\TypedNode',
'\\Drupal\\typed_entity_example\\TypedEntity\\TypedNodeInterface',
'\\Drupal\\typed_entity_example\\TypedEntity\\Node\\Article',
'\\Drupal\\typed_entity_example\\TypedEntity\\Node\\ArticleInterface',
'\\Drupal\\typed_entity_example\\TypedEntity\\Tests\\TypedNodeArticleUnitTest',
'\\Drupal\\typed_entity_example\\Tests\\TypedEntityExampleUnitTestCase',
'\\Drupal\\typed_entity_example\\Tests\\TypedEntityExampleWebTestCase',
),
'entity' => array(
'\\EntityDrupalWrapperInterface',
),
);
}
/**
* Replicates drupal_parse_info_file(dirname($module->uri) . '/' . $module->name . '.info')
*
* @see drupal_parse_info_file()
*
* @param string $name
*
* @return array
* Parsed info file contents.
*/
public function drupalParseInfoFile($name) {
$info = array('core' => '7.x');
if (0 === strpos($name, 'typed_entity_example')) {
$info['dependencies'][] = 'xautoload';
$info['dependencies'][] = 'entity';
$info['dependencies'][] = 'typed_entity';
}
elseif (0 === strpos($name, 'typed_entity')) {
$info['dependencies'][] = 'xautoload';
$info['dependencies'][] = 'entity';
}
return $info;
}
}
Supports Markdown
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