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

Add some tests #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add some tests #97

wants to merge 2 commits into from

Conversation

cristianoc72
Copy link

Starting to use this awesome library, I decided to contribute writing some tests.

Thanks to Guzzle mock system we can easily test our library in isolation, without real calls to the Dropbox api.
I included the needed methods in src/TestCase.php. Each test class should extend it, to have the mock system already initialized. In this way, we can test if our library send correct requests and if correctly manages the responses, as you can see in tests/DropboxTest.php.

By now, I'm submitting this small PR, testing only Dropbox::getMetadata method. If you like this approach, I can go on with all the other tests.

$this->assertEquals('id:a4ayc_80_OEAAAAAAAAAXw', $metadata->getId());
}

public function testGettMetadataOnFolder()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a tiny typo: testGettMetadataOnFolder must be testGetMetadataOnFolder.

public function testDemo()
public function testGetMetadataOnFile()
{
$responseBody = <<<JSON
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can delegate the creation of dummy JSON responses for the request to a separate class(es). Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, test cases could be clearer. Maybe, we could have some traits, to do something like this:

namespace Util 
{
    trait ResponsesForDropbox
    {
        public function getResponseForBlaBlaTest()
        {
             return new Response(......);
        }
    }
}

class DropboxTest extends TestCase
{
    use Util\ResponsesForDropbox;

    public function blaBlaTest()
    {
        $response = $this->getResponseForBlaBlaTest();
        ..............................
    }
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat. This makes sense 👌

/** @var Request $request */
$request = $this->getHistory()[0]['request'];
$this->assertEquals('POST', $request->getMethod(), 'Submit Dropbox call via POST method');
$this->assertEquals('https://api.dropboxapi.com/2/files/get_metadata', $request->getUri(), 'Correct uri');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost all the API methods that we're gonna test, will require us to assert the correctness of the URI for that endpoint. Can we delegate this to a separate trait/method, so that we only have to pass the endpoint (/files/get_metadata in this case)?

Also, the getBasePath() on the DropboxClient class's object can be used instead of https://api.dropboxapi.com/2 for RPC endpoints (like getMetadata()) and getContentPath() for Content-Upload/Download endpoints (like download()).

Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! We can write a method (into TestCase class) to test the request in one single call, i.e.:

public function testDropbox()
{
    ...........
    $this->testRequest('/url/to/test');
}

About the use of getBasePath() and getContenPath() functions , I'm not sure about it: hardcoding urls in such tests is considered a best practice.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Any reason why this is considered a best practice?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I've read more carefully some documents about testing and, I'm sorry, but it was my fault: harcoding urls is not ALWAYS a best practice. It is in those cases where an error in generating paths can propagate through the code, i.e. when I'm working on the route system of a framework.
In our case we can use the functions you suggested.
I'll fix it this evening.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem.

It is in those cases where an error in generating paths can propagate through the code, i.e. when I'm working on the route system of a framework.

FWIW, I learnt something new 👆 apparently :)

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

Successfully merging this pull request may close these issues.

None yet

2 participants