-
Notifications
You must be signed in to change notification settings - Fork 179
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
base: master
Are you sure you want to change the base?
Add some tests #97
Conversation
tests/DropboxTest.php
Outdated
$this->assertEquals('id:a4ayc_80_OEAAAAAAAAAXw', $metadata->getId()); | ||
} | ||
|
||
public function testGettMetadataOnFolder() |
There was a problem hiding this comment.
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
.
tests/DropboxTest.php
Outdated
public function testDemo() | ||
public function testGetMetadataOnFile() | ||
{ | ||
$responseBody = <<<JSON |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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();
..............................
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat. This makes sense 👌
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.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
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::getMetadata
method. If you like this approach, I can go on with all the other tests.