setting phpunit command from the console symfony - php

I have already posted this question on php unit first test symfony
I installed phpunit via the composer as a per project installation.
When trying vendor/bin>phpunit -c ../../app every thing is ok and I get a positive answer.
Whereas this command give the answer to all the tests in the tests directory.
But I want the result to every test alone.
When trying /vendor/bin>phpunit -c ../../src/xxx/Bundle/tests/entity/yyy.php and I get the following message : could not load c:\wamp\www\symfony\src/xxx/Bundle/tests/entity/yyy.php Parse PI : PI php never end ... Start ttag expected, '<' not found
and when trying /vendor/bin>phpunit -c ../../src/xxx/Bundle/tests/entity/yyy and I get the following message : could not read "..\..\src/xxx/Bundle/tests/entity/yyy"
Could anybody help me to know how should I write the command and from where execute it???
Any ideas???

Don't use the -c option here. The -c option is a shortcut for --configuration and it points to the directory of a PHPunit configuration file (like app/phpunit.xml.dist). That configuration tells PHPunit where to look for the test classes and some other configuration, like the bootstrap file.
If you want to run tests for a specific test, you can do it like phpunit path/to/tests/MyTest.php. But you'll loose the autoloading then. To get that back, you can use the --bootstrap option to point to the bootstrap file. So it'll be phpunit --bootstrap vendor/autoload.php path/to/tests/MyTest.php.
If you want to run this command more often, you can better edit the app/phpunit.xml.dist file and create a new suite:
<?xml version="1.0" encoding="utf-8" ?>
<phpunit ...>
<!-- ... -->
<testsuites>
<testsuite name="MyBundle">
<file>path/to/tests/MyTest.php</file>
</testsuite>
</testsuites>
<!-- ... -->
</phpunit>
And then run: phpunit -c app --testsuite MyBundle

Happened with me too, but in fact we are misreading the documentation you are forgetting 'app' in the command line look:
phpunit -c app src/AppBundle/Tests/Util/CalculatorTest.php
Note the app parameter in command sentence.

Related

phpunit impossible to follow the installation process

I have a lot of problems trying to intall phpunit, maybe my knowledge isn't enought or the guide is very incomplete.
First, the install, I tried all the ways, globally, with "downloaded PHAR file directly" or with "sudo apt-get install phpunit" but when I tried to do:
$phpunit -v
bash: /usr/bin/phpunit: No chuch file or directory
if I do:
$ ll /usr/local/bin (I know, the path is different, other unexplicable event)
-rwxr-xr-x 1 user user 2784899 abr 29 17:09 phpunit*
but
$ sudo phpunit --version
PHPUnit 7.1.5 by Sebastian Bergmann adn contributors.
ok, looks better, so I tried to make the first example
<?php
use PHPUnit\Framework\TestCase;
class StackTest extends TestCase
{
public function testPushAndPop()
{
$stack = [];
$this->assertSame(0, count($stack));
array_push($stack, 'foo');
$this->assertSame('foo', $stack[count($stack)-1]);
$this->assertSame(1, count($stack));
$this->assertSame('foo', array_pop($stack));
$this->assertSame(0, count($stack));
}
}
but it give me the next error:
PHP Fatal error: Class 'PHPUnit\Framework\Testcase' not found in /var/www/html/phpunit/index.php on line 4
I'm using Ubuntu 18 and php 7.2
Any idea?
When you are running PHPUnit from the command line, you also need to include a 'bootstrap' file - it can be as simple as the composer autoload.php file:
phpunit --bootstrap vendor/autoload.php
Longer term, that configuration would be put into the phpunit.xml file so it is read, and run automatically by PHPunit.
<!-- file: phpunit.xml
src/autoload.php would also include the ./vendor/autoload.php file
and do any other locally required setup -->
<phpunit bootstrap="src/autoload.php">
<testsuites>
<testsuite name="money">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Ok, I start to understand some things.
First #Sebastian Bergmann give me the clue, with this example, it works.
But if you start with the documentation you never found it. I think it's an error and can confuse a begginer like me.
Yet I can't install phpunit with PHAR or globally, maybe it could be a future new post.
Thanks for all

PHPUnit: bootstrap not being executed?

When running a test, my phpunit.xml is being loaded by PHPUnit. I can see it in the console as --bootstrap parameter, but it seems like it's not being executed?
In the xml I set bootstrap="bootstrap.php"
Is it normal that the xml file contents are echoed just below the line Testing started at... and just above PHPUnit 3.7.21 by Sebastian Bergmann.?
Any way in my bootstrap.php file I just put
die "bootstrap executed";
But never see that. What could be wrong?
Edit: This is the contents of phpunit.xml:
<phpunit bootstrap="./bootstrap.php">
</phpunit>
You need to launch the phpunit in one of the following example:
#for load a bootstrap php file (with full path and file extension)
>phpunit --bootstrap bootstrap.php
or
#for load with your xml configuration
>phpunit -c phpunit.xml
Hope this help

How to configure PHPUnit to test the whole vendor folder in a ZF2 application?

I'm developing a Zend Framework 2 application with a common folder structure, so that the folder /vendor contains all (project external) libraries. Setting up the unit testing environment I would like to be able to run all vendor tests. The folders structures are different depending on the library. Some packages have no tests at all.
A possible solution would be to create a test suite "vendor" and manually define there the paths to every single test folder, e.g.:
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit ...>
<testsuites>
<testsuite name="vendor">
<directory>../vendor/lib-foo/path/to/tests</directory>
<directory>../vendor/package-bar/path/to/tests</directory>
...
</testsuite>
...
</testsuites>
...
</phpunit>
I don't like this solution. First of all because then I'd have to handle every package manually.
Another solution would be to define /vendor as test folder:
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit ...>
<testsuites>
<testsuite name="vendor">
<directory>../vendor</directory>
...
</testsuite>
...
</testsuites>
...
</phpunit>
Well, but then PHPUnit has to scan a lot of folders, that it doesn't need, and the tests will need much more time.
Is there a better solution, that would make possible to automate the process and avoid much manual configuration?
It would probably be difficult to run all PHPUnit vendor test suites with a single test run. One issue is that each of the different test suites might ship its own configuration file or even require a custom bootstrap configuration file. You cannot cover that when running all test suites with a single command.
I'd probably use some shell magic for this. Note that this example relies on the presence of a phpunit.xml(.dist) file in each of your 3rd party packages (for most libraries that's a reasonable assumption). You could even integrate this into your continuous integration process to test this continuously:
for FILE in $(find . -name 'phpunit.xml*') ; do
sh -c 'cd '$(dirname $FILE)' && composer install'
vendor/bin/phpunit -c $FILE
done

Automatic load a Listener in PHPUnit

Is there a way to automatically load a Listener (or phpunit configuration file for that matter), without using no more than:
phpunit testdir
? Today I'm using:
phpunit -c phpunit.xml --bootstrap bootstrap.php testdir
and want to exclude all switches. I know that I could have a phpunit.xml file in every directory, but thats not an option..
Thanks in advance!
PHPUnit by itself should look in the current directory for a phpunit.xml file. So your first and second example should both find and include the same phpunit.xml.
Use a Makefile:
unittest:
phpunit -c phpunit.xml --bootstrap bootstrap.php testdir
And simply call it with make unittest
Now you can add more for different cases, in the same pattern.

phpunit memory_limit parameter does not apply

I've just installed phpunit via pear in a mac osx 10.7 and everything works fine except I got memory limit errors (xdebug enabled for reports).
I tried to add the -d memory_limit=512M parameter to phpunit but it is not applying because, on the very first error, I added var_dump(ini_get('memory_limit')); exit; and it prints string(3) "32M"
So, why is it not being applied?
Besides that, if I run
php -d memory_limit=256M -r "echo ini_get('memory_limit');"
it echoes "256M"
Is it possible that phpunit is not executing same php?
For those who reach this thread and need to get the configuration for phpunit.xml configuration file:
<php>
<ini name="memory_limit" value="512M" />
</php>
More on section "Setting PHP INI settings, Constants and Global Variables" at
https://phpunit.de/manual/6.5/en/appendixes.configuration.html
Yes you can set every php option with phpunit -d that can be set with ini_set.
You already opened a bug over in the phpunit bug tracker but well I'm going for the more verbose answer here
Reproduce to show it works in general:
echo "<?php var_dump(ini_get('memory_limit')); " > foo.php
phpunit -d memory_limit=12M --bootstrap foo.php
Produces:
string(3) "12M"
PHPUnit 3.6.5 by Sebastian Bergmann.
But phpunit only applies this option once before the first test is run!
So chances are your code is somewhere changing the memory limit back to 32M which is something phpunit can't "fix".
Same goes for setting the memory limit in the phpunit.xml file.
If you don't mind about the speed of the tests, found this thread very useful as it's better handling of memory for your app.
Basically; In your phpunit.xml, under the <phpunit> tag, set the processIsolation to true i.e
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
...
processIsolation="true"
... >
Alternatively,
You can just disable memory limiting altogether under the <php> tag, set the memory_limit to -1 i.e
<php>
...
<ini name="memory_limit" value="-1"/>
...

Categories