Class from PHAR not found - php

I want to use the PHAR from nette-tester V1.7.1 ... and build a simple script that runs a test, but i fail at a very basic level.
The class Tester\Runner\CliTester is correct and available within the PHAR, but is not found.
Composer is no option and including all files without the phar would we the worst option.
Input:
<?php
include_once 'tester.phar';
$tester = new Tester\Runner\CliTester;
die($tester->run());
Output:
Fatal error: Class 'Tester\Runner\CliTester' not found in
C:\webdev\testrunner.php on line 14 Call Stack: 0.0000 136168 1. {main}()
C:\webdev\testrunner.php:0

If you want to simply run CliTester you just need to run php tester.php in your command line.
If you include tester.phar into your application some files are not loaded. Look at stub.php in tester.phar.

Related

PHPUnit Plugin in Netbeans 8.2 gives Fatal error: Class 'PHPUnit_Framework_TestSuite' not found when trying to run a test case

When I try to run PHPUnit in Netbeans I face this error:
Fatal error: Class 'PHPUnit_Framework_TestSuite' not found in C:\Users\julian\AppData\Roaming\NetBeans\8.2\phpunit\NetBeansSuite.php on line 63
Done.
This happens both in Netbeans and CLI.
I started debugging this problem by navigating to this directory: C:\Users\julian\AppData\Roaming\NetBeans\8.2\phpunit\.
That directory contained one file: NetBeansSuite.php. I opened it to look for clues, and saw this line:
class NetBeansSuite extends PHPUnit_Framework_TestSuite {
What I didn't see is any concrete PHPUnit_Framework_TestSuite class.
What's next is that the NetBeansSuite.php file doesn't have any include or require language constructs that may include the PHPUnit_Framework_TestSuite class.
So, in order to fix the fatal error problem I should include the PHPUnit_Framework_TestSuite in NetbeansSuite.php.
This is a problem because I'm not the author of NetbeansSuite.php.
On top of that the author of NetbeansSuite.php wrote this in the comment section:
<b>WARNING: User changes to this file should be avoided.</b>
I read further in the comments:
* Copyright 2010 Oracle and/or its affiliates. All rights reserved.
I guess the NetbeansSuite.php file is outdated.
Searching on the net brought me to this stackoverflow question:
Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...?
They claim that using a namespaced version of PHPUnit_Framework_TestCase should fix the problem. So I did what they wrote.
I stubbornly changed NetBeansSuite.php.
Old code:
class NetBeansSuite extends PHPUnit_Framework_TestSuite {
New code:
require_once 'PHPUnit/Autoload.php';
use PHPUnit\Framework\TestCase;
class NetBeansSuite extends TestCase {
I tried to run the test case again, and this was the unfortunately result:
Fatal error: Declaration of CalculatorTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in C:\wamp64\www\test\CalculatorTest.php on line 97
In other words, it gave me a new problem.
My system:
Windows 10
WAMP
php 7.2.14
PHPUnit 8.0.6
Netbeans version 8.2 (Netbeans PHPUnit plugin installed through Tools > Plugins. Version: 0.26.2)
My question is: does anybody know how the NetBeansSuite.php file should be like with the system described above?
The setUp method in my test case:
protected function setUp()
{
$this->object = new Calculator;
}
Update: The Skeleton Generator project is abandoned.
See link: https://github.com/sebastianbergmann/phpunit-skeleton-generator
So in order to fix the Declaration of CalculatorTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp() error the corresponding return type declaration should be used in the test case.
// I added : void
protected function setUp(): void
{
$this->object = new Calculator;
}
// : void needs to be added in the tearDown method as well
protected function tearDown(): void
Unfortunately, this gave me new problems:
Warning: require_once(PHPUnit/Autoload.php): failed to open stream: No such file or directory in C:\wamp64\www\test\CalculatorTest.php on line 4
Fatal error: require_once(): Failed opening required 'PHPUnit/Autoload.php' (include_path='.;C:\php\pear') in C:\wamp64\www\test\CalculatorTest.php on line 4
I solved this by manually installing PEAR and creating a new "PHPUnit" directory in C:\php\PEAR. Then I created a new Autoload.php file. I filled the content of Autoload.php with a PSR-4 example file found at: https://www.php-fig.org/psr/psr-4/.
This solved the Autoload problem, but I faced a new problem during the execution of a test case.
C:\wamp64\www\test>phpunit CalculatorTest.php
PHPUnit 8.0.6 by Sebastian Bergmann and contributors.
Time: 170 ms, Memory: 10.00 MB
No tests executed!
It shows No tests executed! but I have 5 tests.
I'll make a new question for this.
Here it is: PHPUnit 8 installed on Windows through PHAR shows No tests are executed
Initially indeed you should update your namespaces since it has been changed in more recent versions of PHPUnit.
The error however indicates that your CalculatorTest::setUp() method is not compatible with the PHPUnit version. Could you maybe post that method here?
For the same issue, I directly changed the file "NetBeansSuite.php" (C:\Users\USERNAME\AppData\Roaming\NetBeans\8.2\phpunit) as following :
class NetBeansSuite extends \PHPUnit\Framework\TestSuite {
After that, CLI and Netbeans was working.

phpStorm : wrong path returned by realpath function

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

PHPUNIT error when running tests with PHPSTORM

Running phpunit tests using PhpStorm.
Receiving this error:
/usr/local/php5/bin/php
/private/var/folders/m8/k61mmmmj7g732j3pd0_91s0c0000gn/T/ide-phpunit.php
--configuration /Users/psteinheuser/unity/test/phpunit.xml DatabaseDumperTest
/Users/psteinheuser/unity/test/DatabaseDumperTest.inc Testing started
at 11:36 AM ...
Fatal error: Class IDE_PHPUnit_Framework_TestListener contains 1
abstract method and must therefore be declared abstract or implement
the remaining methods (PHPUnit_Framework_TestListener::addRiskyTest)
in
/private/var/folders/m8/k61mmmmj7g732j3pd0_91s0c0000gn/T/ide-phpunit.php
on line 504
Call Stack:
0.0013 340096 1. {main}() /private/var/folders/m8/k61mmmmj7g732j3pd0_91s0c0000gn/T/ide-phpunit.php:0
PHP Fatal error: Class IDE_PHPUnit_Framework_TestListener contains 1
abstract method and must therefore be declared abstract or implement
the remaining methods (PHPUnit_Framework_TestListener::addRiskyTest)
in
/private/var/folders/m8/k61mmmmj7g732j3pd0_91s0c0000gn/T/ide-phpunit.php
on line 504
Have found previous posts which have said this is fixed, though I seem to have the correct versions where it should work.
PHPStorm 7.1.3
phpunit 3.7.28
php 5.4.24
The OS is Mac 10.9.2
Have removed and re-installed PHPStorm, upgraded php, re-installed phpunit, restarted apache, rebooted, scratched my head, etc.
Running the phpunit tests manually from the terminal, works fine.
Looking at Preferences within PHpStorm, it seems to be pointing to phpunit correctly.
I'm thinking it's a permission or path issue, but I don't know where to look next.
Appreciate any input or direction.
In PHPStorm, the only way I was able to solve this was by manually updating the contents of the jar file where this error exists.
We're both on Mac OSX 10.9, so these instructions should work for you.
Begin by making a copy of the existing jar file:
cp /Applications/PhpStorm.app/plugins/php/lib/php.jar /Applications/PhpStorm.app/plugins/php/lib/php.jar.orig
Now, create a temporary directory to hold your jar file's contents and extract
mkdir ~/jarfiles
cd ~/jarfiles
jar xf /Applications/PhpStorm.app/plugins/php/lib/php.jar
Once extracted, you'll need to edit the phpunit.php file to add the missing method stub
vi scripts/phpunit.php
Around line 310 in my file is a class 'IDE_PHPUnit_Framework_TestListener'. Insert the following method stub:
public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time){}
Now just re-pack your jar and replace it into your PhpStorm:
jar cf0 php.jar ./*
mv php.jar /Applications/PhpStorm.app/plugins/php/lib/php.jar
That fixed it for me. Hope it fixes it for you too. Good luck!

Class 'PharData' not found

I get this error on my production server (CentOS 5.4 and php 5.3.5) :
Warning: include_once(PharData.php): failed to open stream: No such
file or directory in /var/www/ZendFramework/library/Zend/Loader.php on
line 146
Warning: include_once(): Failed opening 'PharData.php' for inclusion
(include_path='/var/www/fw:/var/www/vmms:/var/www/ZendFw/library:.:/usr/share/pear:/usr/share/php')
in /var/www/ZendFw/library/Zend/Loader.php on line 146
Fatal error: Class 'PharData' not found in
/var/www/vm/app/Backup.php on line 40
And this is the code which fail :
$phar = new PharData($imageBackupFile);
$phar->buildFromDirectory($imageDir);
Logger::info("Image directory backed up to: $imageBackupFile");
This code is working fine on my own computer.
PharData should be included by default in php 5.3+ ...
Thanks for your help!
UPDATE :
I am using the Zend Auto loader feature to load the good php files using this code :
require_once("Zend/Loader/Autoloader.php");
$autoloader = Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
Zend autoloader is doing the include_once(PharData.php).
Just because Phar is bundled by default in PHP 5.3 doesn't mean that it's necessarily included in your install. When you build PHP with ./configure, you can pass the --disable-phar to disable the Phar extension.
To confirm this, run the following script:
<?php
phpinfo();
?>
One of the first sections to appear will be the Configure Command section. Review this section to see if the --disable-phar switch is present, and if there is a Phar section to the page in general.
If it's not present, you'll need to contact your host to have it enabled. There's a decent chance, however, that they won't do it for you since it could impact other users depending on how their servers are set up. If this is on your own machine, you'll need to either rebuild PHP without that switch, or install Phar manually from PECL (no idea if this would still work in 5.3, but I don't see why it wouldn't).

Runkit : does not work on a linux server

I have a problem with PECL::Runkit with this little example
index.php contain <?php
runkit_import('a.php');
runkit_import('b.php');
Doublon::get();
a.php et b.php each contain the same code
class Doublon
{
static function get() { echo "class " . __FILE__; }
}
On my PC (Windows XP, Wampserver 2, php 5.2.9-2, runkit DLL bundled with
Wamp) it work and index.php show
class C:\wamp2\www\utilitaires\essais\runkit\b.php
On my Linux CentOS 5 server, PHP 5.2.10, Runkit compiled by hand
Warning: runkit_import() [function.runkit-import]: class doublon not
found in /shares/public/cedric/test/index.php on line 2
Warning: runkit_import() [function.runkit-import]: Cannot redeclare
class doublon in /shares/public/cedric/test/index.php on line 2
Warning: runkit_import() [function.runkit-import]: class doublon not
found in /shares/public/cedric/test/index.php on line 3
Warning: runkit_import() [function.runkit-import]: Cannot redeclare
class doublon in /shares/public/cedric/test/index.php on line 3
Fatal error: Class 'Doublon' not found in
/shares/public/cedric/test/index.php on line 4
One problem : runkit's make test give me 100% of tests failed, but I still don't know why.
The runkit version from the linux distribution just make crash Apache :
PHP Startup: Timezone database is corrupt
I dropped xdebug, return to php 5.2.9, but the errors are the same
Thanks in advance, Cédric
The Package site says:
WARNING: 0.9 does not compile with PHP 5.2+ so use the CVS version instead.
Are you using the CVS version?
The up-to-date runkit extension can be found on http://github.com/zenovich/runkit
Anyway, as I know, runkit never had a feature to define new class on importing. It only can add or change members of existing classes. If you really want this, you can open the feature-request on http://github.com/zenovich/runkit
To determine why you get different results on your platforms, I need to know the versions of runkit and PHP for both of them. You can get all the information using the command 'php -i'.

Categories