/**
  * @test
  */
 public function shouldExecuteSameRequestWithModelDetails()
 {
     $expectedDetails = new \stdClass();
     $modelMock = $this->getMock('Payum\\Core\\Model\\DetailsAggregateInterface');
     $modelMock->expects($this->atLeastOnce())->method('getDetails')->will($this->returnValue($expectedDetails));
     $request = new CaptureRequest($modelMock);
     // guard
     $this->assertInstanceOf('Payum\\Core\\Request\\ModelRequestInterface', $request);
     $testCase = $this;
     $paymentMock = $this->getMock('Payum\\Core\\PaymentInterface');
     $paymentMock->expects($this->once())->method('execute')->with($this->identicalTo($request))->will($this->returnCallback(function ($request) use($expectedDetails, $testCase) {
         $testCase->assertSame($expectedDetails, $request->getModel());
     }));
     $action = new ExecuteSameRequestWithModelDetailsAction();
     $action->setPayment($paymentMock);
     $action->execute($request);
 }
    /**
     * @test
     */
    public function shouldWrapArrayDetailsToArrayObjectAndSetDetailsBackEvenOnException()
    {
        $expectedDetails = array('foo' => 'fooVal', 'bar' => 'barVal');

        $model = new DetailsAggregateAndAwareModel();
        $model->details = $expectedDetails;

        $request = new ModelAggregateAwareRequest($model);

        $testCase = $this;

        $paymentMock = $this->getMock('Payum\Core\PaymentInterface');
        $paymentMock
            ->expects($this->once())
            ->method('execute')
            ->with($this->identicalTo($request))
            ->will($this->returnCallback(function ($request) use ($expectedDetails, $testCase) {
                $details = $request->getModel();

                $testCase->assertInstanceOf('ArrayAccess', $details);
                $testCase->assertSame($expectedDetails, iterator_to_array($details));

                $details['baz'] = 'bazVal';

                throw new \LogicException('The exception');
            }))
        ;

        $action = new ExecuteSameRequestWithModelDetailsAction();
        $action->setPayment($paymentMock);

        try {
            $action->execute($request);
        } catch (\LogicException $e) {
            $details = $model->getDetails();
            $testCase->assertInstanceOf('ArrayAccess', $details);
            $testCase->assertSame(
                array('foo' => 'fooVal', 'bar' => 'barVal', 'baz' => 'bazVal'),
                iterator_to_array($details)
            );

            return;
        }

        $this->fail('The exception is expected to be thrown');
    }