omise / omise-php

Omise PHP library
https://docs.opn.ooo
MIT License
67 stars 41 forks source link

Omise Unit Tests Clashing With Laravel Unit Tests #110

Open ahirota opened 5 years ago

ahirota commented 5 years ago

Hey all,

To preface, Laravel has phpunit tests built into the framework, where users can write custom tests for various parts of their application. Ideally, I'd like to use my own custom tests with the Omise Test API, but due to the snippet below, any phpunit tests (including Laravel's) are executed with the executeTest() method and return data saved in the tests/fixtures directory.

// OmiseApiResource.php Lines:134-141
protected function execute($url, $requestMethod, $key, $params = null)
    {
        // If this class is execute by phpunit > get test mode.
        if (preg_match('/phpunit/', $_SERVER['SCRIPT_NAME'])) {
            $result = $this->_executeTest($url, $requestMethod, $key, $params);
        } else {
            $result = $this->_executeCurl($url, $requestMethod, $key, $params);
        }

My current workaround is deleting the contents of the fixtures folder before I run my tests, forcing executeTest to go and retrieve the data from the test API source. Is there any way to override this method? Or maybe a way to localize the preg_match to only the Omise Vendor files?

guzzilar commented 5 years ago

@ahirota Hi, this might not related to the solution that you are looking for, but would you mind to share a case where you want to connect to the real API server for a unit test script? Is there any specific reason to bypass the fixture files?

ahirota commented 5 years ago

@guzzilar To Preface, I've created a Repository to handle calls to the Omise API. My test suites are all designed to test basic functionality within my application and I'd need to connect to the real API server in order for my tests to pass.

One of my test suites generates a new customer with a customer token, tests creating charges, creating and destroying charge schedules, adding and removing cards from the customer, and then finally removing the customer all together. If I were to use the default fixture files, the act of creating a new customer with email: test@example.com and any other parameters, returns with the response saved in the fixture files.

Here's a code example:

  public function testCustomer()
    {
        // create
        $customer = $this->OmiseRepository->makeCustomer('sizuka@gmail.com', 333);
        self::assertEquals('sizuka@gmail.com', $customer->email);

        // delete
        $bool = $this->OmiseRepository->destroyCustomerById($customer->id);
        self::assertTrue($bool);
    }

In this test, both assertions fail because new customer data is never created. The response is always taken from the fixture files. This issue extends to customer tokens and beyond.

I guess my main issue is that I don't want to test the package itself, but rather the extensions and wrappers I've created, and using PHPunit calls _executeTest for any Omise API related functions.