Travis.ci Environment Variables not reading in phpunit - php

Can someone help me understand why my environment variables aren't being read inside of my phpunit test from travis.ci?
So I'm attempting to write some automatic testing using travis for a php/javascript app I'm working on. However when I wrote a test to check for environment variables reading into phpunit from travis, they fail. That means (as far as I can tell) that either the environment variables aren't able to be read by phpunit, or they aren't being passed through to the travis test properly.
.travis.yml
language: php
php:
- '7.0'
- '7.1'
before_install:
- echo "extension=ldap.so" >>php --ini | grep "Loaded Configuration" | sed -e "s|.:\s||"``
install:
- cd test
- npm install
- cd ..
script:
- echo $API_BASE_URL
- phpunit test/build_tests.php
notifications:
on_success: never
on_failure: never
phpunit Test File
<?php
use PHPUnit\Framework\TestCase;
class build_tests extends TestCase
{
public function testForEnv()
{
$this->assertEquals(isset($_ENV['API_BASE_URL']), true);
$this->assertEquals(isset($_ENV['DRINK_SERVER_URL']), true);
$this->assertEquals(isset($_ENV['LOCAL_DRINK_SERVER_URL']), true);
$this->assertEquals(isset($_ENV['RATE_LIMIT_DROPS_DROP']), true);
$this->assertEquals(isset($_ENV['DEBUG']), true);
$this->assertEquals(isset($_ENV['DEBUG_USER_UID']), true);
$this->assertEquals(isset($_ENV['DEBUG_USER_CN']), true);
$this->assertEquals(isset($_ENV['USE_LOCAL_DRINK_SERVER']), true);
}
}
?>
travis Exporting of Environment Variables
$ Setting environment variables from repository settings
$ export DRINK_SERVER_URL=https://drink.csh.rit.edu:8080
$ export LOCAL_DRINK_SERVER_URL=http://localhost:3000
$ export RATE_LIMIT_DROPS_DROP=3
$ export DEBUG=true
$ export DEBUG_USER_UID=[secure]
$ export DEBUG_USER_CN=[secure]
$ export USE_LOCAL_DRINK_SERVER=true
$ export API_BASE_URL='api/index.php?request='
phpunit Result
PHPUnit 6.1.1 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 260 ms, Memory: 6.00MB
There was 1 failure:
1) build_tests::testForEnv
Failed asserting that true matches expected false.
/home/travis/build/devinmatte/WebDrink-2.0/test/build_tests.php:9
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Can someone help me understand why my environment variables aren't being read inside of my phpunit test? I would really appreciate it.

Try using getenv function call instead. In travis enviroment the $_ENV variables are not available

Related

Codeception Acceptance Tests not working

everyone!
I have been trying to configure Codeception 2.3.6 with Laravel 5.3.30 running on PHP 7.0.23 powered by WAMP 3.1.0. My functional test cases are running fine, but when I try to run my acceptance test cases, a new chrome window opens and then closes without doing anything.
The output in the HTML Report is Codeception Results OK(0s), while the output on the command line is:
WelcomeCept: Perform actions and see result (0.00s)
Time: 3.24 seconds, Memory: 22.75MB
OK (1 test, 0 assertions)
HTML report generated in file://D:\wamp\www\myApp\tests/_output\report.html
First I start ChromeDriver with the command
chromedriver --url-base=/wd/hub
Then I start Selenium Standalone Server 3.13.0 with the command:
java -Dwebdriver.chrome.driver="chromedriver" -jar selenium-server-standalone-3.13.0.jar -port 4445
Then I run my acceptance test suite which contains a single test file, with the command:
call vendor/bin/codecept run acceptance --html
My acceptance.suite.yml is:
class_name: AcceptanceTester
modules:
enabled:
- WebDriver:
url: http://lcms.com/
window_size: false # disabled in ChromeDriver
port: 9515
browser: 'chrome'
restart: true
wait: 200
capabilities:
unexpectedAlertBehaviour: 'accept'
webStorageEnabled: true
javascriptEnabled: true
- Laravel5:
part: ORM
cleanup: false # can't wrap into transaction
environment_file: .env
- \Helper\Acceptance
My WelcomeCept.php file, just for testing the configuration, is:
<?php
class WelcomeCept
{
public function welcomeTest(AcceptanceTester $I)
{
$I->wantTo('perform actions and see result');
}
}
Please review my workflow and let me know if I'm doing things incorrectly or am missing something.
Thanks!
Update: Same thing is happening using GeckoDriver or PhantomJS in WebDriver mode. The tests are passing with OK but not performing any actions.
Solved! I tried WelcomeCest instead of WelcomeCept and things have fallen into place.

How to hide cakephp log in console result phpunit?

When running unit test by phpunit on console, it always displays the message I input in logfile:
This the sample file I will test:
Sample.php
public funtion testThis() {
CakeLog::write('error', "This is a test");
}
then when I run in console
$ ./Console/cake test app
I will get this result:
PHPUnit 3.7.38 by Sebastian Bergmann.
............This is a test..........50/50 (100%)
You need to check the configuration / configure the logger. My guess is that it doesn't have where to write, so is using STDOUT or STDERR for output.
Try to set up it with instructions from here (choose the appropriate Cake version).

How to run a single test case method in cakephp

I am using the cakephp test (php unit test) with xdebug to run the server side code and monitor the output from the terminal rather than using it for validation.
So, every time, i want to run some particular lib/controller/model method from the terminal and see the output, i have to comment out the other test case functions.
I know this might not be the right approach,
but i want to know, if there is a way, i can build up a wrapper around the cake test, that will take the argument of the method name which i want to run?
There is no need to write a wrapper script
To run one test method
Use the filter option:
-> phpunit --help
PHPUnit 4.4.1 by Sebastian Bergmann.
Usage: phpunit [options] UnitTest [UnitTest.php]
phpunit [options] <directory>
...
Test Selection Options:
--filter <pattern> Filter which tests to run.
For example:
-> phpunit --debug --filter testValidationDefault tests/TestCase/Model/Table/PostsTableTest.php
PHPUnit 4.4.1 by Sebastian Bergmann.
Configuration read from /var/www/cakephp.dev/phpunit.xml.dist
Starting test 'App\Test\TestCase\Model\Table\PostsTableTest::testValidationDefault'.
I
Time: 130 ms, Memory: 9.75Mb
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Incomplete: 1.
www-data # dev [ /var/www/cakephp.dev ]
->
To run only one test use the filter option in the cake command line tool:
cake test app Model/Post --filter testGetAllPostsByAuthor
Works on CakePhp 2.2

Laravel : phpunit cannot open file ExampleTest.php

Hi i am quit new in Laravel PHPUnit, getting the following error :
Laravel : phpunit cannot open file ExampleTest.php
I don't have idea why i am getting this error. I installed PHPUnit globally and when i run "phpunit" in terminal it runs fine. But I want to run it on specific file like :
phpunit ExampleTest
Thanks In Advance.
Make sure you are on the project root and referencing the file inside the tests folder.
Example:
phpunit tests/ExampleTest.php
I am working in Windows. Try to use the full path:
C:\Desktop\code\blog\vendor\bin>phpunit C:\Desktop\code\blog\tests\Feature\ExampleTest.php
PHPUnit 7.2.6 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 1.46 seconds, Memory: 10.00MB
OK (1 test, 1 assertion)
This started happening when I upgrade phpUnit, I had to update phpStorm to fix it.
try it like this:
vendor\bin\phpunit tests\Unit\ExampleTest.php
it will work surly
but first install phpunit globally.
Maybe you call wrong to your test.
Try like this :
php artisan test --filter YourTestClassName

Using phpunit CLI output in a php script

The CLI output from a phpunit test contains useful information, such as:
PHPUnit 3.6.4 by Sebastian Bergmann.
F
Time: 7 seconds, Memory: 5.50Mb
There was 1 failure:
1) testHandler::testItem
Failed asserting that page text contains <itest New Family Name>.
Driver.php:632
testHandler.php:29
S163.php:18
testHandler.php:46
FAILURES!
Tests: 1, Assertions: 97, Failures: 1.
I'd like to send this information to my test tracking software by way of a POST through php curl.
However I can't find a way of catching the information in my test class (which extends PHPUnit_Framework_TestCase).
Any help / thoughts would be appreciated.
The easiest way if you have a custom build solution seems to be:
phpunit | curl -X http://url
or
phpunit 2>&1 > output.txt && cat output.txt | curl -X http://url
or something like that.
If you are using a proper Continuous Integration server look into the --log-junit and --coverage-clover or other output formats like --log-json.
I don't see any reason why you would want to get the output programmatically while the test are running. It just seems to be a lot more work than gain. There are ways to extend PHPUnit to integrate it with bugtrackers though. Have a look at the PHPUnit_Framework_TestListener.

Categories