2

I've been following a tutorial on phpUnit but i can't seem to make it work.

here's the link:

https://jtreminio.com/2013/03/unit-testing-tutorial-part-4-mock-objects-stub-methods-dependency-injection/

There was 1 error:

1) phpUnitTutorial\Test\PaymentTest::testProcessPaymentReturnsTrueOnSuccess
ReflectionException: Class Mock_AuthorizeNetAIM_2d93f549 does not have a constructor, so     you cannot pass any constructor arguments

phpUnitTutorial/Payment.php

<?php

namespace phpUnitTutorial;

class Payment
{
    const API_ID = 123456;
    const TRANS_KEY = 'TRANSACTION KEY';

    //Use Dependency Injection
    public function processPayment(\AuthorizeNetAIM $transaction, array $paymentDetails)
    {
        $transaction->amount = $paymentDetails['amount'];
        $transaction->card_num = $paymentDetails['card_num'];
        $transaction->exp_date = $paymentDetails['exp_date'];

        $response = $transaction->authorizeAndCapture();

        if ($response->approved) {
            return $this->savePayment($response->transaction_id);
        }

        throw new \Exception($response->error_message);
    }

    public function savePayment($transactionId)
    {
        // Logic for saving transaction ID to database or anywhere else would go in here
        return true;
    }
}

phpUnitTutorial/Test/PaymentTest.php

<?php

namespace phpUnitTutorial\Test;

require_once __DIR__ . '/../Payment.php';
use phpUnitTutorial\Payment;

class PaymentTest extends \PHPUnit_Framework_TestCase
{

    public function testProcessPaymentReturnsTrueOnSuccess()
    {
        $paymentDetails = array(
            'amount' => 123.99,
            'card_num' => '4111-111-111-111',
            'exp_date' => '03/2013'
        );

        $payment = new Payment();

        // Note stdClass is used to conveniently create a generic
        // class
        $response = new \stdClass();
        $response->approved = true;
        $response->transaction_id = 123;

        $authorizeNet = $this->getMockBuilder('\AuthorizeNetAIM')
             ->setConstructorArgs(
                 array(
                     $payment::API_ID,
                     $payment::TRANS_KEY
                 )
             )->getMock();

        // $authorizeNet =  $this
        //     ->getMock(
        //         '\AuthorizeNetAIM',
        //         array(),
        //         array($payment::API_ID, $payment::TRANS_KEY)
        //     );

        $authorizeNet->expects($this->once())
            ->method('authorizeAndCapture')
            ->will($this->returnValue($response));

        $result = $payment->processPayment($authorizeNet, $paymentDetails);

        $this->assertTrue($result);
    }
}
2

1 Answer 1

1

\AuthorizeNetAIM has no constructor - as the error message states. So you can't pass any arguments to it. Create the mock in this way:

$authorizeNet = $this->getMockBuilder('\AuthorizeNetAIM')->getMock();

or shorter:

$authorizeNet = $this->getMock('\AuthorizeNetAIM');
Sign up to request clarification or add additional context in comments.

6 Comments

yeah, that's what i've been thinking but there seems to be a constructor since on the tutorial on my link, the author instantitiated it with 2 parameters. Here's the snippet from the tutorial. $transaction = new \AuthorizeNetAIM(self::API_ID, self::TRANS_KEY);
Could you point where in the documentation this snippet is? I can't find it - every example has building this object without any arguments.
i've followed the tutorial on this jtreminio.com/2013/03/…. You can immediately see it on the start of the tutorial.
sorry, but what documenation are you referrring to? is it on AuthorizeNetAim docs? if it is i haven't seen the docs yet. I've simply followed the tutorial as it is. I think i have to look up the the library's API itself.
I've reffered to the documentation from github. Maybe tutorial you've pointed is basing on an older version of this library or just has a mistake. Take a look on unit tests from this project. And pay attention that class AuthorizeNetRequest takes API_ID and TRANSACTION_KEY as its arguments.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.