Problem/Motivation

Before #2488568: Add a TypedDataManagerInterface and use it for typed parameters, it was not possible to create a test class that implements the interface but instead creation of a mock based on TypedDataManager itself was required. After that, its possible to simplify tests around this.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Component: base system » phpunit
Status: Postponed » Active

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

Issue tags: +Novice

,

nicolas.rafaelli’s picture

Version: 8.1.x-dev » 8.2.x-dev
Status: Active » Needs review
FileSize
2.2 KB

This is my first patch, let me know if i made some mistake.

Status: Needs review » Needs work

The last submitted patch, 4: replace-typeddatamanager-2533776-4.patch, failed testing.

joshi.rohit100’s picture

Check \Drupal\Tests\Core\Plugin\Context\ContextTest class as well

nicolas.rafaelli’s picture

Status: Needs work » Needs review
FileSize
2.2 KB

Thank you, @joshi.rohit100.

dagmar’s picture

Status: Needs review » Needs work

It seems there are other places where the TypedDataManager is mocked.

  • core/tests/Drupal/Tests/Core/Plugin/Context/ContextTypedDataTest.php Line:37
  • core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php Line:64
  • core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php Line:130
  • core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php Line:49
  • core/tests/Drupal/Tests/Core/Field/BaseFieldDefinitionTestBase.php Line:38
  • core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php Line:133
  • core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php Line:139
  • core/modules/views/tests/src/Unit/EntityViewsDataTest.php Line:86
  • core/modules/serialization/tests/src/Unit/Normalizer/ListNormalizerTest.php Line:40
nicolas.rafaelli’s picture

Status: Needs work » Needs review
FileSize
6.97 KB

Thanks, dagmar.
I added the others and i tested them.

dagmar’s picture

Status: Needs review » Reviewed & tested by the community

Looks good.

$ egrep -unr "getMockBuilder.*TypedDataManager"

./core/tests/Drupal/Tests/Core/Plugin/Context/ContextTypedDataTest.php:37:    $typed_data_manager = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManagerInterface')
./core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php:64:    $mock_data_manager = $this->getMockBuilder('\Drupal\Core\TypedData\TypedDataManagerInterface')
./core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php:129:    $mock_data_manager = $this->getMockBuilder('\Drupal\Core\TypedData\TypedDataManagerInterface')
./core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php:49:    $this->typedDataManager = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManagerInterface')
./core/tests/Drupal/Tests/Core/Field/BaseFieldDefinitionTestBase.php:38:    $typed_data_manager = $this->getMockBuilder('\Drupal\Core\TypedData\TypedDataManagerInterface')
./core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php:133:    $this->typedDataManager = $this->getMockBuilder('\Drupal\Core\TypedData\TypedDataManagerInterface')
./core/tests/Drupal/Tests/Core/Entity/TypedData/EntityAdapterUnitTest.php:139:    $this->typedDataManager = $this->getMockBuilder('\Drupal\Core\TypedData\TypedDataManagerInterface')
./core/modules/views/tests/src/Unit/EntityViewsDataTest.php:86:    $typed_data_manager = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManagerInterface')
./core/modules/serialization/tests/src/Unit/Normalizer/ListNormalizerTest.php:40:    $typed_data_manager = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManagerInterface')
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/serialization/tests/src/Unit/Normalizer/ListNormalizerTest.php
@@ -37,9 +37,8 @@ class ListNormalizerTest extends UnitTestCase {
+    $typed_data_manager = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManagerInterface')
       ->disableOriginalConstructor()
-      ->setMethods(array('getPropertyInstance'))
       ->getMock();

This can be shorten to $typed_data_manager = $this->getMock(TypedDataManagerInterface::class);
You might need to add a use statement in a few places but ::class is preferred to strings of class names.

dagmar’s picture

Status: Needs work » Reviewed & tested by the community

@alexpott sounds good, but if we do that all the tests cases will have a mix of ClassName::class and Drupal\namespace\ClassName.

What do you think about committing this issue as is and creating a new META issue to replace all the occurrences of this Drupal\namespace\ClassName to ClassName::class in tests that creates mock objects?

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

@dagmar I think the sting vs ::class inconsistency is ok. However using getMockBuilder()->disableOriginalConstructor()->getMock() on an interface is not. There's no constructor on an interface.

dagmar’s picture

Status: Needs work » Needs review
FileSize
5.93 KB
7.14 KB

Let see if the tests pass with this.

alexpott’s picture

@dagmar they are unit tests - you can run them before submitting the patch :) also just $this->getMock(TypedDataManagerInterface::class); is enough - there is no need to call $this->getMockBuilder().

alexpott’s picture

Status: Needs review » Needs work

For #15

lluvigne’s picture

Status: Needs work » Needs review
FileSize
7.06 KB
7.51 KB

According to @alexpott let's use only $this->getMock(TypedDataManagerInterface::class); call. Tested locally and all seems to be ok. Hope it works.

dimaro’s picture

Status: Needs review » Reviewed & tested by the community

I review all ocurrences of TypedDataManagerInterface and look goods to me!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/serialization/tests/src/Unit/Normalizer/ListNormalizerTest.php
@@ -37,10 +37,7 @@ class ListNormalizerTest extends UnitTestCase {
+    $typed_data_manager = $this->getMock('Drupal\Core\TypedData\TypedDataManagerInterface');
...
diff --git a/core/modules/views/tests/src/Unit/EntityViewsDataTest.php b/core/modules/views/tests/src/Unit/EntityViewsDataTest.php

Let's change every place already touched by this patch from using the string 'Drupal\Core\TypedData\TypedDataManagerInterface' to use TypedDataManagerInterface::class. This is better because it makes any future refactor simpler.

lluvigne’s picture

Status: Needs work » Needs review
FileSize
8.9 KB
9.58 KB

Ok, i changed all strings on the patch.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Nice!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 11d4d9f and pushed to 8.1.x and 8.2.x. Thanks!

  • alexpott committed 9548184 on 8.2.x
    Issue #2533776 by nicolas.rafaelli, lluvigne, dagmar: Instead of mocking...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.