Add some tests#97
Add some tests#97cristianoc72 wants to merge 2 commits intokunalvarma05:masterfrom cristianoc72:test-suite
Conversation
tests/DropboxTest.php
Outdated
| $this->assertEquals('id:a4ayc_80_OEAAAAAAAAAXw', $metadata->getId()); | ||
| } | ||
|
|
||
| public function testGettMetadataOnFolder() |
There was a problem hiding this comment.
There's a tiny typo: testGettMetadataOnFolder must be testGetMetadataOnFolder.
tests/DropboxTest.php
Outdated
| public function testDemo() | ||
| public function testGetMetadataOnFile() | ||
| { | ||
| $responseBody = <<<JSON |
There was a problem hiding this comment.
We can delegate the creation of dummy JSON responses for the request to a separate class(es). Thoughts?
There was a problem hiding this comment.
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();
..............................
}
}
tests/DropboxTest.php
Outdated
| /** @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'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Interesting. Any reason why this is considered a best practice?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
Add some tests for Dropbox class.
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 intests/DropboxTest.php.By now, I'm submitting this small PR, testing only
Dropbox::getMetadatamethod. If you like this approach, I can go on with all the other tests.