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

Omise Unit Tests Clashing With Laravel Unit Tests #110

Open
ahirota opened this issue Feb 27, 2019 · 2 comments
Open

Omise Unit Tests Clashing With Laravel Unit Tests #110

ahirota opened this issue Feb 27, 2019 · 2 comments

Comments

@ahirota
Copy link

ahirota commented Feb 27, 2019

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
Copy link
Contributor

guzzilar commented Feb 27, 2019

@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
Copy link
Author

ahirota commented Mar 4, 2019

@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: [email protected] 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('[email protected]', 333);
        self::assertEquals('[email protected]', $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.

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

2 participants