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

[API] Initial API tests #3701

Merged
merged 4 commits into from
Dec 28, 2015
Merged

Conversation

lchrusciel
Copy link
Member

Q A
Bug fix? yes
New feature? yes
BC breaks? no
Deprecations? no
Fixed tickets #3468
License MIT
Doc PR #371

I want to introduce initial tests for Sylius API. The previous ApiTestCase was removed from Sylius and after small refactoring and a few improvements we prepared a stand-alone library Lakion ApiTestCase. I added this library to composer and wrote some sample test for Shipping Categories. I want to test all available endpoints so feel free to help me with this task.

Update:
NOTE: Because of depending on PHP to string, which has a class String the tests will fail on PHP7. We are waitng to merge PR which will resolve this issue.

Update2:
NOTE: PHP to string was fixed and it supports PHP7. But because we are still waiting for the official release temporary I added "minimum-stability": "dev" to composer. Do not merge it yet.

Update3:
NOTE: As far as we are good with depending on PHP-Matcher dev explicitly its ready for merge. At the end, ApiTestCase doesn't have a stable version to.

/**
* @author Łukasz Chruściel <lukasz.chrusciel@lakion.com>
*/
class ShippingCategoryControllerTest extends JsonApiTestCase
Copy link
Member

Choose a reason for hiding this comment

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

I am wondering if we should name tests like this. The ShippingCategoryController class does not exist and people are used to the fact that there is usually Xyz class and XyzTest. Something that sounds less like a class will be less confusing I hope.

ShippingCategoryApiTest vs ShippingCategoryControllerTest

WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm for ShippingCategoryApiTest to minimalize confusion.

Copy link
Member Author

Choose a reason for hiding this comment

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

ShippingCategoryApiTest is fine for me.

@pjedrzejewski
Copy link
Member

Nice work Łukasz! Let's hear more comments.

$this->assertResponse($response, 'shipping_category/create_validation_fail', 400);
}

public function testCreateShippingCategoryResponse()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we should suffix all methods with Response. Isn't it enough to call it testCreateShippingCategory?

Copy link
Member Author

Choose a reason for hiding this comment

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

The question is: do we test logic or received response?

Copy link
Member

Choose a reason for hiding this comment

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

We are testing response contents/headers only so I am fine with leaving that suffix.

Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO we test logic and we check if it's correct by comparing received responses.

@lchrusciel lchrusciel force-pushed the lets-test-some-apis branch 2 times, most recently from 2436168 to 66cc1f6 Compare December 11, 2015 10:48
@lchrusciel
Copy link
Member Author

@bendavies Thanks for advices! I have to update ApiTestCase README to, to point out these features ;)

@lchrusciel lchrusciel force-pushed the lets-test-some-apis branch 3 times, most recently from 3074947 to a4c2b96 Compare December 11, 2015 11:06
</testsuites>

<php>
<server name="KERNEL_CLASS_PATH" value="/app/TestKernel.php" />
Copy link
Contributor

Choose a reason for hiding this comment

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

missing KERNEL_DIR?

Copy link
Member Author

Choose a reason for hiding this comment

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

ApiTestCase allows you to use Kernel class directly. We need it, because we have two separated Kernels. The problem is with version of ApiTestCase on travis.

@lchrusciel lchrusciel force-pushed the lets-test-some-apis branch 2 times, most recently from a4c2b96 to ef68913 Compare December 15, 2015 13:35
bootstrap="app/bootstrap.php.cache"
>
<testsuites>
<testsuite name="Project Test Suite">
Copy link
Member

Choose a reason for hiding this comment

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

Sylius Test Suite

@pamil
Copy link
Contributor

pamil commented Dec 18, 2015

@Arn0d neither WebTestCase nor our Behat with Symfony2Extension (without JS) send a real HTTP request as both of them use Symfony\Bundle\FrameworkBundle\Client under the hood

>
<testsuites>
<testsuite name="Project Test Suite">
<directory>src/*/*Bundle/Tests</directory>
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have any directory that matches this pattern? Shouldn't it be src/*/Component/*/Tests as the component equivalent of the bundle one line below?

Copy link
Member

Choose a reason for hiding this comment

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

I'd remove this. Components don't have PHPUnit test and won't have them anytime soon I guess.

@arnolanglade
Copy link
Contributor

@pamil I know but it is enough for web case (get and post) when you use put method and you want to update a file, it is another story ;). PHP does not decode HTTP header... I just wanted to warn you but anyway, even if I prefer behat I prefer you test the API with any solution because : No test no beer

@pamil
Copy link
Contributor

pamil commented Dec 18, 2015

@Arn0d didn't know about that, thanks! Does it happen only on PUT request, but not on POST ones? Last week I was uploading image in some scenario on while creating taxonomy (POST) and it worked like a charm.

@@ -7,6 +7,7 @@ sylius_api_shipping_category_index:
defaults:
_controller: sylius.controller.shipping_category:indexAction
_sylius:
serialization_groups: [Default]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it needed? Isn't Default a default serialization group?

Copy link
Member

Choose a reason for hiding this comment

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

Right now:

https://github.com/Sylius/SyliusResourceBundle/blob/master/Controller/Configuration.php#L369

I don't know if JMS requires to specify Default explicitly as it won't add it automatically if you pass an empty array - not sure. So I think we should add 'Default' to the Configuration::getSerializationGroups() method which I linked above.

Copy link
Member Author

Choose a reason for hiding this comment

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

Shouldn't it be done in a separate PR?

@arnolanglade
Copy link
Contributor

@pamil Just with PUT and PATCH I did not test the latest version of PHP.

]);

$response = $this->client->getResponse();
$this->assertResponse($response, 'shipping_category/create_validation_fail', 400);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we use constant for codes? E.g. Response::HTTP_BAD_REQUEST here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we should ;)

Copy link
Member

Choose a reason for hiding this comment

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

👍

@pjedrzejewski
Copy link
Member

Please change the payload formatting according to my sample PR if you agree and update ApiTestCase version. And we are good to go! Good job Łukasz!

@pjedrzejewski
Copy link
Member

@lchrusciel Please rebase. :)

@lchrusciel
Copy link
Member Author

@pjedrzejewski done! I have done a little refactoring also, cuz of changes in ApiTestCase and feature added by @bendavies.

pjedrzejewski pushed a commit that referenced this pull request Dec 28, 2015
@pjedrzejewski pjedrzejewski merged commit 767f79f into Sylius:master Dec 28, 2015
@pjedrzejewski
Copy link
Member

Awesome work, thank you Łukasz! 👍

$this->client->request('PATCH', '/api/shipping-categories/'.$shippingCategories['shipping_category_1']->getId(), [], [], [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
'CONTENT_TYPE' => 'application/json',
], '{"name": "Light", "description": "Light weight items"}');
Copy link
Member

Choose a reason for hiding this comment

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

Would be great to update these to use the format I suggested above.

@pjedrzejewski
Copy link
Member

@lchrusciel Please have a look at my comment above and also... seems like we missed test for delete? /cc @michalmarcinkowski

@lchrusciel lchrusciel deleted the lets-test-some-apis branch February 1, 2016 15:13
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

8 participants