Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bradbird's avatar

API Testing in Travis-CI

I am trying to setup API Testing in Laravel. We are using travis to automate the build and deploy process. I have tried many different things and up to this point I know that Laravel routing on the travis server does not work.

When the API test attempts to hit an endpoint it always returns 404 not found. I have tried adding "php artisan serve --port=80" to my scripts but it just holds the terminal window and does not continue the rest of the build. The only other option I have considered is to try and point all API calls to the actual URL rather than the masked one so something like ".php?route=something" in plain PHP terms.

I also stumbled upon this post if it helps anyone help me: https://laracasts.com/discuss/channels/testing/testing-api-calls-on-travis.

I've exhausted all of my options with this one and really need to get this working as it's on a very large project of mine. Any ideas would be greatly appreciated! I have listed one of my tests below.

function it_creates_a_thread()
    {
        $thread = factory(\App\Models\Forum\Thread::class)->make();
        $thread->body = 'Testing';

        $this->authUserPost($this->apiForumResource . '/thread', ['thread' => $thread->toArray()])
            ->seeInDatabase('forum_threads', ['title' => $thread->title])
            ->seeInDatabase('forum_posts', ['body' => $thread->body])
            ->seeJsonContains(['title' => $thread->title]);
    }
0 likes
2 replies
zachleigh's avatar

We need more info. What does your travis.yml file look like? Any scripts for travis to run? Can we see your routes file? What kind of errors are you getting? Also, have you set a travis section in .env?

bradbird's avatar

Everything works fine except when I try to implement API tests.

$this->apiForumResource

The above translates to "http://localhost/api/forum" so the full API call would be to "http://localhost/api/forum/thread" - This works on my local environment fine.

Below is my .travis.yml file.

# see http://about.travis-ci.org/docs/user/languages/php/ for more hints
language: php

# list any PHP version you want to test against
php:
# using major version aliases
    # aliased to a recent 7.x version
    - 7.0

sudo: false

cache:
  directories:
      - node_modules
      - vender
      - $HOME/.composer/cache

services:
  - mysql
  - elasticsearch

before_install:
  - nvm install 0.12
  - nvm use 0.12

install:
  - composer install
  - npm install
  - npm install gulp
  - npm install bower
  - bower install

# execute any number of scripts before the test run, custom env's are available as variables
before_script:
  - mysql -e 'create database ###;'
  - export DB_DATABASE='###'
  - export DB_USERNAME=travis
  - export DB_PASSWORD=
  - php artisan db:seed --class=ElasticSearchSeeder --force
  - php artisan migrate --seed --force
  - php artisan key:generate

# omitting "script:" will default to phpunit
script: 
    - gulp
    - phpunit --configuration phpunit.xml --coverage-text

# whitelist
branches:
  only:
    - master
    - develop

before_deploy: 
    - "git config --global user.email '###'"
    - "git config --global user.name '###'"
    - "git add public -f"
    - "git commit -m 'Add build assets'"

deploy:
  skip_cleanup: true
  provider: heroku
  api_key: ###
  strategy: git
  app:
    develop: ###
    master: ###
  run: "php /app/artisan migrate --force"
  
# configure notifications (email, IRC, campfire etc)
notifications:
  email: "k###"

Please or to participate in this conversation.