I am working on a project for a WebApp and I would like to specify types for my class properties. I didn't realise this is a PHP version 7.4.* feature, so I read this and updated my composer.json to include the relevant material:
{
"name": "srmes/shopping-app-test",
"description": "an assignment from `scandiweb.com`. A simple php-based web application to display and inventory a range of products",
"require-dev": {
"phpunit/phpunit":"~9.0",
"squizlabs/php_codesniffer": "~3.0"
},
"require": {
"doctrine/orm": "~2.7",
"php": "7.4.4"
},
"config": {
"platform": {
"php": "7.4.4"
}
},
"autoload" : {
"psr-4": {
"WebApp\\": "src/"
}
},
"autoload-dev" : {
"psr-4": {
"WebApp\\Tests\\" : "tests/"
}
}
}
I then ran composer install and composer update.
No problems seemed to occur with the install, except that now my phpunit tests don't run:
PHPUnit 9.1.1 by Sebastian Bergmann and contributors.
Time: 58 ms, Memory: 4.00 MB
No tests executed!
And all composer commands give the following error:
Parse error: syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /Users/ScottAnderson/Documents/Tech/commissions/shopping_app_test/vendor/ocramius/package-versions/src/PackageVersions/Installer.php on line 33
Ironically this is syntax exception about the php feature I was trying to utilise! I can't even run composer -vvv to debug which php executable is being used by composer.
In order to resolve this should I use a package like phpbrew to make directory environments of php?
My assumption was that after requiring php 7.4.4 in composer.json that the correct php executable would be installed and used by composer and phpunit
Looks like you are not running php 7.4. and the dependencies installed (here phpunit and PackageVersion) need it.
Sometimes composer runs commands hooked on events and if the command fails everything else can fall.
Delete the vendor folder.
Delete composer.lock
Remove the constraint on php 7.4 version in your composer.json
Run composer update
You should be alright 🤗
Btw composer manages project dependencies, not php versions installed.
If you're starting out in PHP I'd recommend to stick with 7.3 which is widely available. Honestly you don't need the latest syntax additions to the language.
Related
I've got a problem with PhpStorm, composer and PHPUnit.
Windows 8.1 Pro (64 bit)
PhpStorm is up to date: 2018.2.2
Tried different PHP interpreters like XAMPP and a clean PHP for win
PHPUnit is required by composer with: "phpunit/phpunit": "^7.3.3".
PHPUnit is successfully installed via composer to the vendor directory as well.
PHPUnit is recognized from PhpStorm:
My test class extends the PhpUnit\Framework\TestCase class and when you run the test, the following happens:
First it seems like PhpStorm loads the old PHPUnit (3.7.21) from XAMPP's PHP and not the recognized PHPUnit (7.3.3) as setup in the PhpStorm settings / installed to the vendor folder.
But I don't think so.
I think PhpStorm tries to load the PhpUnit\Framework\TestCase class by the autoloader, but I don't know why it doesn't find the PHPUnit...
Thanks in advance!
The whole project could be minimized to this simple test class:
The composer.json looks like the following:
"autoload": {
"psr-4": {
"Flo\\Newsletter\\": "src/"
}
},
"require": {
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^7.3.3"
}
Namespaces are case-sensitive. Please change PhpUnit to PHPUnit in the import.
Although PhpStorm could really detect that. Here's a feature request for that: https://youtrack.jetbrains.com/issue/WI-38140
When I use composer to install packages in my project I am only able to do so if I update my json file manually.
For example, if I run the following command in Git-Bash in my project directory (which contains my composer.phar and composer.json file):
php composer.phar require php-di/slim-bridge
It returns the following error:
[Invalid Argument Exception]
Could not find package
php-di\slim-bridge at any version for your minimum-stability (stable).
Check the package spelling or your minimum stability.
However, if i were to just update my json file to the following (example I've provided contains multiple packages I am using in my project):
{
"require": {
"slim/slim": "^3.0",
"slim/twig-view": "^2.1",
"illuminate/database": "^5.2",
"respect/validation": "^1.0",
"slim/csrf": "^0.6",
"slim/flash": "^0.1",
"phpmailer/phpmailer": "^5.2",
"php-di/slim-bridge":"^1.0"
},
"autoload":{
"psr-4": {
"App\\": "app"
}
}
}
... And I run the command: $ php.composer.phar update
Everything installs to project correctly.
What is going on that I am not able to install packages using the require method thus making me resort to manually updating my json file each time?
Since I am using windows, I used the windows installer for composer rather than install through command line and I got this working correctly. Much much easier now since I don't have to update my JSON files manually.
Just updated composer on a functioning Behat, and now getting the following messages open running behat.
PHP Deprecated: "Symfony\Component\Console\Helper\DialogHelper" is deprecated since version 2.5 and will be removed in 3.0.
Im using Behat-3 (~3.0#dev).
Any advice will be appreciated :)
I don't know what your composer.json looks like but I would suggest you to use stable versions for packages in require blocks. Try to use specific versions when possible.
"require": {
"hello/world": "1.2.3",
"abc/cde": "~4.3.0",
"php": ">=5.4.0"
},
"require-dev": {
"similar/to-above": "1.11"
},
"require-test": {
"similar/to-above": "3.2.6"
},
On my computer I have a CakePHP Project. And inside the cakephp project I have a composer.json file like below:
{
"name": "a/b",
"description": "c",
"require-dev": {
"phpunit/phpunit": "3.7.*",
"phpunit/phpunit-selenium": ">=1.2"
},
"license": "Proprietary",
"authors": [
{
"name": "d",
"email": "e"
}
],
"minimum-stability": "f"
}
After running composer install I have phpunit and the selenium package installed and working here
/cakephpproject/vendor/bin/phpunit
/cakephpproject/vendor/phpunit/phpunit-selenium
Now from the /cakephpproject/app/ I tried to run the following command
../vendor/bin/phpunit Test/Case/Controller/MyControllerTest
But I am receiving the following error message
Fatal error: Class 'App' not found...
If I use ./Console/cake test app Controller/GranulesController the test does attempt to run but throws the PHPUnit_Extensions_Selenium2TestCase not found error since on the system PHPUnit Selenium is not installed.
How do I make cakephp use the phpunit & the phpunit-selenium I installed using composer and not use the phpunit installed globally?
The following command seems to be executing the PHPUnit & PHPUnit-Selenium2 I installed using Composer.
./Console/cake test app AllControllers --bootstrap ../vendor/autoload.php
I encountered a similar issue in CakePHP 2.x (it was fixed in the latest CakePHP 2.8.5).
CakePHP expects to find PHPUnit in one of the following places:
vendors/phpunit/phpunit
vendors/PHPUnit
vendors/phpunit.phar
app/Vendor/phpunit
app/Vendor/PHPUnit
app/Vendor/phpunit.phar
[Composer's global directory]/vendor/phpunit
[Composer's global directory]/vendor/PHPUnit
[Composer's global directory]/vendor/phpunit.phar
But Composer (by default) creates a directory called vendor for its files (note the lack of a plural).
This issue was recently fixed for CakePHP 2.x, but you can get the same behaviour by setting the COMPOSER_VENDOR_DIR environment variable to vendors.
I am trying to add a local project A as dependency to project B. Using git daemon I am able to fetch project A as dependency, but the dependencies defined with require in the composer.json in project A are not recognized. What am I missing?
project A:
{
"name": "project/a",
"require": {
"monolog/monolog": "dev-master"
}
}
project B:
"repositories": [
{
"type": "vcs",
"url": "git://localhost/home/user/project-a"
}
],
"require": {
"project/a": "dev-master"
}
result (in project B):
vendor/
project/a
expected:
vendor/
project/a
monolog/monolog
The most likely explanation is that you forgot to commit the changes to your composer.json in /home/user/project-a.
To debug this you can use composer show project-a dev-master -v. The -v will output more verbose info while it loads the repository, and then you will see details about the version you are installing, if it does not contain the monolog require, then I would say my guess above was correct. If it does contain it, we got a serious bug in composer and you should report it on github.
I encountered a similar issue and my issue was that I was running composer update instead of composer install and one of the libraries that I required defined some of its dependencies as zipballs from GitHub.