Api: Run phpunit with dingo/api

Created on 14 Apr 2016  路  7Comments  路  Source: dingo/api

I want to run phpunit to test api, for example, I have api: /api/user, however when I test $this->get('/api/user') in phpunit, it tells me 404 not found.

And I run php artisan route:list, no api route list.

How to solve that?

Most helpful comment

I don't remember what was my problem with it nor why I'm getting notifications on this thread, but I know this:

I changed my phpunit.xml and added Dingo API environment variables like this:

        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="sqlite_testing"/>
        <env name="API_STANDARDS_TREE" value="x"/>
        <env name="API_SUBTYPE" value="prime"/>
        <env name="API_DOMAIN" value="127.0.0.1"/>
        <env name="API_VERSION" value="v2"/>
        <env name="API_DEFAULT_FORMAT" value="json"/>
        <env name="API_STRICT" value="false"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>

and in my TestCase.php file I did this:

    protected $baseUrl = 'http://127.0.0.1';

and that was enough to solve this problem. I figured this out by digging a lot deep into the files that process a request on Laravel/Dingo API and I noticed that I was getting blocked due to domain validation and that's when I noticed I should be making a call to a URL that my API_DOMAIN offers.

All 7 comments

use php artisan api:routes instead

@timsims I am sorry, I know php artisan api:routes can list api.

However, phpunit does not work on api list

@yaoshanliang what framework are you using and what versipn? lumen|laravel 5.1|5.2

@catalinux laravel 5.1

I mean phpunit only test routes which php artisan route:list has. Not include php artisan api:routes

how you solve it, Could you tell me?

I don't remember what was my problem with it nor why I'm getting notifications on this thread, but I know this:

I changed my phpunit.xml and added Dingo API environment variables like this:

        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="sqlite_testing"/>
        <env name="API_STANDARDS_TREE" value="x"/>
        <env name="API_SUBTYPE" value="prime"/>
        <env name="API_DOMAIN" value="127.0.0.1"/>
        <env name="API_VERSION" value="v2"/>
        <env name="API_DEFAULT_FORMAT" value="json"/>
        <env name="API_STRICT" value="false"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>

and in my TestCase.php file I did this:

    protected $baseUrl = 'http://127.0.0.1';

and that was enough to solve this problem. I figured this out by digging a lot deep into the files that process a request on Laravel/Dingo API and I noticed that I was getting blocked due to domain validation and that's when I noticed I should be making a call to a URL that my API_DOMAIN offers.

Was this page helpful?
0 / 5 - 0 ratings