Packaging PHPUnit tests as a PHAR archive? - php

Is it possible to package PHPUnit tests as a PHAR archive, and run them using phpunit?
I've created a .phar with the follow script:
<?php
$cPhar = new Phar('mytests-archive.phar', 0);
$cPhar->addFile('mytest.php');
$sStub = <<<ENDSTUB
#! /usr/bin/php
<?php
Phar::mapPhar('mytest-archive.phar');
require 'phar://mytests-archive.phar/mytest.php';
__HALT_COMPILER();
ENDSTUB;
$cPhar->setStub($sStub);
$cPhar->compressFiles(Phar::GZ);
$cPhar->stopBuffering();
?>
But when I then try running the resulting archive as follows:
phpunit mytests-archive.phar
I get the error message:
#! /usr/bin/php
PHPUnit 3.3.17 by Sebastian Bergmann.
Class MyTestClass could not be found in /path/to/mytests-archive.phar
Does PHPUnit not support PHAR files, or am I missing a step in my build script? (This is my first attempt at using PHAR)

I don't think PHPUnit understands tests which are in a PHAR archive. PHPUnit doesn't just interpret the passed file and run tests; it reads the source of the test to be run and then executes it. So when it goes looking for the source of MyTestClass, it can't find it as it's wrapped up inside of the archive.

Related

Namespace that seems it doesn't exist is working but why?

I have this exercice folder that gave me instructions on how to install the web application:
So this folder contains a .sh file that installs the necessary things:
npm install
curl -o phpunit -L https://phar.phpunit.de/phpunit-7.phar
chmod +x phpunit
./phpunit --version
So basically this adds a file called phpunit inside root folder. So by far I kind of understand what's going on, so this installs node dependencies and also installs phpunit (I think so), but this project has a test example unit:
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class HelloWorldTest extends TestCase
{
public function testWhenGetDataFromHelloWorld(): void
{
$this->assertEquals('Hello World', 'Hello World');
}
}
At this point is where I don't get how is use PHPUnit\Framework\TestCase; working, since I don't have any folder called PHPUnit, I get that use is something like importing a class, but still, how is this working (when I run the test it does not give me any kind of error)?, also, it appears that vscode detects the import as an error because it seems that it doesn't exist.
My file tree is as follows:
Can someone explain me, please?
This is because all the dependencies are in the phpunit-7.phar file, which was named phpunit by that script.
"The easiest way to obtain PHPUnit is to download a PHP Archive (PHAR) that has all required (as well as some optional) dependencies of PHPUnit bundled in a single file."
PHPUnit - Installing PHPUnit - PHP Archive (PHAR)

How to run phpunit in laravel 5.5

I have a problem running phpunit in my laravel 5.5. I already go to vendor/bin and then execute phpunit using my command prompt in windows. But cmd just give another option or flag as shown in the picture below :
I have read laravel 5.5 documentation for unittest. It's said that we just need to execute phpunit {as shown in https://laravel.com/docs/5.5/testing}
Then I tried this :
How to run single test method with phpunit?
phpunit --filter testBasicTest tests\Unit\ExampleTest
As shown below :
BUt it's said that 'cannot open the file'. Then I Tried
phpunit ExampleTest, but still cannot open the file. So what's wrong here...?
Thanks in advance
Note :
Here's my phpunit.xml :
You need to run phpunit without getting inside the bin folder.
Try this:
vendor/bin/phpunit
This will load your phpunit.xml file. Otherwise it cannot load your configuration file. Unless if you don't give spesific path:
vendor/bin/phpunit --configuration /path/to/laravels/phpunit.xml
Just install phpunit through composer:
composer global require phpunit/phpunit
then all you are set to test your laravel project.

laravel 5.1 phpspec command not found

I'm trying to run phpspec but when I do a command phpspec run inside root project folder it just prints:
-bash: phpspec: command not found
But when I do composer show -i it shows it's installed (because it's in package.json):
phpspec/php-diff v1.0.2 A comprehensive li...
phpspec/phpspec 2.5.3 Specification-orie...
phpspec/prophecy v1.6.1 Highly opinionated...
What's wrong?
You probably need to add the path to the executable to your $PATH or, when you call it...call it by the full path to phpspec (something like ./vendor/bin/phpspec run)

Running PHP Unit from Terminal - just not working

I have installed PHP Unit and done everything the instructions tell me.
I'm told that I can just run:
$ phpunit
...and it will work. I may need to specify the config file and I can do that.
I have changed the directory to the PHP Unit folder, but nothing.
I am using Mac's Terminal if this means anything.
I have tried (with the error shown):
$ phpunit - $: command not found
phpunit - phpunit: command not found
php phpunit - ...doesn't return an error, but doesn't do return anything
% phpunit - fg: %: no such job
Also, the folder structure is like this:
- phpunit
- php-code-coverage
- php-file-iterator
- php-text-template
- php-timer
- php-token-stream
- phpunit
- phpunit-mock-objects
phpunit.php
- PHPUnit (folder)
- various things inside
... what am I actually running? The outer folder, inner folder or the phpunit.php file?
I must be doing something wrong?
My question: How'd you install PHPUnit? By "hand" (download it and just unzip it), via PEAR, ...?
Try executing (inside the phpunit folder):
./phpunit
Make sure the path where the phpunit executable lies is added to your $PATH variable.
A similiar question on SO on how to install phpunit can be found here, general installation instructions here.

How to unittest symfony2 core

After a fresh symfony2 install i can run phpunit -c app/ and phpunit tests the included demo application: OK (1 test, 1 assertion).
But i receive no output (even with verbosity) when i run phpunit -c vendor/symfony/ as described here: http://symfony.com/doc/2.0/contributing/code/tests.html.
Does anyone know how to make this work?
PHPUnit: 3.6.2
PHP: 5.3.8
Symfony: 2.0.5
Testing twig, doctrine and other plugins works as expected (although doctrine tests fail for some reason).
If you have no output it's maybe because you configured php not to display errors.
You must install the vendors using the vendors.php script before lauching the Symfony test suite:
$ php vendor/symfony/vendors.php
Are you running the tests using the provided phpunit.xml.dist configuration file?
From your projects root directory:
$ phpunit --configuration app/phpunit.xml.dist vendor/symfony/tests
That should add an autoloader for vendors.

Categories