Travis-Ci not following PSR-4 - php

I am trying to learn how to use Travis-CI. My first attempt is not going well.
I have a project at https://github.com/RogerCreasy/simpleJWT
Here is my .travis.yaml
language: php
php:
- '5.4'
- '5.5'
- '5.6'
- '7.0'
before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev
script: vendor/bin/phpunit --configuration phpunit.xml
I am using PSR-4 autoloading in my composer.json file.
"autoload": {
"psr-4": {
"RogerCreasy\\SimpleJWT\\": "src"
}
},
PHPUnit tests run successfully locally. But, through Travis they fail. Travis looks for the class I am testing at the full namespace (/RogerCreasy/SimpleJWT/)
Where should I look for the problem?

Related

Travis composer install issue

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

Gitlab CI -Laravel 5.5 - PHPUnit "Tests\TestCase not found"

I set up a Gitlab CI Pipeline for a Laravel 5.5 project (files at the bottom of the post) and the build is succeeding, but the tests fail because PHPUnit exits with the error message
Fatal error: Class 'Tests\TestCase' not found in /builds/[User]/[Repo]/tests/Feature/DocumentTest.php on line 8
(Obviously, [User] and [Repo] aren't the real values, just don't wanna make it public here)
I've tried different things already, like installing PHPUnit globally, calling PHPUnit in different ways, it all ends up with the same error message.
PHPUnit is running successfully on my local (no matter HOW I call it, it always works). So anyone knows what the problem here might be?
For reference here are the pipeline files:
.gitlab-ci.yml:
image: woohuiren/php-laravel-env:latest
services:
- mysql:latest
- redis:4.0.2-alpine
before_script:
- apk update
- docker-php-ext-install pcntl
variables:
MYSQL_DATABASE: testdb
MYSQL_ROOT_PASSWORD: root
stages:
- build
- test
build_job:
stage: build
script:
- sh .gitlab-build.sh
artifacts:
paths:
- vendor/
- bootstrap/
- composer.phar
- .env
tags:
- docker
test_job:
stage: test
dependencies:
- build_job
script:
- sh .gitlab-ci.sh
tags:
- docker
.gitlab-build.sh:
#!/bin/bash
set -eo pipefail
php -v
ping -c 3 mysql
php composer.phar install --no-progress --no-scripts
php artisan package:discover
cp -v .env.testing .env
php artisan key:generate
php artisan optimize
php artisan config:clear
php artisan storage:link
php artisan migrate --seed
php artisan jwt:secret
php artisan passport:install --force
.gitlab-ci.sh:
#!/bin/sh
set -eo pipefail
php -v
ping -c 3 mysql
./vendor/phpunit/phpunit/phpunit -v --testdox
What is Tests\TestCase? I'm guessing line 8 in DocumentTest.php is
public class DocumentTest extends TestCase
...where TestCase is PHPUnit\Framework\TestCase. It seems like there's an autoloading problem or the file doesn't import TestCase. Try changing this line to:
public class DocumentTest extends PHPUnit_Framework_TestCase
I can try to give a better answer if you include the phpunit.xml and composer.json.
I got the same error on local though. Make sure your composer.json has
"psr-4": {
"App\\": "app/",
"Tests\\": "tests/"
},
run composer dump-autoload at least once or make it a part of your testing procedure.

PHPUnit version does not match version in Composer

I'm trying to update my phpunit version, however it doesn't seem to be working with the regular composer install.
My composer.json file is as follows:
{
"require": {
...
},
"require-dev": {
"phpunit/phpunit": "4.*",
"phpunit/php-invoker": "^1.1",
"phpunit/dbunit": "^2.0"
}
}
And running both composer install and update as php composer.phar install and php composer.phar update (I don't have composer installed globally which might be the issue?) I get
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
When I check to see the phpunit version, I get
$ phpunit --version
PHPUnit 3.7.28 by Sebastian Bergmann.
My PHP version is 5.6 which does seem to rule out the solution from this very similar issue.
My best guess is that there is a global version of phpunit installed and overriding my local one. But I haven't been able to find any guidance as to how to fix this without making too many changes to the rest of the global environment.
The command by itself phpunit will search in your shell's path for a PHPUnit executable. A composer install will place it into the composer bin dir, which is vendor/bin by default. You can execute the script directly with ./vendor/bin/phpunit, or allow composer to execute it for you, resolving the proper path for your project with composer exec phpunit

How to configure CakePHP in Travis-Ci

I have a CakePHP project where I also integrated Travis-CI to start automated testing.
Now my tests look something like this:
<?php
App::uses('CustomControllerTest', 'Lib');
class AllocationsControllerTest extends CustomControllerTest
{...
My travis.yml:
language: php
php:
- '5.3'
before_script:
- composer install
- php bootstrap.php
script:
phpunit --configuration phpunit.xml --coverage-text
bootsrap.php is currently empty.
When triggering Travis it says it doesn't find Class'App'.
What do I have to add to the .travis.yml or bootstrap.php so it loads all the dependencies needed in the test classes?

Install PHPUNIT with Composer

I have project on Symfony 2 and i would like use PHPUNIT on Windows 7.
On githut phpunit is:
Composer
Simply add a dependency on phpunit/phpunit to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a development-time dependency on PHPUnit 3.7:
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
For a system-wide installation via Composer, you can run:
composer global require 'phpunit/phpunit=3.7.*'
Make sure you have ~/.composer/vendor/bin/ in your path.
First i use system-wide installation but i dont know when this installed.
Next i add to my composer.json require-dev.
This installed phpunit in C:/wamp/www/myproject/vendor/symfony. Next i try commands:
composer install --dev
And i can't use phpunit. In cmd.exe i enter "phpunit" and i have error:
'phpunit' is not recognized as an internal or external command operable program or batch file
How can i use phpunit? I have Windows 7, Wamp server and php 5.4.12.
When you install PHP-Unit in windows via composer, the global installation will create files in
C:\Users\YOUR_USERNAME\AppData\Roaming\Composer
To execute phpunit easily via command line you need to add path of phpunit.bat file in windows Environment Variables. For this:
Right click My Computer
Go to Properties -> Advance system settings and
Click Environment variables from the Advance tab.
Now add C:\Users\YOUR_USERNAME\AppData\Roaming\Composer\vendor\bin to the windows PATH.
You can now run the phpunit from command. Note that you may need to restart your command prompt for the changes to take effect.
The bin file of packages are put in the configured bin directory. By default, this is vendor/bin and when you use the symfony standard edition, this is the bin folder.
To execute this bin file, run ./bin/phpunit (or ./vendor/bin/phpunit when not using the Symfony Standard Edition)
Windows users have to put this in double quotes: "bin/phpunit" (or "vendor/bin/phpunit")
composer require --dev phpunit/phpunit ^7
The above example assumes, composer is already on your $PATH variable.
You composer.json should look similar to;
{
"name": "vendor_name/package_name",
"description": "This project is for practicing writing php unit tests",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Umair Anwar",
"email": "umair.anwar#gmail.com"
}
],
"autoload": {
"classmap": [
"src/"
]
},
"require-dev": {
"phpunit/phpunit": "^7",
"phpunit/dbunit": "^4.0"
}
}
Easiest way to install phpunit via composer is to run from project root.
$ composer require phpunit/phpunit
What this would do is, it will create a phpunit folder inside vendor/bin
and you can run unit tests like this..
$ ./vendor/bin/phpunit
Too simple operation on Windows with composer and works for me following way:
Install composer
https://getcomposer.org/doc/00-intro.md#installation-windows Go to
your symphony folder e.g C:\wamp64\www\symfony\UserManagement where is
composer.json and run this command.
Should be register with global to not have issue $phpunit bash: phpunit: command not found
//old version is 5.7 new 6.4 or put newest version.
composer global require --dev phpunit/phpunit ^5.7
I remember futzing around with the composer dependency stuff for phpunit and never could get it to work.
Instead, from your git bash shell:
mkdir ~/bin
cd ~/bin
curl https://phar.phpunit.de/phpunit.phar > phpunit
chmod +x phpunit
exit out of bash and then start a new bash session.
And you should be good to go. You can echo $PATH to verify you have a path to ~/bin but one seems to added by default.
https://phar.phpunit.de/phpunit.phar
I also came across the same issue and find out the solution by following steps
To run PHPUnit in Windows 7 under WAMP installation
Composer Install
{
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
}
Simply Set Environment Variable
The php unit will be install in a vendor dir in vendor/bin
Path : C:\wamp\www\myproject\vendor\bin;
Open a new Command Prompt
C:\Users\guny >phpunit --version
PHPUnit 3.7.30 by Sebastain Bergmann

Categories