I'm using PHPUnit Bridge in my Symfony project. I'm currently using PHPUnit 7 and I want to upgrade to PHPUnit 8.
In PHPUnit 8 array subset assertions are deprecated and generate warnings. I want to use dms/phpunit-arraysubset-asserts package to provide them. With regular PHPUnit I'd just composer require it and call it a day.
Now, the Bridge doesn't have original PHPUnit as its dependency, instead it installs it to a temporary folder, patches it and runs from there. phpunit-arraysubset-asserts has PHPUnit dependency though and generates a warning:
Adding phpunit/phpunit as a dependency is discouraged in favor of Symfony's PHPUnit Bridge.
* Instead:
1. Remove it now: composer remove --dev phpunit/phpunit
2. Use Symfony's bridge: composer require --dev phpunit
I don't want to install phpunit/phpunit to avoid confusion.
I've tried to ignore it by adding a * replacement, but just adding the replacement to composer.json produces a Composer error:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- __root__ No version set (parsed as 1.0.0) conflicts with symfony/phpunit-bridge[v5.0.5].
- symfony/phpunit-bridge v5.0.5 conflicts with __root__[No version set (parsed as 1.0.0)].
- Installation request for __root__ No version set (parsed as 1.0.0) -> satisfiable by __root__[No version set (parsed as 1.0.0)].
- Installation request for symfony/phpunit-bridge v5.0.5 -> satisfiable by symfony/phpunit-bridge[v5.0.5].
What's the correct way to add a PHPUnit extension when using PHPUnit Bridge?
WARNING I do not know phpunit-bridge well (in fact today was my first day with it), but I can see the way to achieve what you want. It's not that convenient, but possible. I think it's better to either check with symfony team (maybe there is a supported way for this) or just use plain phpunit (it seems easier and it should be possible to integrate it with symfony's phpunit-bridge without relaying on bridge to install phpunit itself)
# create new symfony project
symfony new gronostaj-phpunit
# cd in it
cd gronostaj-phpunit
# install phpunit-bridge
symfony composer req phpunit-bridge
# to change phpunit version to 9.5
vim phpunit.xml.dist
# install phpunit in symfony bridge way
bin/phpunit
# to update phpunit config
bin/phpunit --migrate-configuration
# black magic begins here
cd bin/.phpunit/phpunit-9.5-0
# to bypass `autoload-dev` in later dump-autoload
mkdir -p tests/_files ; touch tests/_files/CoverageNamespacedFunctionTest.php tests/_files/CoveredFunction.php tests/_files/NamespaceCoveredFunction.php
# install phpunit plugin
composer require --no-plugins dms/phpunit-arraysubset-asserts
# cd back to the root of project
cd -
# create test
--- phpunit/gronostaj-phpunit ‹master* M?› » cat tests/SubsetTest.php
<?php
declare(strict_types=1);
namespace App\Tests;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use DMS\PHPUnitExtensions\ArraySubset\Assert;
use PHPUnit\Framework\TestCase;
class SubsetTest extends TestCase
{
use ArraySubsetAsserts;
public function testWithTrait(): void
{
$expectedSubset = ['bar' => 0];
$content = ['bar' => 0];
self::assertArraySubset($expectedSubset, $content, true);
}
public function testWithDirectCall(): void
{
$expectedSubset = ['bar' => 0];
$content = ['bar' => 0];
Assert::assertArraySubset($expectedSubset, $content, true);
}
}
# run test
--- phpunit/gronostaj-phpunit ‹master* M?› » bin/phpunit
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
Testing
.. 2 / 2 (100%)
Time: 00:00.006, Memory: 8.00 MB
OK (2 tests, 2 assertions)
# done
For phpunit 8, use phpunit verion 8.0 in phpunit.xml.dist and install your plugin like this composer require --no-plugins 'dms/phpunit-arraysubset-asserts:*', that will work.
To make it reproducible for others in your team you will need to provide composer scripts (to create dummy files and install plugin, but it should do the trick) and hook them into something like post-install and post-update events.
I have been struggling with this issue as well in the past, and found this tip in the PHPUnit Bridge documentation:
It is also possible to require additional packages that will be
installed along the rest of the needed PHPUnit packages using the
SYMFONY_PHPUNIT_REQUIRE env variable. This is specially useful for
installing PHPUnit plugins without having to add them to your main
composer.json file.
So basically, edit phpunit.xml.dist and declare the env variable under the existing <php> tag:
<php>
<server name="SYMFONY_PHPUNIT_REQUIRE" value="dms/phpunit-arraysubset-asserts" />
</php>
To add multiple packages, use a space delimiter:
<php>
<server name="SYMFONY_PHPUNIT_REQUIRE" value="dms/phpunit-arraysubset-asserts spatie/phpunit-snapshot-assertions" />
</php>
Related
I followed both the Codeception and general PHP instructions in our wiki when I set up my project in PhpStorm. It worked perfectly with both the ./vendor/bin/codecept run and docker-compose run --rm codecept run ui_automation --env staging --debug for over a day. Now, when I run the same project, it attempts the first test and just hangs at this part:
julie#Julies-MacBook-Pro qa-tests % ./vendor/bin/codecept run
Codeception PHP Testing Framework v4.1.9
Powered by PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
Running with seed:
Ui_automation Tests (2) --------------------------------------------------------------------
------------------------------------------------------------------------
- LoginCest: Test login
My environment is as such:
php 7.4.12
Mac OS Catalina 10.15.6
Composer 2.0.5
Codecept 4.1.11
Docker 19.03.13
And when I try to run it with the docker command I get this error. I was originally getting this error with the above command too but now I'm just getting it for the docker command:
julie#Julies-MacBook-Pro qa-tests % docker-compose run --rm codecept run ui_automation --
env staging --debug
Starting qa-end-to-end-tests_db_1 ... done
Creating qa-end-to-end-tests_codecept_run ... done
==== Redirecting to Composer-installed version in vendor/codeception. You can skip this
using --no-redirect ====
Command "run " is not defined.
I wasn't doing anything other than adding locators when this problem suddenly appeared. Also I used to verify the flow through a VNCView window and I can see there that my project is pulling up Chrome but not going to any page.
https://github.com/Codeception/Codeception/issues/5495 says about the redirect line in the error, "This redirect is here for a reason - it prevents odd errors when different versions of Codeception or dependencies are installed in vendor dir and external locations and some classes are loaded from one place and some - from another." But, it doesn't give tips on how to resolve this.
I initially was following two sets of instructions, I may have set up composer twice (once with wget and once with homebrew) and tried fixing from that angle. I also was setting up my mac in it's entirety the same day so it's possible I downloaded something else that is somehow conflicting.
I have tried the following:
Using both PhpStorm and VSCode, both IDEs display the same behavior
Running ./vendor/bin/codecept clean and .vendor/bin/codecept build
Updating with "composer update" command
restarting IDEs, computer
Deleted composer according to these instructions: Remove composer
Re-installed composer with homebrew only
Uninstalled and reinstalled docker according to these instructions: https://nektony.com/how-to/uninstall-docker-on-mac
Added codeception/verify and codeception/specify to my composer.json
Made sure the URL the page is supposed to go to is still in my yaml file under Webdriver, URL
Noticed a "No CLI interpreter" in my Edit Configuration page in IntelliJ so re-added that and set PHP 7.4 to Interpreter). This was using the executable /usr/local/Cellar/php/7.4.12/bin/php
Looked in preferences and module settings, didn't see anything weird
Ran in verbose mode (-vvv) didn't get hints from that
Blew out my whole git repo and re-imported it
Ran composer show, saw many codeception packages
Ran autoload dump
Ran Tools > Composer > Diagnose in both regular and verbose and got back "Composer diagnosed some issues in ./composer.json." but it doesn't tell me what those issues are and there is nothing lit up or red in composer.json
Deleted the whole vendor folder and reinstalled composer
I now notice after the last step composer.phar is red! I have tried repeating the steps above but it is still red.
Right now I'd just like to get my project working through the regular ./vendor/bin/codecept run bc I have SREs to help with the Docker stuff, although I'd be happy to skip straight to docker working as that's how they'd like us to run it.
My composer.json (without the specify, verify troubleshooting step):
{
"require-dev": {
"codeception/robo-paracept": "^0.4.2",
"codeception/codeception": "^4.1",
"codeception/module-phpbrowser": "^1.0.0",
"codeception/module-asserts": "^1.0.0",
"codeception/module-webdriver": "^1.1",
"codeception/verify": "2.1.0",
"codeception/specify": "1.4.0",
"phpunit/phpunit": "^9.4"
},
"require": {
"ext-zip": "^1.15",
"codeception/specify": "*",
"codeception/verify": "*"
},
"autoload": {
"psr-4": {
"Tests\\Support\\": "tests/_support",
"UiAutomationTester\\": "tests/_support/UiAutomation.php"
}
}
}
I updated composer to 4.1.11 and verified that ./vendor/bin/codecept run -V matched the version in composer.json
composer -V says it's at Composer version 2.0.5
Results of composer-show:
codeception/lib-asserts 1.13.2 Assertion methods used by
Codeception core and Asserts module
codeception/lib-innerbrowser 1.3.4 Parent library for all Codeception framework modules and PhpBrowser
codeception/module-asserts 1.3.1 Codeception module containing various assertions
codeception/module-phpbrowser 1.0.2 Codeception module for testing web application over HTTP
codeception/module-webdriver 1.1.3 WebDriver module for Codeception
codeception/phpunit-wrapper 9.0.5 PHPUnit classes used by Codeception
codeception/robo-paracept 0.4.2 Codeception Parallel Execution Tasks via Robo Task Runner
codeception/stub 3.7.0 Flexible Stub wrapper for PHPUnit's Mock Builder
consolidation/annotated-command 4.2.3 Initialize Symfony Console commands from annotated command class methods.
consolidation/config 1.2.1 Provide configuration services for a commandline tool.
consolidation/log 2.0.1 Improved Psr-3 / Psr\Log logger based on Symfony Console components.
consolidation/output-formatters 4.1.1 Format text by applying transformations provided by plug-in formatters.
consolidation/robo 1.4.13 Modern task runner
consolidation/self-update 1.2.0 Provides a self:update command for Symfony Console applications.
container-interop/container-interop 1.2.0 Promoting the interoperability of container objects (DIC, SL, etc.)
dflydev/dot-access-data v1.1.0 Given a deep data structure, access data by dot notation.
doctrine/instantiator 1.3.1 A small, lightweight utility to instantiate objects in PHP without invoking their constr...
grasmash/expander 1.0.0 Expands internal property references in PHP arrays file.
grasmash/yaml-expander 1.4.0 Expands internal property references in a yaml file.
guzzlehttp/guzzle 7.2.0 Guzzle is a PHP HTTP client library
guzzlehttp/promises 1.4.0 Guzzle promises library
guzzlehttp/psr7 1.7.0 PSR-7 message implementation that also provides common utility methods
league/container 2.4.1 A fast and intuitive dependency injection container.
monolog/monolog 2.1.1 Sends your logs to files, sockets, inboxes, databases and various web services
myclabs/deep-copy 1.10.1 Create deep copies (clones) of your objects
nikic/php-parser v4.10.2 A PHP parser written in PHP
phar-io/manifest 2.0.1 Component for reading phar.io manifest information from a PHP Archive (PHAR)
phar-io/version 3.0.2 Library for handling version information and constraints
php-webdriver/webdriver 1.8.3 A PHP client for Selenium WebDriver. Previously facebook/webdriver.
phpdocumentor/reflection-common 2.2.0 Common reflection classes used by phpdocumentor to reflect the code structure
phpdocumentor/reflection-docblock 5.2.2 With this component, a library can provide support for annotations via DocBlocks or othe...
phpdocumentor/type-resolver 1.4.0 A PSR-5 based resolver of Class names, Types and Structural Element Names
phpspec/prophecy 1.12.1 Highly opinionated mocking framework for PHP 5.3+
phpunit/php-code-coverage 9.2.3 Library that provides collection, processing, and rendering functionality for PHP code c...
phpunit/php-file-iterator 3.0.5 FilterIterator implementation that filters files based on a list of suffixes.
phpunit/php-invoker 3.1.1 Invoke callables with a timeout
phpunit/php-text-template 2.0.4 Simple template engine.
phpunit/php-timer 5.0.3 Utility class for timing
phpunit/phpunit 9.4.2 The PHP Unit Testing framework.
psr/container 1.0.0 Common Container Interface (PHP FIG PSR-11)
psr/http-client 1.0.1 Common interface for HTTP clients
psr/http-message 1.0.1 Common interface for HTTP messages
psr/log 1.1.3 Common interface for logging libraries
ralouphie/getallheaders 3.0.3 A polyfill for getallheaders.
sebastian/cli-parser 1.0.1 Library for parsing CLI options
sebastian/code-unit 1.0.8 Collection of value objects that represent the PHP code units
sebastian/code-unit-reverse-lookup 2.0.3 Looks up which function or method a line of code belongs to
sebastian/comparator 4.0.6 Provides the functionality to compare PHP values for equality
sebastian/complexity 2.0.2 Library for calculating the complexity of PHP code units
sebastian/diff 4.0.4 Diff implementation
sebastian/environment 5.1.3 Provides functionality to handle HHVM/PHP environments
sebastian/exporter 4.0.3 Provides the functionality to export PHP variables for visualization
sebastian/global-state 5.0.2 Snapshotting of global state
sebastian/lines-of-code 1.0.2 Library for counting the lines of code in PHP source code
sebastian/object-enumerator 4.0.4 Traverses array structures and object graphs to enumerate all referenced objects
sebastian/object-reflector 2.0.4 Allows reflection of object attributes, including inherited and non-public ones
sebastian/recursion-context 4.0.4 Provides functionality to recursively process PHP variables
sebastian/resource-operations 3.0.3 Provides a list of PHP built-in functions that operate on resources
sebastian/type 2.3.1 Collection of value objects that represent the types of the PHP type system
sebastian/version 3.0.2 Library that helps with managing the version number of Git-hosted PHP projects
symfony/browser-kit v5.1.8 Symfony BrowserKit Component
symfony/console v4.4.16 Symfony Console Component
symfony/css-selector v5.1.8 Symfony CssSelector Component
symfony/dom-crawler v5.1.8 Symfony DomCrawler Component
symfony/event-dispatcher v4.4.16 Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.9 Generic abstractions related to dispatching event
symfony/filesystem v4.4.16 Symfony Filesystem Component
symfony/finder v5.1.8 Symfony Finder Component
symfony/polyfill-ctype v1.20.0 Symfony polyfill for ctype functions
symfony/polyfill-mbstring v1.20.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php73 v1.20.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80 v1.20.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/process v4.4.16 Symfony Process Component
symfony/service-contracts v2.2.0 Generic abstractions related to writing services
symfony/yaml v4.4.16 Symfony Yaml Component
theseer/tokenizer 1.2.0 A small library for converting tokenized PHP source code into XML and potentially other ...
webmozart/assert 1.9.1 Assertions to validate method input/output with nice error messages.
I don't know much about PHP set up or Codeception, this is my first project in either. This is running on everyone else's machine in QA except mine!
I have a Github project in Scrutinizer, that has an optional recommended package in its composer.json.
I would like to install this optional package during the Scrutinizer build, but could not find any information on how to do so in the Scrutinizer config. While the package is optional, Scrutinizer detects a number of bugs because the dependency is not present, and some of the unit tests will only run if it is present.
Is it possible to run custom composer commands, or to have additional composer packages installed?
The related package is Mistralys/application-utils.
Looking through scrutinizer configurations of other projects, I was able to find out how to run custom composer commands.
To require additional packages, these can be added in the dependencies:
build:
dependencies:
before:
- composer require vendor/package-name:version
It is also possible to override scrutinizer's composer command entirely, for example to run a script:
build:
dependencies:
override:
- composer run-script scriptname
In my case, because the suggested package also requires the source package back (cyclic dependency), I had to set the root version, like this:
build:
dependencies:
override:
- COMPOSER_ROOT_VERSION=dev-master composer require mistralys/application-localization:dev-master
I followed the instructions on the official PHPUnit page to install PHPUnit 6.
composer require --dev phpunit/phpunit ^6.0
However, If I go to the project folder and execute phpunit --version then I get PHPUnit 3.7.21 by Sebastian Bergmann..
Why is PHPUnit 3.7.21 installed instead of PHPUnit 6?
I suppose you have xampp installed? It comes with PHPUnit 3.x.x preinstalled with PEAR which strangely can not be uninstalled with pear uninstall. And since its config is in php root folder, that 3-ish version has priority when running phpunit command (even if you have phpunit installed globally) in CMD or PS. How to fix:
Go to xampp/php folder and delete two files phpunit (with no extension) and phpunit.bat. Usually it's enough to prevent older PHPUnit version from being run but let's be on the safer side:
Go to xampp/php/PEAR dir and delete two folders PHPUnit and PHPUnit2.
Go to Control Panel, then navigate System and Security->System and click "Advanced system settings" link on the left. This will open Advanced tab in System Properties window. Click "Environment Variables" button, then under System Variables select "Path" variable and click "Edit..." button. In the new window, click "New" button and type your path to vendor/bin folder (if you already installed PHPUnit for your project there will be phpunit.bat file). By default for xamp it looks like this: C:\xampp\htdocs\yourprojectname\vendor\bin.
Restart your Command Prompt (or PowerShell) window, then type phpunit --version to see the that it's PHPUnit 6.x.x now.
P.S. (If you want latest PHPUnit do not use ^6.0 in require string, since it will install v6.0.0, write it in composer.lock file that will never update PHPunit to the latest e.g. 6.2.1 version atm). Just use
composer require --dev phpunit/phpunit
to install the latest stable version for your project.
You run your global PHPUnit version which is installed in another folder. To get the installed version you have to go to the vendor/bin folder.
vendor/bin/phpunit --version
PHPUnit 6.0.8 by Sebastian Bergmann and contributors.
In newer versions you can run it with bin/phpunit there should be the executable. When you need another PHP-Version then define it before php74 bin/phpunit.
If you use XAMPP, why don't you update XAMPP version of global PHPUnit to desired.
1.
Read this post, which quickly explains what to do:
StackOverflow post
2.
You can get phar file for version you need here:
proper PHPUnit phar file
3.
Which version of PHPUnit you need depends on your PHP version and you can identify it here:
which phar file version you need
Just click on links: PHPUnit 9 - PHPUnit 8 - PHPUnit 7 - PHPUnit 6 - PHPUnit 5 - PHPUnit 4 and read description explaining which PHP version it works with.
I just installed a fresh Laravel 5 project, my first one on this version. PHPUnit is supposed to be out of the box with the framework and every tutorials I saw just say to type phpunit within the project folder to launch the Unit Tests.
I checked and PHPUnit is in the composer.json, I also did a composer install and composer update just in case it wouldn't be here
website(master)$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Removing phpunit/phpunit (4.6.1)
- Installing phpunit/phpunit (4.6.2)
Downloading: 100%
But it just doesn't work phpunit isn't recognized at all
website(master)$ phpunit
-bash: phpunit: command not found
Seems like nobody got this problem before as I Googled it. I hope I'm not doing any stupid mistake. Any idea or suggestion ? Thanks guys ;)
I didn't install PHPUnit globally and didn't define the path. So for anyone who would have same problem :
composer global require phpunit/phpunit
composer global require phpunit/dbunit
Then you add this to you ~/.bash_profile or ~/.profile
export PATH=~/.composer/vendor/bin:$PATH
This occurs when you don't have phpunit installed globally.
Run this command to use the local version (installed with composer):
vendor/bin/phpunit
in windows machine the command is different please use this command
php vendor/phpunit/phpunit/phpunit
orignal source
You can run this command in cmd before running phpunit command:
doskey phpunit="vendor/bin/phpunit"
And if you are lazy as I am, you can run this one:
doskey pu="vendor/bin/phpunit"
for people who have WINDOWS 7, use the .\vendor\bin\phpunit command instead of ./vendor/bin/phpunit
Run the command
composer config --list --global | grep -w home
You can find the find the [home] with composer path, similar to this one.
[home] /home/example_username/.config/composer
The path ~/.config/composer is where composer global packages are installed. Next run the command...
export PATH=~/.config/composer/vendor/bin:$PATH
I made a permanent link to my phpunit like this
echo 'alias phpunit=vendor/bin/phpunit' >> ~/.bash_aliases
now phpunit is working by itself and stays even after I restart the terminal
Include this line on your composer.json
"phpunit/phpunit": "4.0.*",
Run composer update.
You should be able to run the following command on your Laravel directory.
vendor/bin/phpunit
I'm using Homestead to serve my Laravel application. I'm trying to run PHPUnit. According to the docs:
An example test file is provided in the app/tests directory. After
installing a new Laravel application, simply run phpunit on the
command line to run your tests.
Well, when I'm "simply running" phpunit in my project root (inside the Homestead environment) I get this:
The program 'phpunit' is currently not installed.
Do I need to install PHPUnit separately then? The documentation does not mention it. What am I doing wrong?
You can install it globally on the system using.
composer global require phpunit/phpunit
However, if you need different versions for different projects this can cause issues.
The alternative option is to use the version installed as part of your dependencies by referencing the path to your vendor directory.
./vendor/bin/phpunit
You could even add an alias to your aliases file in your ~/Homestead directory. That way you're always using the phpunit version that is installed with your project dependencies.
alias phpunit=./vendor/bin/phpunit
You'll need to restart the homestead box to make use of the alias.
You can install it globally with:
$ composer global require "phpunit/phpunit=4.4.*"
# then use
$ phpunit
or you can use it with your local composer:
$ composer require "phpunit/phpunit=4.4.*"
# then
$ vendor/bin/phpunit
Since it's a package required for development, Laravel provide PHPunit(require-dev section in composer), you should find it in vendor's folder :
$ your_app/vendor/bin/
You can run the command from the root of your app folder by typing :
$ vendor/bin/phpunit
I hope it will help !