We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

[SOLVED]UnitTest on my API project

I wrote a simple API website, my apps POST data to APIs and APIs return data to app directly, no views. So I want to know how can I write unit tests using PHPUnit for those APIs.

Thanks for any suggestions.

use curl -d ... url now.

You will need to use a HTTP Client or CURL for your requests.

Basically you can test against your existing website with data that you already know i.e. your website connects to a test database where you know for your testing purposes that you have one user in the user table called "User" and one product etc. So when you hit the /user/1 endpoint you will get a response with the relevant user object.

There are plenty of HTTP Clients (one in the incubator actually https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Http/Client), Guzzle (https://docs.guzzlephp.org/en/latest/) or even CURL itself that you can use to POST to your endpoint and then parse the response.

The way I would do it is as follows:

I have a site that connects to a test database. That site is a clone of my own site. In the test database I have known data i.e. one user, one product etc.

In my test I invoke the HTTP client and POST to the test site's endpoints and then check what I get if it is the result I am expecting.

A more elaborate approach would be to invoke tests with a special setting/parameter i.e. UNIT_TEST = 1, where your app on bootstrap would connect to the test database and work from there.