Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when collaborator has a final construct #54

Closed
raul782 opened this issue Sep 14, 2013 · 4 comments
Closed

Error when collaborator has a final construct #54

raul782 opened this issue Sep 14, 2013 · 4 comments

Comments

@raul782
Copy link

raul782 commented Sep 14, 2013

If I pass a collaborator that has a final construct to a method I'm specking, phpspec gives me an obvious error.

Is there a workaround about this, by the way that collaborator is an instance of SimpleXMLElement, but it applies to any class that has the same structure.

class Collaborator
{
    final public __construct($data)
    {
        ..... do something ....
    }
}

class Worker 
{
    public function doWork($collaborator)
    {
        return $collaborator->otherMethod();
    }
}

class WorkerSpec
{
    /**
    *  @param Collaborator $collaborator
    */
    function it_should_return_something($collaborator)
    {
        $data = "data";
        $this->doWork($collaborator)->willReturn($data);
    }
}

This leads to this error

 PHP Fatal error:  Cannot override final method Collaborator::__construct()
@ciaranmcnulty
Copy link
Member

It might be worth treating SimpleXmlElements as value objects and not mocking them.

@ciaranmcnulty
Copy link
Member

Actually this might be possible to fix for PHP classes with final constructors, but not for internals like SimpleXmlElement.

@stof
Copy link
Member

stof commented Oct 18, 2013

This is actually possible when the constructor is public. See this example taken from #61 (SQLFilter has a private constructor):

class SomeRepositorySpec extends ObjectBehavior
{
   function let(
      EntityManager $manager,
      ClassMetadata $metadata,
      FilterCollection $filters,
      SoftDeleteableFilter $softDeleteableFilter
   ) {
        // Here, you can specify the constructor argument
        // It must be done before $softDeleteableFilter is unwrapped, so the 
        // beginning of let() is the best location
        $softDeleteableFilter->beConstructedWith([$manager->getWrappedObject()]);


        $manager->getFilters()->willReturn($filters);
        $filters->enable('softdeletable')->willReturn($softDeleteableFilter);

        $this->beConstructedWith($manager, $metadata);
   }
}

Having to unwrap the collaborators is unfortunate and I would consider it as a Prophecy bug

@stof
Copy link
Member

stof commented Oct 18, 2013

and for the fatal error, it is solved by the PR above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants