Travis composer install issue - php

I want to use travis with my simple php projects. My project structure is next:
My .travis.yml
language: php
sudo: required
before_install:
- cd http
before_script:
- cd http
install:
- composer self-update
- composer-install --no-interaction
script:
- phpunit --configuration phpunit.xml
and I want to run trevis into http folder, my composer.json and phpunit are there. However, as a result of my build, I received:
How can I solve this issue and run travis correctly? Thanks

Related

Configure cache on GitLab runner - install dependencies by composer only once

I have problem with gitlab cache config.
I use GitLab Community Edition 11.7.7, in docs we have:
https://docs.gitlab.com/ee/ci/caching/#caching-php-dependencies
So, my config:
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- vendor/
stages:
- code-style
- analysis
before_script:
- composer global require hirak/prestissimo
- composer install --ignore-platform-reqs --no-scripts
php-cs-fixer:
stage: code-style
script:
- vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --config=.php_cs.dist src tests
php-cs-fixer2:
stage: analysis
script:
- vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --config=.php_cs.dist src tests
Unfortunately, composer re-installs all dependencies in every job.
I would like to install all dependencies through the composer only once, how can I achieve it?
Your .gitlab-ci.yml configuration is similar to my own that run on self-hosting Gitlab at my company, I think it should works as well, So I have created an sample repo that runs pipelines based on your configuration in order to reproduce your problem.
As you can see here, I was trying to make a working pipeline by complete the configuration, For example, wrong image, missing composer executable...something like that, The final configuration is not much diffrent to yours.
image: php:7.2-alpine
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- vendor/
stages:
- code-style
- analysis
before_script:
- php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
- php composer-setup.php --install-dir=/usr/local/bin --filename=composer
- export PATH="/usr/local/bin:$PATH"
- php -r "unlink('composer-setup.php');"
- composer global require hirak/prestissimo
- composer install --ignore-platform-reqs --no-scripts
php-cs-fixer:
stage: code-style
script:
- vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --config=.php_cs.dist src tests || true
allow_failure: true
php-cs-fixer2:
stage: analysis
script:
- vendor/bin/php-cs-fixer fix --dry-run --diff --verbose --config=.php_cs.dist src tests
allow_failure: true
The last job #97744833 have a clue to answer your problem.
Compare the last part of outputs from previous failed job https://gitlab.com/Gasol/php-composer-cache-on-gitlab-ci/-/jobs/358118759#L191
187 Checked all files in 0.006 seconds, 10.000 MB memory used
191 ERROR: Job failed: exit code 8
to https://gitlab.com/Gasol/php-composer-cache-on-gitlab-ci/-/jobs/358121571#L190
The cache is creating when build success.
190 Creating cache master...
191 vendor/: found 4860 matching files
192 Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/15472690/master
193 Created cache
196 Job succeeded
So, after cache created, The job on the next stage should be ok for downloading cache archive.
https://gitlab.com/Gasol/php-composer-cache-on-gitlab-ci/-/jobs/358121572#L20
18 Checking cache for master...
19 Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/15472690/master
20 Successfully extracted cache
https://gitlab.com/Gasol/php-composer-cache-on-gitlab-ci/-/jobs/358121572#L46
And there are nothing to install when cache extracted. That's the result you want.
43 $ composer install --ignore-platform-reqs --no-scripts
44 Loading composer repositories with package information
45 Installing dependencies (including require-dev) from lock file
46 Nothing to install or update
47 Generating autoload files

How to run Laravel own unit tests

I just installed Laravel 5.5.* using following composer commands:
composer create-project --prefer-dist laravel/laravel blog "5.5.*" --prefer-source
Here is my Laravel instalation folder:
╭─yusuf#yusuf-he ~/test-laravel/blog/vendor/laravel/framework ‹cf750b0›
╰─➤ ls
CHANGELOG-5.4.md CHANGELOG-5.5.md CODE_OF_CONDUCT.md composer.json CONTRIBUTING.md LICENSE.md phpunit.xml.dist README.md src tests
And now I am wondering about how to run the unit tests that owned by the Framework it self ?
I've tried this but got another error:
╭─yusuf#yusuf-he ~/test-laravel/blog ‹f4cba4f*›
╰─➤ ./vendor/bin/phpunit ./vendor/laravel/framework/tests
PHP Fatal error: Class 'Illuminate\Tests\Integration\Database\DatabaseTestCase' not found in /home/yusuf/test-laravel/blog/vendor/laravel/framework/tests/Integration/Database/EloquentBelongsToManyTest.php on line 16
Fatal error: Class 'Illuminate\Tests\Integration\Database\DatabaseTestCase' not found in /home/yusuf/test-laravel/blog/vendor/laravel/framework/tests/Integration/Database/EloquentBelongsToManyTest.php on line 16
I need this to make sure that installed Laravel 5.5 is running well in my environtment.
Set the environment database testing on the .env file and phpunit.xml
Finally I got the answer by my-self, to test the Framework it self by running unit tests I need to install it from the inside of Framework folder.
cd ~/test-laravel/blog/vendor/laravel/framework
composer install
./vendor/bin/phpunit
Don't forget to set/uncomment the redis connection in php phpunit.xml
<env name="REDIS_HOST" value="127.0.0.1" />
<env name="REDIS_PORT" value="6379" />
and don't forget to make sure that your redis server is on
redis-server

Travis configuration for composer packages

While working on a laravel 5.1+ package I have this need to run automated tests through travis-ci.org. The difference with regular automated tests is the requirement to include this package into a framework and set specific configuration options to run the tests.
So the requirement would be:
install laravel
add my package as dependency
set some travis specific configurations like the travis database access
run migrations of laravel
run migrations specific for package or run an artisan command
run package specific unit tests
I searched everywhere; asked on laravel forums, asked in a travis community chat and saw this topic being closed as too localized (although an answer would have certainly been helpful now). I'm hoping my question is fit to remain open.
At this time I have the following configuration:
language: php
php:
- 5.5
- 5.6
- hhvm
addons:
hosts:
- system.hyn.me
- tenant.hyn.me
before_install:
- sudo composer self-update
install:
- composer create-project laravel/laravel
- cd ./laravel
- composer require hyn-me/multi-tenant ~0.1.0
- composer update
before_script:
- cp .env.travis .env
- export APP_ENV="testing"
- php artisan migrate -q -n --path ./vendor/hyn-me/multi-tenant/src/migrations
- cd ./vendor/hyn-me/multi-tenant
script: phpunit
Yet my knowledge of travis (thus far) is limited and before I send in an unneeded number of commits to fix my problems I'd rather have your opinion on what would be a good method to test integration into a framework.
Ps. this concerns the package hyn/multi-tenant.
Advise on how to keep this question as generic as possible would be helpful. I hope without explicitly mentioning best practice and requesting integration into framework examples helps in defining the scope of the answers.
So after weeks of pushing commits into travis, I finally made this work.
The .travis.yml:
language: php
sudo: true
php:
- 5.5
- 5.6
- 7.0
- hhvm
addons:
hosts:
- system.hyn.me
- tenant.hyn.me
install:
# fix ipv6 issue that prevented composer requests and resulted in failing builds
- sudo sh -c "echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf"
# updates composer on travis
- travis_retry composer self-update
# clear composer cache, might speed up finding new tags
- travis_retry composer clear-cache
# set the global github token, so connections won't be cancelled
- composer config -g github-oauth.github.com $GITHUB_TOKEN
# create a new database for the hyn connection
- mysql -e 'create database hyn;' -uroot
- mysql -e "grant all privileges on *.* to 'travis'#'localhost' with grant option;" -uroot
# create a new laravel project in the subfolder laravel (default composer behaviour)
- composer create-project laravel/laravel
# set global variables
- export DB_USERNAME=travis DB_DATABASE=hyn DB_PASSWORD= QUEUE_DRIVER=sync
script:
# run the script calling unit tests and so on
- ./scripts/travis.sh
after_script:
- if [[ $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover ${TRAVIS_BUILD_DIR}/coverage.clover; fi
And the scripts/travis.sh
#!/bin/bash
# e causes to exit when one commands returns non-zero
# v prints every line before executing
set -ev
cd ${TRAVIS_BUILD_DIR}/laravel
BRANCH_REGEX="^(([[:digit:]]+\.)+[[:digit:]]+)$"
if [[ ${TRAVIS_BRANCH} =~ $BRANCH_REGEX ]]; then
echo "composer require ${TRAVIS_REPO_SLUG}:${TRAVIS_BRANCH}"
composer require ${TRAVIS_REPO_SLUG}:${TRAVIS_BRANCH}
else
echo "composer require ${TRAVIS_REPO_SLUG}:dev-${TRAVIS_BRANCH}"
# development package of framework could be required for the package
composer require hyn-me/framework "dev-master as 0.1.99"
composer require "${TRAVIS_REPO_SLUG}:dev-${TRAVIS_BRANCH}#${TRAVIS_COMMIT}"
fi
# moves the unit test to the root laravel directory
cp ./vendor/${TRAVIS_REPO_SLUG}/phpunit.travis.xml ./phpunit.xml
phpunit
# phpunit --coverage-text --coverage-clover=${TRAVIS_BUILD_DIR}/coverage.clover
This code might change due to new Laravel versions or changes in travis. If this is the case, you will find the latest release here.

Travis-CI: how to cache composer executable between builds?

On every travis build, composer self-update is run. And it gets updated on every build. Is it possible to cache composer executables like we do it with vendor dir via
cache:
directories:
- vendor
- $HOME/.composer/cache
I thought about caching the whole /home/travis/.phpenv/versions/5.5/bin/composer but I feel this is not right because the contents of this folder may change without notifying caching system about a change (when travis updates php version for example).
Any suggestions (except for custom composer, of course)?
I'd recommend not updating composer itself, but let travis handle it. (its automatically updated every 30/60 days)
Also i can recommend using the new containerized infrastructure to speed up the runs and allow caching...
language: php
sudo: false
cache:
directories:
- $HOME/.composer/cache
php:
- 5.5
- 5.6
- 7
- hhvm
install:
- composer install
script: vendor/bin/phpunit
The sudo: false statement indicates the use of containers. The cache: statement makes sure composer caches correctly.
If you really want to cache the composer binary:
language: php
php:
- 5.5
- 5.6
- 7
- hhvm
cache:
directories:
- $HOME/.composer/cache
install:
- travis_retry composer self-update && composer --version
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction
script: vendor/bin/phpunit
Also just as a heads up, if testing for HHVM and you need to set the datetime, have a look at https://github.com/travis-ci/travis-ci/issues/2523. My way of solving this is adding a .ini-file in my test-directory with the datetime and setting this in the correct folder for all test-runners. This is prepended in the install:-step:
- mkdir -p /home/travis/.phpenv/versions/$(phpenv version-name)/etc/conf.d
- phpenv config-add test/phpconfig.ini
Anyway, a bit more information than you requested, but i hope this helps someone looking for composer/travis/stuff :)

PHPUnit not working with Laravel 5

I just installed a fresh Laravel 5 project, my first one on this version. PHPUnit is supposed to be out of the box with the framework and every tutorials I saw just say to type phpunit within the project folder to launch the Unit Tests.
I checked and PHPUnit is in the composer.json, I also did a composer install and composer update just in case it wouldn't be here
website(master)$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Removing phpunit/phpunit (4.6.1)
- Installing phpunit/phpunit (4.6.2)
Downloading: 100%
But it just doesn't work phpunit isn't recognized at all
website(master)$ phpunit
-bash: phpunit: command not found
Seems like nobody got this problem before as I Googled it. I hope I'm not doing any stupid mistake. Any idea or suggestion ? Thanks guys ;)
I didn't install PHPUnit globally and didn't define the path. So for anyone who would have same problem :
composer global require phpunit/phpunit
composer global require phpunit/dbunit
Then you add this to you ~/.bash_profile or ~/.profile
export PATH=~/.composer/vendor/bin:$PATH
This occurs when you don't have phpunit installed globally.
Run this command to use the local version (installed with composer):
vendor/bin/phpunit
in windows machine the command is different please use this command
php vendor/phpunit/phpunit/phpunit
orignal source
You can run this command in cmd before running phpunit command:
doskey phpunit="vendor/bin/phpunit"
And if you are lazy as I am, you can run this one:
doskey pu="vendor/bin/phpunit"
for people who have WINDOWS 7, use the .\vendor\bin\phpunit command instead of ./vendor/bin/phpunit
Run the command
composer config --list --global | grep -w home
You can find the find the [home] with composer path, similar to this one.
[home] /home/example_username/.config/composer
The path ~/.config/composer is where composer global packages are installed. Next run the command...
export PATH=~/.config/composer/vendor/bin:$PATH
I made a permanent link to my phpunit like this
echo 'alias phpunit=vendor/bin/phpunit' >> ~/.bash_aliases
now phpunit is working by itself and stays even after I restart the terminal
Include this line on your composer.json
"phpunit/phpunit": "4.0.*",
Run composer update.
You should be able to run the following command on your Laravel directory.
vendor/bin/phpunit

Categories