commit d48c7c9a5bf9c69397800077c5e933ca5ddc8e2d Author: Pieter Frenssen Date: Fri Mar 28 23:50:17 2014 +0100 2190313-40 diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php index 33ceb08..e1a37a4 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php @@ -233,52 +233,62 @@ public function testLanguage() { } /** - * @covers ::load - * - * Tests Entity::load() when called statically on the Entity base class. + * Setup for the tests of the ::load() method. */ - public function testLoad() { + function setupTestLoad() { // Use an entity type object which has the methods enabled which are being // called by the protected method Entity::getEntityTypeFromStaticClass(). $methods = get_class_methods('Drupal\Core\Entity\EntityType'); unset($methods[array_search('getClass', $methods)]); unset($methods[array_search('isSubclassOf', $methods)]); unset($methods[array_search('setClass', $methods)]); - $entity_type = $this->getMockBuilder('\Drupal\Core\Entity\EntityType') + $this->entityType = $this->getMockBuilder('\Drupal\Core\Entity\EntityType') ->disableOriginalConstructor() ->setMethods($methods) ->getMock(); // Base our mocked entity on a real entity class so we can test if calling // Entity::load() on the base class will bubble up to an actual entity. - $entity_type_id = 'entity_test_mul'; + $this->entityTypeId = 'entity_test_mul'; $methods = get_class_methods('Drupal\entity_test\Entity\EntityTestMul'); unset($methods[array_search('load', $methods)]); - $test_entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTestMul') + unset($methods[array_search('loadMultiple', $methods)]); + $this->entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTestMul') ->disableOriginalConstructor() ->setMethods($methods) ->getMock(); - $test_entity->id = 1; - $entity_type->setClass(get_class($test_entity)); + $this->entity->id = 1; + $this->entityType->setClass(get_class($this->entity)); + + $this->entityManager->expects($this->once()) + ->method('getDefinitions') + ->will($this->returnValue(array($this->entityTypeId => $this->entityType))); + + $this->entityType->expects($this->any()) + ->method('id') + ->will($this->returnValue($this->entityTypeId)); + } + + /** + * @covers ::load + * + * Tests Entity::load() when called statically on the Entity base class. + */ + public function testLoad() { + $this->setupTestLoad(); $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); $storage->expects($this->once()) ->method('load') ->with(1) - ->will($this->returnValue($test_entity)); + ->will($this->returnValue($this->entity)); $this->entityManager->expects($this->once()) ->method('getStorage') - ->with($entity_type_id) + ->with($this->entityTypeId) ->will($this->returnValue($storage)); - $this->entityManager->expects($this->once()) - ->method('getDefinitions') - ->will($this->returnValue(array($entity_type_id => $entity_type))); - $entity_type->expects($this->any()) - ->method('id') - ->will($this->returnValue($entity_type_id)); // Call Entity::load statically and check that it returns the mock entity. - $this->assertSame($test_entity, Entity::load(1)); + $this->assertSame($this->entity, Entity::load(1)); } /** @@ -350,45 +360,22 @@ public function testLoadWithNoCorrespondingSubclasses() { * Tests Entity::load() when called statically on a subclass of Entity. */ public function testLoadSubClass() { - // Entity::load() is a wrapper for EntityStorage::load(). Test that the same - // mock entity object that is returned by load() on the storage controller - // will be returned by Entity::load(). - $entity_type_id = $this->randomName(); - $methods = get_class_methods('Drupal\Core\Entity\Entity'); - unset($methods[array_search('load', $methods)]); - $test_entity = $this->getMockBuilder('Drupal\Core\Entity\Entity') - ->disableOriginalConstructor() - ->setMethods($methods) - ->getMock(); - $test_entity->id = 1; + $this->setupTestLoad(); - // Mock the methods called by Entity::load() and - // Entity::getEntityTypeFromStaticClass(). $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); $storage->expects($this->once()) ->method('load') ->with(1) - ->will($this->returnValue($test_entity)); + ->will($this->returnValue($this->entity)); $this->entityManager->expects($this->once()) ->method('getStorage') - ->with($entity_type_id) + ->with($this->entityTypeId) ->will($this->returnValue($storage)); - $this->entityManager->expects($this->once()) - ->method('getDefinitions') - ->will($this->returnValue(array($entity_type_id => $this->entityType))); - $this->entityType->expects($this->once()) - ->method('getClass') - ->will($this->returnValue(get_class($test_entity))); - $this->entityType->expects($this->exactly(0)) - ->method('isSubClassOf'); - $this->entityType->expects($this->once()) - ->method('id') - ->will($this->returnValue($entity_type_id)); // Call Entity::load statically on the subclass and check that it returns // the mock entity. - $class = get_class($test_entity); - $this->assertSame($test_entity, $class::load(1)); + $class = get_class($this->entity); + $this->assertSame($this->entity, $class::load(1)); } /** @@ -398,47 +385,21 @@ public function testLoadSubClass() { * class. */ public function testLoadMultiple() { - // Entity::load() is a wrapper for EntityStorage::loadMultiple(). Test that - // the same mock entity object that is returned by loadMultiple() on the - // storage controller will be returned by Entity::load(). - $entity_type_id = $this->randomName(); - $methods = get_class_methods('Drupal\Core\Entity\Entity'); - unset($methods[array_search('loadMultiple', $methods)]); - $test_entity = $this->getMockBuilder('Drupal\Core\Entity\Entity') - ->disableOriginalConstructor() - ->setMethods($methods) - ->getMock(); - $test_entity->id = 1; + $this->setupTestLoad(); - // Mock the methods called by Entity::loadMultiple() and - // Entity::getEntityTypeFromStaticClass(). $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); $storage->expects($this->once()) ->method('loadMultiple') ->with(array(1)) - ->will($this->returnValue(array(1 => $test_entity))); + ->will($this->returnValue(array(1 => $this->entity))); $this->entityManager->expects($this->once()) ->method('getStorage') - ->with($entity_type_id) + ->with($this->entityTypeId) ->will($this->returnValue($storage)); - $this->entityManager->expects($this->once()) - ->method('getDefinitions') - ->will($this->returnValue(array($entity_type_id => $this->entityType))); - $this->entityType->expects($this->once()) - ->method('getClass') - ->will($this->returnValue(get_class($test_entity))); - $this->entityType->expects($this->once()) - ->method('isSubClassOf') - ->with('Drupal\Core\Entity\Entity') - ->will($this->returnValue(TRUE)); - $this->entityType->expects($this->any()) - ->method('id') - ->will($this->returnValue($entity_type_id)); // Call Entity::loadMultiple statically and check that it returns the mock // entity. - $class = get_class($test_entity); - $this->assertSame(array(1 => $test_entity), Entity::loadMultiple(array(1))); + $this->assertSame(array(1 => $this->entity), Entity::loadMultiple(array(1))); } /** @@ -448,46 +409,22 @@ public function testLoadMultiple() { * Entity. */ public function testLoadMultipleSubClass() { - // Entity::load() is a wrapper for EntityStorage::loadMultiple(). Test that - // the same mock entity object that is returned by loadMultiple() on the - // storage controller will be returned by Entity::load(). - $entity_type_id = $this->randomName(); - - $methods = get_class_methods('Drupal\Core\Entity\Entity'); - unset($methods[array_search('loadMultiple', $methods)]); - $test_entity = $this->getMockBuilder('Drupal\Core\Entity\Entity') - ->disableOriginalConstructor() - ->setMethods($methods) - ->getMock(); - $test_entity->id = 1; + $this->setupTestLoad(); - // Mock the methods called by Entity::loadMultiple() and - // Entity::getEntityTypeFromStaticClass(). $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); $storage->expects($this->once()) ->method('loadMultiple') ->with(array(1)) - ->will($this->returnValue(array(1 => $test_entity))); + ->will($this->returnValue(array(1 => $this->entity))); $this->entityManager->expects($this->once()) ->method('getStorage') - ->with($entity_type_id) + ->with($this->entityTypeId) ->will($this->returnValue($storage)); - $this->entityManager->expects($this->once()) - ->method('getDefinitions') - ->will($this->returnValue(array($entity_type_id => $this->entityType))); - $this->entityType->expects($this->once()) - ->method('getClass') - ->will($this->returnValue(get_class($test_entity))); - $this->entityType->expects($this->exactly(0)) - ->method('isSubClassOf'); - $this->entityType->expects($this->once()) - ->method('id') - ->will($this->returnValue($entity_type_id)); // Call Entity::loadMultiple statically and check that it returns the mock // entity. - $class = get_class($test_entity); - $this->assertSame(array(1 => $test_entity), $class::loadMultiple(array(1))); + $class = get_class($this->entity); + $this->assertSame(array(1 => $this->entity), $class::loadMultiple(array(1))); } /**