Problem/Motivation

@dawehner pointed out that PHPUnit now ships with phpspec/prophecy, a mocking framework.
I wanted to try it out, figured page_manager would be a good candidate.

Proposed resolution

Use phpspec/prophecy for mocks

Remaining tasks

Decide if this is actually an improvement
Figure out how to replace getMockBuilder calls

User interface changes

N/A

API changes

N/A

Data model changes

N/A

CommentFileSizeAuthor
#1 2538794-prophecy-1.patch42.67 KBtim.plunkett
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

Status: Active » Needs review
FileSize
42.67 KB

I may be using this completely wrong, couldn't find too many examples to go on.

jibran’s picture

WoW this looks awesome.

Berdir’s picture

+++ b/tests/src/Unit/BlockDisplayVariantTest.php
@@ -123,40 +98,34 @@ public function testBuildNoCache() {
     $display_variant = $this->getMockBuilder(BlockDisplayVariant::class)
-      ->setConstructorArgs([['page_title' => $page_title], 'test', array(), $context_handler, $account, $uuid_generator, $token])
-      ->setMethods(array('getBlockCollection', 'drupalHtmlClass', 'renderPageTitle'))
+      ->setConstructorArgs([['page_title' => $page_title], 'test', [], $context_handler->reveal(), $account->reveal(), $uuid_generator->reveal(), $token])
+      ->setMethods(['getBlockCollection', 'drupalHtmlClass', 'renderPageTitle'])
       ->getMock();
...
     $display_variant->expects($this->once())
       ->method('getBlockCollection')
-      ->will($this->returnValue($block_collection));
+      ->willReturn($block_collection);
     $display_variant->expects($this->once())
       ->method('renderPageTitle')
       ->with($page_title)
-      ->will($this->returnValue($page_title));
+      ->willReturn($page_title);

prophesize can't handle partial mocks?

dawehner’s picture

They don't want to support it by design, see https://github.com/phpspec/prophecy/issues/101.

In an ideal world phpunit would have used mockery.

Fabianx’s picture

#3 #4: Yeah, sadly not.

Mockery shines when using partial or even Mocks for classes that do not exist, e.g. interfaces that are not defined.

However Prophecy is really great in overwriting the behavior of a defined mock per test class and also the dynamic binding of allowing a setter to overwrite the expectation of a getter is really great.

http://everzet.com/post/72910908762/conceptual-difference-between-mocker... is an insightful post on that.

tim.plunkett’s picture

Right. So the question here is, do we want to mix mocking libraries within a test (or project)?

dawehner’s picture

Within a project I would say no problem at all. Within a test, mh, depends how much it improves readability.

Berdir’s picture

And this improves it a lot.

I'd say we should just commit it, want to continue working on #2528370: Simplify block caching and allow to cache the whole page, including access checks but kind of waiting for this to land as it will conflict a lot with the unit test changes that I will have to do there.

dawehner’s picture

Too bad that tim is on vacation.

  • tim.plunkett committed 1a09c53 on 8.x-1.x
    Issue #2538794 by tim.plunkett: Use phpspec/prophecy for mocks.
    
tim.plunkett’s picture

Status: Needs review » Fixed

Committed, should have done this before I left, sorry!

Berdir’s picture

Wow, is that week already over? :)

Thanks, no problem, didn't have time anyway to work on it :)

Status: Fixed » Closed (fixed)

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