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)
Related
I am using ALE for my PHP developing.
There is possibility to use phpstan as one of linters, but no matter what I do there is notification that phpstan cannot load class / method definitions:
Function foo not found while trying to analyse it - autoloading is probably not configured properly.
Default configuration of ALE unfortunately does not help, because it expects phpstan to be in $PATH, and even if it is there it still complains about missing autoloading.
This solution is git and composer specific.
I have not found good solution without either any vcs or composer.
So first of all we assume that there is composer being used in project, so there is autoload.php file generated. If you are not using this feature you probably should, because it maps classes to files and makes autoloading simple.
Unfortunately using global phpstan binary is futile. This is because of phpstan looks in current directory for autoload or neon config file that tells it where to look for class definitions. So if you don't want to be forced to open each file in project from root project's directory, then there should be another solution.
Using phpstan binary installed by composer in your project (in vendor/bin/phpstan) solves this problem. Local binary is using the same autoload.php and correctly finds each class definition. So we need to tell ale where phpstan binary is.
Inserting in vimrc file (or better in .vim/ftplugin/php.vim) following line:
let g:ale_php_phpstan_executable = system('if ! type git &> /dev/null; then echo phpstan; else PSE=`git rev-parse --show-toplevel 2> /dev/null`/vendor/bin/phpstan; if [ -x "$PSE" ]; then echo -n $PSE; else echo phpstan; fi; fi')
It tells ALE path to phpstan executable, which is determined by running shell comand.
if ! type git part checks if there is git command in system. If not, then default phpstan text is being returned to variable.
git rev-parse --show-toplevel is trying to find out whether we are in git repository and what is it's top level directory. When found correctly, it determines path to phpstan by adding /vendor/bin/phpstan to top level directory. This is where locally installed phpstan should reside.
If there is no such file or it is not executable, then default phpstan variable value is being returned.
I am trying to run the default testcase of laravel 4 for an app. Phpunit works when I explicitly mention the testfile path but doesn't seem to work otherwise, furthermore it returns 255 code when I try to run Phpunit only.
Any idea?
Checkout your phpunit.xml file, there you will find bootstrap path to autoloader.
Are you using global PHPUNit framework or you are using local (installed with composer) ?
I created my vim mapping so it is pointing always to current file:
nmap ,t :!clear && phpunit %<esc>
I have the following example test code (PHP4) I need to run with phpunit:
<?php
require_once 'PHPUnit/Framework.php';
class RemoteConnectTest extends PHPUnit_Framework_TestCase
{
public function setUp(){ }
public function tearDown(){ }
public function testConnectionIsValid()
{
$this->assertTrue(true);
}
}
?>
which does not run with phpunit:
PHP Fatal error: require_once(): Failed opening required 'PHPUnit/Framework.php' ...
How do I need to setup my environment to make this work (Linux, Ubuntu 12.04)? Do I need to set a search path? Change php.ini? I AM NOT ABLE TO CHANGE THE TEST CODE ITSELF.
The answer is an incompatibility between the used versions. Newer versions of phpunit do have a different internal setup (see example for 3.7 here) which are different from older phpunit versions.
One need to 'downgrade' phpunit, as example as follows:
sudo pear uninstall phpunit/PHPUnit
sudo pear install phpunit/PHPUnit-3.2.8
to install version 3.2.8, for example. It can be verified the existance of the file Framework.php:
> ls /usr/share/php/PHPUnit
Extensions Framework Framework.php Runner TextUI Util
The example code should work now when called as
phpunit SimpleTest.php
(assuming identical name of class and file [excluding the php of course]).
Since you can run phpunit from command line, you most likely have the source code installed with PEAR already (or composer, which is less likely). But path to framework is not in global include paths (or in system variable PATH).
The path to PHPUnit is usually inside PEAR. And PEAR is usually near php installation. You can always use global filesearch in terminal with
find / -name 'Framework.php' 2>/dev/null
You either need to either
edit php.ini and add path to PHPUnit in include_path directive
require_once absolute path
use autoloading (but thats only from PHP 5.3)
I am using Symfony2, everything is installed, my tests work so good so far.
I'd like to get an autocompletion of PHPUnit's methods.
Symfony's WebTestCase class extends from PHPUnit_Framework_TestCase just like below:
abstract class WebTestCase extends \PHPUnit_Framework_TestCase
The parent class is highlighted as not existing although.
How can I tell my IDE to use PHPUnit's library?
I am using PHPStorm
PHPUnit is available by path /Users/myUser/pear/share/pear/PHPUnit/
Add it as a library... in the project that you are editing add it to 'External Libraries'.
It should then be included.
For me (Ubuntu 12.04) it was adding this folder as external library:
/usr/share/php/PHPUnit
For PHPStorm users, go to File -> Settings -> Project Settings ->PHP and add the path in there.
PhpStorm 2016.2 introduces a feature which - in this case - is also a bug.
Autocompletion now no longer includes static methods as an option when in $this-> context. (https://blog.jetbrains.com/phpstorm/2016/07/completion-changes-in-phpstorm/#more-10425)
As phpunit tests are defined as static methods but called via $this->, autocomplete for phpunit is now effectively broken.
They've rolled back this change for phpunit in the next EAP (https://youtrack.jetbrains.com/issue/WI-32530).
Workaround until the next stable release: Press CTRL-Space twice; this will then show static methods in the autocomplete field.
You can add package phpunit/phpunit to the require-dev section of your composer.json file.
After running composer install, PHPStorm will know about the PHPUnit classes.
I have OSX, phpunit installed by homebrew and phpstorm 9.0
So how it works for me: open preferences or press cmd+, -> Languages&Frameworks -> PHP -> Include path -> add "/usr/local/Cellar/phpunit/4.7.6/libexec/"
I have PHPStorm 2017.1.4 and my OS is Ubuntu 16.04. I already have phpunit.phar installed in my /usr/local/bin.
I will use ~/WORK/.. for the example paths but you should use the full path /home/myname/WORK/..
What I did is just go to a folder inside my home(like: ~/WORK/) and run:
composer require phpunit/phpunit
After composer was done downloading phpunit I added a new project include path to:
~/WORK/vendor/phpunit/phpunit/src
Now I have all of the PHPUnit autocompletion, I can jump into PHPUnit's source code directly and I can keep the PHPUnit's code updated with composer. I also removed the phpunit.phar from /usr/local/bin and replaced it with a link to ~/WORK/vendor/bin/phpunit
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.