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.
Related
I am trying to use aspect mock with codeception tests.
From their documentation it is not clear how to configure.
https://github.com/Codeception/AspectMock
Include AspectMock\Kernel into tests/_bootstrap.php.
I do not have such file. Should I create it? Where should I include it?
My directory structure of codeception is:
test/codeception/acceptance.
I have file SummaryCest.php in test/codeception/acceptance.
Since I do not have _bootstrap.php file, I decided to try in SummaryCest - before declaring a class:
include __DIR__.'/../../../vendor/autoload.php'; // composer autoload
$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
'debug' => true,
'includePaths' => [__DIR__.'/../../../'],
'excludePaths' => [__DIR__.'../../../vendor'],
'cacheDir' => '/tmp/datamanager',
]);
I do not know do I really need to exclude vendor directory, but I saw such suggestions. If that is mandatory, it should be written probably in readme which I did not see.
In includePaths there should be visible all my project files.
I have function in SummaryCest.php
public function correctSummaryCounts(AcceptanceTester $I)
{
\AspectMock\Test::double(SummaryController::class, ['get' => null]);
}
and when I run test
php codecept.phar run test/codeception/acceptance/SummaryCest.php
I get message
==== Redirecting to Composer-installed version in vendor/codeception ====
Codeception PHP Testing Framework v2.3.5
Powered by PHPUnit 6.2.4 by Sebastian Bergmann and contributors.
PHP Fatal error: Uncaught Error: Class 'Go\ParserReflection\ReflectionFile' not found in /var/www/warehouseDataManager/vendor/codeception/aspect-mock/src/AspectMock/Intercept/BeforeMockTransformer.php:16
Stack trace:
#0 /var/www/warehouseDataManager/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php(124): AspectMock\Intercept\BeforeMockTransformer->transform(Object(Go\Instrument\Transformer\StreamMetaData))
#1 /var/www/warehouseDataManager/vendor/goaop/framework/src/Instrument/Transformer/CachingTransformer.php(83): Go\Instrument\Transformer\CachingTransformer->processTransformers(Object(Go\Instrument\Transformer\StreamMetaData))
#2 /var/www/warehouseDataManager/vendor/goaop/framework/src/Instrument/ClassLoading/SourceTransformingLoader.php(134): Go\Instrument\Transformer\CachingTransformer->transform(Object(Go\Instrument\Transformer\StreamMetaData))
#3 /var/www/warehouseDataManager/vendor/goaop/framework/src/Instrument/ClassLoading/SourceTransformingLoader.php(101): Go\Instrument\ClassLoading\SourceTran in /var/www/warehouseDataManager/vendor/codeception/aspect-mock/src/AspectMock/Intercept/BeforeMockTransformer.php on line 16
Can you explain me how to configure this?
Also I saw in readme
$userModel = test::double('UserModel', ['tableName' => 'my_users']);
but test is not even found. So I tried to use \AspectMock\Test which is at least found.
Notice that the error is throw before even running my test function. When I tried running before class declaration
$kernel->init();
it already gives same error.
_bootstrap.php files are no longer created automatically by Codeception.
To enabled them you have to add
settings:
bootstrap: _bootstrap.php
to codeception.yml file
and manually create _bootstrap.php files in tests directory and in every suite.
http://codeception.com/docs/reference/Configuration
ReflectionFile issue looks like autoloading issue.
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.
I am using symfony and phpunit for my tests.
I had a project in foodmeup.dev directory which I copied into foodmeupnew.dev
Now, when making a call to phpunit from phpStorm command line, I've got something strange with the autoload file being looked for in the previous folder.
See the following, at line 8 it's changing dir from foodmeupnew.dev to foodmeup.dev, how can I correct this ?
> D:\Divers\Programmation\Web\foodmeupnew.dev\bin\phpunit.bat -c app
Fatal error: Cannot redeclare getLadybug() (previously declared in D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\raulfraile\ladybug-themes\Ladybug\Plugin\Extra\Resources\php\helpers.php:12) in D:\Divers\Programmation\Web\foodmeup.dev\vendor\raulfraile\ladybug-themes\Ladybug\Plugin\Extra\Resources\php\helpers.php on line 21
Call Stack:
0.0002 234304 1. {main}() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\phpunit:0
0.0088 1457264 2. PHPUnit_TextUI_Command::main() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\phpunit:36
0.0088 1463816 3. PHPUnit_TextUI_Command->run() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:99
0.0088 1466536 4. PHPUnit_TextUI_Command->handleArguments() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:109
0.0132 2056112 5. PHPUnit_TextUI_Command->handleBootstrap() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:575
0.0134 2065160 6. PHPUnit_Util_Fileloader::checkAndLoad() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:750
0.0136 2065352 7. PHPUnit_Util_Fileloader::load() D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\Util\Fileloader.php:36
0.0183 3407040 8. include_once('D:\Divers\Programmation\Web\foodmeup.dev\app\bootstrap.php.cache') D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\Util\Fileloader.php:52
0.0185 3409304 9. require_once('D:\Divers\Programmation\Web\foodmeup.dev\app\autoload.php') D:\Divers\Programmation\Web\foodmeup.dev\app\bootstrap.php.cache:5
0.0187 3411320 10. require('D:\Divers\Programmation\Web\foodmeup.dev\vendor\autoload.php') D:\Divers\Programmation\Web\foodmeup.dev\app\autoload.php:9
0.0191 3424808 11. ComposerAutoloaderInit751ca4f7e73ded3907cdc8698c543ac6::getLoader() D:\Divers\Programmation\Web\foodmeup.dev\vendor\autoload.php:7
0.0214 3630760 12. composerRequire751ca4f7e73ded3907cdc8698c543ac6() D:\Divers\Programmation\Web\foodmeup.dev\vendor\composer\autoload_real.php:45
EDIT:
I've taken the following measure to make sure foodmeup.dev was not hardcoded :
- find and replace in content of every file including hidden and system files the string 'foodmeup.dev' by 'foodmeupnew.dev'
- delete entire directory, create new one, pull git branch, run composer install, reset bootstrap.cache
same error happens again. I'm sure there might be some misconfiguration but I can find out where it's coming from. any clue ?
EDIT2
OK, I figured out that line 5 is loading phpunit configuration that contains the hardcoded 'foodmeup.dev' value.
Where does this configuration come from... that is to find out
> $phpunit - D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:566
============================================================================================================
array(
[colors] => string(4) "auto"
[backupGlobals] => bool(false)
[bootstrap] => string(64) "D:\Divers\Programmation\Web\foodmeup.dev\app\bootstrap.php.cache"
)
EDIT 3
Digging into it, I figured out that in file D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:566 the realpath function applied to a relative path would render a different path.
var_dump($this->arguments['configuration'] . '/phpunit.xml');
string(15) "app/phpunit.xml"
var_dump(realpath($this->arguments['configuration'] . '/phpunit.xml'));
realpath($this->arguments['configuration'] . '/phpunit.xml') - D:\Divers\Programmation\Web\foodmeupnew.dev\vendor\phpunit\phpunit\src\TextUI\Command.php:529
================================================================================================================================================================ string(56) "D:\Divers\Programmation\Web\foodmeup.dev\app\phpunit.xml"
How can that be that the realpath function renders a path with foodmeup.dev when the call is made from a file located in the foodmeupnew.dev directory ?
EDIT 4:
I figured out this is a phpStorm issue. there must be a global variable which sets the dir for php function. If I run the command from an external command line, the error does not arise
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
From what I've read, it seems like I should be able to set up a folder, e.g. tests/ , put a few files in it with unit test classes, and then run phpunit on that file and have it find and run the tests.
For whatever reason, in my installation (on OS X), it thinks the folder tests/ is a file, or so it would seem:
$ ls tests
test1.php test2.php
$ phpunit tests/test1.php
PHPUnit 3.5.3 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 5.00Mb
There was 1 failure:
1) FailingTest::testFail
Your test successfully failed!
/Users/****/tmp/tests/test1.php:4
FAILURES!
Tests: 1, Assertions: 0, Failures: 1.
$ phpunit tests/test2.php
PHPUnit 3.5.3 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 5.00Mb
OK (1 test, 1 assertion)
$ phpunit tests
PHP Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Neither "tests.php" nor "tests.php" could be opened.' in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php:102
Stack trace:
#0 /usr/local/PEAR/PHPUnit/TextUI/Command.php(157): PHPUnit_Util_Skeleton_Test->__construct('tests', '')
#1 /usr/local/PEAR/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#2 /usr/local/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#3 {main}
thrown in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php on line 102
Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Neither "tests.php" nor "tests.php" could be opened.' in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php:102
Stack trace:
#0 /usr/local/PEAR/PHPUnit/TextUI/Command.php(157): PHPUnit_Util_Skeleton_Test->__construct('tests', '')
#1 /usr/local/PEAR/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#2 /usr/local/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#3 {main}
thrown in /usr/local/PEAR/PHPUnit/Util/Skeleton/Test.php on line 102
I have what I hope is a fairly standard installation of phpunit via PEAR, following these instructions http://www.newmediacampaigns.com/page/install-pear-phpunit-xdebug-on-macosx-snow-leopard, on OS X Snow Leopard.
$ pear version
PEAR Version: 1.9.1
PHP Version: 5.3.2
Zend Engine Version: 2.3.0
Running on: **** 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386
$ phpunit --version
PHPUnit 3.5.3 by Sebastian Bergmann.
I'm hoping someone else out there ran into this issue and it's just a simple fix, or else I'm just doing something wrong?
It is not a bug, it is a feature.
You have a directory full of .php files, in your case they all contain testcases.
But as your testsuite grows, you will likely want to have other php files inside tests directory, files that do not contains tests, that exist solely to support tests. Those files should never be executed by PHPUnit itself.
This is a very common scenario.
So how would PHPUnit know which files it needs to run and which ones not?
Checking the file name suffix is one option for doing it - by default PHPUnit considers everything with name ending with Test.php as being a test and ignores everything else.
You can change that behaviour if you really want to - by creating a file named phpunit.xml in your tests directory with the following content
<?xml version="1.0" encoding="utf-8" ?>
<phpunit>
<testsuite name='Name your suite'>
<directory suffix='.php'>./</directory>
</testsuite>
</phpunit>
Once you have done that, PHPUnit will run all files with '.php' at the end of the file name
(in this context file extension is considered to be part of the file name)
But it really is better to get used to the convention and name your tests accordingly.
The simpler way to running test on folder is to add "Test.php" at the end all of your tests and run phpunit specifing your folder like this
phpunit .
or
phpunit your_test_folder/.
Annoying little quirk, but I figured it out.
At least with the default configuration, test files have to end with "Test.php", eg. fooTest.php, or they are not found by the test runner.