0

I have class which mostly to this

$newItem = new SeriesDiscountDecorator($item);
$cart->remove($item);
$cart->add($newItem);

And my spec looks like

$decoratedItem1->beConstructedWith([$item1]);
$cart->add($decoratedItem1)->shouldBeCalled();

But PHPSpec is saying that no right $cart->add call was made because of wrong parameter. In fact my $decoratedItem was not the same object that Cart class made. How to write spec like this right?

PHPSpec returns:

method call:
    - add(Domain\Discount\Decorator\SeriesDiscountDecorator:000000006b0917e60000000063743658 Object (
        'decoratedItem' => Double\ItemInterface\P66:000000006b0917a60000000063743658 Object (
        'objectProphecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)
          )
      ))
    on Double\Domain\Cart\Cart\P65 was not expected, expected calls were:
        - add(exact(Double\Domain\Discount\Decorator\SeriesDiscountDecorator\P69:000000006b0917f90000000063743658 Object (
        'objectProphecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)
              'decoratedItem' => PhpSpec\Wrapper\Collaborator:000000006b0917b30000000063743658 Object (
        'prophecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)
              )
        )))
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 1
    Could you paste the whole class you're speccing? Is there any return value you could inspect? – Jakub Zalas Jan 08 '16 at 09:02
  • You cannot mock a class creation unless you use a factory. But as Jakub suggested, paste the whole class as maybe you can test it by looking at the values of a returned object. – gvf Jan 08 '16 at 09:03

2 Answers2

0
$cart->add(SeriesDiscountDecorator::class)->shouldBeCalled();

seems the only way to test it, as long as you're instantiating the object inside the class you're spec-ing and you're not returning it.

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
0

This is incredibly old but using phpspec 2.5.8 .. I was able to accomplish this w/ the getWrappedObject() method on the collaborator. So the spec will look like this:

$decoratedItem1->beConstructedWith([$item1]);
$cart->add($decoratedItem1->getWrappedObject())->shouldBeCalled();
Dharman
  • 30,962
  • 25
  • 85
  • 135
user1760150
  • 624
  • 1
  • 5
  • 10