Codeception 'Undefined variable output' when trying to run a unit test - php

When attempting to run a simple unit test, as follows
// tests
public function testMe()
{
$this->assertEquals(1+1, 2);
}
I get the following error:
PHP Notice: Undefined variable: output in phar://C:/bin/codecept.phar/src/Codeception/Lib/Parser.php on line 129
PHP Stack trace:
PHP 1. {main}() C:\bin\codecept.phar:0
PHP 2. require_once() C:\bin\codecept.phar:7
PHP 3. Codeception\Application->run() phar://C:/bin/codecept.phar/codecept:36
PHP 4. Symfony\Component\Console\Application->run() phar://C:/bin/codecept.phar/src/Codeception/Application.php:103
PHP 5. Symfony\Component\Console\Application->doRun() phar://C:/bin/codecept.phar/vendor/symfony/console/Application.php:117
PHP 6. Symfony\Component\Console\Application->doRunCommand() phar://C:/bin/codecept.phar/vendor/symfony/console/Application.php:186
PHP 7. Symfony\Component\Console\Command\Command->run() phar://C:/bin/codecept.phar/vendor/symfony/console/Application.php:815
PHP 8. Codeception\Command\Run->execute() phar://C:/bin/codecept.phar/vendor/symfony/console/Command/Command.php:256
PHP 9. Codeception\Command\Run->runSuites() phar://C:/bin/codecept.phar/src/Codeception/Command/Run.php:256
PHP 10. Codeception\Codecept->run() phar://C:/bin/codecept.phar/src/Codeception/Command/Run.php:329
The line in question, 129 in Parser.phpis the following:
public static function validate($file)
{
$config = Configuration::config();
if (empty($config['settings']['lint'])) { // lint disabled in config
return;
}
exec("php -l ".escapeshellarg($file)." 2>&1", $output, $code);
if ($code !== 0) {
throw new TestParseException($file, implode("\n", $output));
}
}
The line 129 is exec("php -l ".escapeshellarg($file)." 2>&1", $output, $code);
I have tried googling around, and couldn't find any one else with the issue. This is in a completely "empty" php project, with only this unit test. I installed Codeception with composer.
I created the test as follows:
codecept generate:test unit Example
filled in the above and
codecept run unit
Codeception version: 2.2.2
Php version: 5.6.21

Ok I found more information, namely here:
https://github.com/Codeception/Codeception/issues/3324
It seems that this is a very new problem with version "2.2.*"
The possible solutions are:
Disable linting
Insert
settings:
lint: false
Into the codeception.yaml file.
Downgrade
Since validation is introduced in version 2.2, downgrading to the latest version (2.1.11) should also fix the issue according to the devs.

Related

PHPunit test errors with `Parse error` on CI

I'm unit testing my code with the help of phpunit 9.3.8. The tests run fine on my local dev environment (I'm running Windows 10, PHP 7.4.2 with Xdebug 2.9.2) but fail because of a parse error when I try to run phpunit on Gitlab CI (The CI runs the latest alpine linux image).
The error is:
$ composer run test
> phpunit
PHPUnit 9.3.8 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.23 with Xdebug 2.9.8
Configuration: /builds/gaspacchio/back-to-the-future/phpunit.xml
Reporter
✘ Can set short message [6.05 ms]
│
│ ParseError: syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
│
│ /builds/gaspacchio/back-to-the-future/src/api/utilities/Reporter.php:21
│ /builds/gaspacchio/back-to-the-future/src/api/tests/ReporterTest.php:12
│
// More errors
Time: 00:01.970, Memory: 310.00 MB
ERRORS!
Tests: 6, Assertions: 0, Errors: 6.
Generating code coverage report in Clover XML format ... done [00:00.330]
PHP Warning: fopen(/builds/gaspacchio/back-to-the-future/): failed to open stream: Is a directory in /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/Util/Printer.php on line 89
PHP Stack trace:
PHP 1. {main}() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/phpunit:0
PHP 2. PHPUnit\TextUI\Command::main() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/phpunit:61
PHP 3. PHPUnit\TextUI\Command->run() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/TextUI/Command.php:100
PHP 4. PHPUnit\TextUI\TestRunner->run() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/TextUI/Command.php:147
PHP 5. PHPUnit\Util\Printer->__construct() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:756
PHP 6. fopen() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/Util/Printer.php:89
Code Coverage Report:
2020-10-13 10:12:09
Summary:
Classes: 0.00% (0/11)
Methods: 0.00% (0/38)
Paths: 0.00% (0/7)
Branches: 0.00% (0/7)
Lines: 0.00% (0/397)
Script phpunit handling the test event returned with error code 2
Line 21 of the Reporter.php file is below:
<?php namespace utilities\Reporter;
/**
* The Reporter class is responsible for returning data to the client.
*/
class Reporter
{
/** This is the code of the answer.
* (more comments)
* #var int The status code.
*/
private int $code; //<-- This is line number 21
And the line 12 of ReporterTest.php is:
<?php
use PHPUnit\Framework\TestCase;
use utilities\Reporter\Reporter;
class ReporterTest extends TestCase
{
protected $reporter;
protected function setUp(): void
{
$this->reporter = new Reporter(); // <-- Line 12 is here
}
}
I'm using the fixtures function of PHPunit, defined as :
PHPUnit supports sharing the setup code. Before a test method is run, a template method called setUp() is invoked. setUp() is where you create the objects against which you will test.
Type hinting of object properties is in PHP 7.4 and this is running
PHPUnit 9.3.8 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.23 with Xdebug 2.9.8
It's basically a PHP version conflict,
for some reason PHPUnit is trying to parse it like older PHP versions where private int $code; is invalid (and private $code; should be used instead).
Oh, just noticed your Windows system is running PHP 7.4
but Gitlab CI image has PHP 7.3.23 installed!

Updated composer.json, now PHP autoload does not work. PHPUnit cannot find my classes

I am a user of the EWZSearchBundle and want to update it to work with Symfony 3.x
Updating the composer.json file should be easy, but I don't manage to get the unit tests running.
Here is the link to my fork of the bundle with some "improvements": link to commit
When I run the phpunit tests, I get the following error:
$ vendor/bin/phpunit Tests/Lucene/FieldTest.php
PHPUnit 5.7.26 by Sebastian Bergmann and contributors.
PHP Fatal error: Call to undefined method Zend\Search\Lucene\Document\Field::getType() in /tmp/azine/EWZSearchBundle/Tests/Lucene/FieldTest.php on line 18
PHP Stack trace:
PHP 1. {main}() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/phpunit:52
PHP 3. PHPUnit_TextUI_Command->run() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/src/TextUI/Command.php:116
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/src/TextUI/Command.php:186
PHP 5. PHPUnit_Framework_TestSuite->run() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:517
PHP 6. PHPUnit_Framework_TestCase->run() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/src/Framework/TestSuite.php:733
PHP 7. PHPUnit_Framework_TestResult->run() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/src/Framework/TestCase.php:868
PHP 8. PHPUnit_Framework_TestCase->runBare() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/src/Framework/TestResult.php:686
PHP 9. PHPUnit_Framework_TestCase->runTest() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/src/Framework/TestCase.php:913
PHP 10. ReflectionMethod->invokeArgs() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/src/Framework/TestCase.php:1062
PHP 11. EWZ\Bundle\SearchBundle\Tests\Lucene\FieldTest->testGetType() /tmp/azine/EWZSearchBundle/vendor/phpunit/phpunit/src/Framework/TestCase.php:1062
Steps to reproduce:
$ git clone git#github.com:azine/EWZSearchBundle.git
$ cd EWZSearchBundle
$ composer install
$ vendor/bin/phpunit Tests/Lucene/FieldTest.php
The cause of this error is, that the EWZ\Bundle\SearchBundle\Lucene\Field class is not (properly) autoloaded, but the Zend\Search\Lucene\Document\Field from the vendor directory is autoloaded but doesn't have that method.
Can anyone tell me why the autoloading of the classes in the namespace EWZ\Bundle\SearchBundle from the root directory does not work?
All help is highly appreciated.
The class is properly autoloaded. The problem is with the code itself.
When you're initiating a $binaryField by code
$binaryField = Field::Binary('Binary', 'value');
the result is of class \Zend\Search\Lucene\Document\Field, not of \EWZ\Bundle\SearchBundle\Lucene\Field.
Because it executes
return new self($name, $value, '', true, false, false, true);
inside of \Zend\Search\Lucene\Document\Field, and this runs a constructor of \Zend\Search\Lucene\Document\Field, not your extended class.

Selenium PHPUnit_Extensions_Selenium2TestCase_Element::fromResponseValue()

I'm following examples from the book PHPUnit Essentials by Zdenek Machek and I'm on the section about functional tests using Selenium.
I already downloaded the server, the webdrivers and the firefox IDE.
The test class exported by Firefox IDE:
class SeleniumFirstTest extends PHPUnit_Extensions_Selenium2TestCase {
protected function setUp() {
$this->setBrowser('firefox');
$this->setHost('127.0.0.1');
$this->setPort(4444);
$this->setBrowserUrl('https://www.google.com.br/');
}
public function testCasephp()
{
$this->url("/?gws_rd=ssl");
$this->byId("lst-ib")->value("phpunit");
$this->byLinkText("Documentation")->click();
$this->byLinkText("13. PHPUnit and Selenium")->click();
$result = $this->byCssSelector("h1.title")->text();
$this->assertEquals("Chapter 13. PHPUnit and Selenium", $result);
}
}
When I try to run the tests in NetBeans I get the following output:
"C:\bin\phpunit.bat" "--colors" "--log-junit" "C:\Users\MARCOS~1.PRA\AppData\Local\Temp\nb-phpunit-log.xml" "--configuration" "C:\WebServer\Apache 2.2\htdocs\DataBaseTests\tests\phpunit.xml" "C:\Program Files\NetBeans 8.0.2\php\phpunit\NetBeansSuite.php" "--run=C:\WebServer\Apache 2.2\htdocs\DataBaseTests\tests"
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.
Configuration read from C:\WebServer\Apache 2.2\htdocs\DataBaseTests\tests\phpunit.xml
E......
Time: 25.44 seconds, Memory: 5.75Mb
There was 1 error:
1) SeleniumFirstTest::testCasephp
Argument 1 passed to PHPUnit_Extensions_Selenium2TestCase_Element::fromResponseValue() must be of the type array, null given, called in C:\WebServer\Apache 2.2\htdocs\DataBaseTests\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php on line 136 and defined
C:\WebServer\Apache 2.2\htdocs\DataBaseTests\tests\Marco\Test\Functional\SeleniumFirstTest.php:26
C:\WebServer\Apache 2.2\htdocs\DataBaseTests\tests\Marco\Test\Functional\SeleniumFirstTest.php:26
C:\Dev\PHPUnitTest\vendor\phpunit\phpunit\src\TextUI\Command.php:152
C:\Dev\PHPUnitTest\vendor\phpunit\phpunit\src\TextUI\Command.php:104
FAILURES!
Tests: 7, Assertions: 14, Errors: 1.
Generating code coverage report in HTML format ...teste done
Done.
I can't understand why the test is failing, considering it passes when executed on the Firefox IDE.
May someone help me to understand this?
Thanks

phpunit running exception when run a very very simple unit test

I get phpunit and install it as this link using the simplest way for test purposes. I just download the phpunit.phar file, chmod & rename & move to /usr/local/bin
Then, I run phpunit --version, its ok.
I write a simple php test case.
class SimpleTest extends PHPUnit_Framework_TestCase {
public function testSomething(){
$this -> assertTrue(true);
}
}
In terminal , I go to my php class folder, and execute
phpunit --colors SimpleTest
Now I got the exceptions
PHP ReflectionException: Method suite does not exist
in phar:///usr/local/bin/phpunit/phpunit/Runner/BaseTestRunner.php on line 113
PHP Stack trace:
PHP 1. {main}() /usr/local/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main($exit = *uninitialized*)
/usr/local/bin/phpunit:612
PHP 3. PHPUnit_TextUI_Command->run($argv = array (
0 => '/usr/local/bin/phpunit',
1 => '--colors',
2 => 'SimpleTest.php'),
$exit = TRUE)
phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:129
PHP 4. PHPUnit_Runner_BaseTestRunner->getTest(
$suiteClassName = 'SimpleTest',
$suiteClassFile = '/home/kevin/Workspace/php/laravel/app/tests/SimpleTest.php',
$suffixes = array (0 => 'Test.php', 1 => '.phpt'))
phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:150
PHP 5. ReflectionClass->getMethod('suite')
phar:///usr/local/bin/phpunit/phpunit/Runner/BaseTestRunner.php:113
PHPUnit 3.7.27 by Sebastian Bergmann.
Anything is welcome, thanks .
It looks like this error comes from an xdebug setting.
The solution appears to be adding this line to your php.ini file (or changing your the existing value to 0):
xdebug.show_exception_trace = 0
Take a look at
PHPUnit ReflectionException Method suite does not exist and
Why does PHPUnit hide my xdebug backtrace? for more info.

PHPUnit fatal error 'call to undefined function xcache_get()' AWS SDK & Symfony 2

I'm trying to get PHPUnit up and running in my Symfony 2 app, but I'm running into a problem I can't seem to figure out.
When I run my unit test a Fatal Error is returned, this does not happen inside of a normal browser.
Test function
$client = static::createClient(array(), array(
'PHP_AUTH_USER' => 'test',
'PHP_AUTH_PW' => 'test',
));
$crawler = $client->request('GET', '/test/url');
Error in console
$ phpunit -c app/
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /Acme/app/phpunit.xml.dist
PHP Fatal error: Call to undefined function xcache_get() in /Acme/vendor/aws-sdk-for-php/lib/cachecore/cachexcache.class.php on line 58
Fatal error: Call to undefined function xcache_get() in /Acme/vendor/aws-sdk-for-php/lib/cachecore/cachexcache.class.php on line 58
Line error refers to (In AWS SDK) L 58 is L 3 in this excerpt
public function read()
{
if ($data = xcache_get($this->id))
{
$data = $this->gzip ? gzuncompress($data) : $data;
return unserialize($data);
}
return false;
}
Any help much appreciated :-)
phpunit is using different ini file that does not load this extension
Try running php --ini from console, compare it with phpinfo() output from the browser to see what ini files are being loaded (most probably your web server runs on different user and may have it's own ini file)
Possible ways to fix it:
Editing ini used by cli and adding necessary options
By setting PHPRC enviroment variable to point to MAMP ini file
Create symlink to the MAMP ini file in php bin folder
Edit phpunit file and add -c path/to/ini after php binary

Categories