Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#20 | instead_of_mocking-2533776-20.patch | 9.58 KB | lluvigne |
#20 | interdiff-2533776-17-20.txt | 8.9 KB | lluvigne |
#17 | instead_of_mocking-2533776-17.patch | 7.51 KB | lluvigne |
#17 | interdiff-2533776-14-17.txt | 7.06 KB | lluvigne |
#14 | replace-typeddatamanager-2533776-14.patch | 7.14 KB | dagmar |
Comments
Comment #1
dawehnerComment #3
dawehner,
Comment #4
nicolas.rafaelli CreditAttribution: nicolas.rafaelli at Globant commentedThis is my first patch, let me know if i made some mistake.
Comment #6
joshi.rohit100Check \Drupal\Tests\Core\Plugin\Context\ContextTest class as well
Comment #7
nicolas.rafaelli CreditAttribution: nicolas.rafaelli at Globant commentedThank you, @joshi.rohit100.
Comment #8
dagmarIt seems there are other places where the TypedDataManager is mocked.
Comment #9
nicolas.rafaelli CreditAttribution: nicolas.rafaelli at Globant commentedThanks, dagmar.
I added the others and i tested them.
Comment #10
dagmarLooks good.
$ egrep -unr "getMockBuilder.*TypedDataManager"
Comment #11
alexpottThis 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.Comment #12
dagmar@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?
Comment #13
alexpott@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.Comment #14
dagmarLet see if the tests pass with this.
Comment #15
alexpott@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().Comment #16
alexpottFor #15
Comment #17
lluvigneAccording to @alexpott let's use only $this->getMock(TypedDataManagerInterface::class); call. Tested locally and all seems to be ok. Hope it works.
Comment #18
dimaro CreditAttribution: dimaro at La Drupalera by Emergya commentedI review all ocurrences of TypedDataManagerInterface and look goods to me!
Comment #19
alexpottLet's change every place already touched by this patch from using the string
'Drupal\Core\TypedData\TypedDataManagerInterface'
to useTypedDataManagerInterface::class
. This is better because it makes any future refactor simpler.Comment #20
lluvigneOk, i changed all strings on the patch.
Comment #21
dawehnerNice!
Comment #22
alexpottCommitted 11d4d9f and pushed to 8.1.x and 8.2.x. Thanks!