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.
Related
I am developing a Laravel package, I have the main project and the package folder inside the same folder.
code
/mainproject
/package
Inside my composer.json in the main project I have:
"repositories": [
{
"type": "path",
"url": "../package"
}
],
running composer require for that package works fine and the code works, but then when I make any changes inside of the package, the changes aren't reflected, and so I have to use composer to remove, then re-require it.
I am using Laravel Sail as my local development environment and setting up the repository in the way that I did has created a sym-link in the vendor folder of the main project.
is there additional setup required when doing package development inside Laravel Sail?
This is a normal behaviour, you must run composer update your/package every time you make a change, so composer tracks all of that. This is due to how it works internally.
In one workplace, I have created some code as packages so it is easier to edit and I do not end up with a giant monolith, so I have created "local" packages like you.
Everytime you have done a change, you must run composer update your-package/name.
What I have done to keep it going normally is adding "version": "1.2.3" to my package's composer.json, like this:
{
"name": "my/package",
"description": "My package",
"keywords": ["my", "package"],
"version": "1.2.3",
So, when I have done a change, I would just bump it like 1.2.4 or whatever the desired number would be following the correct semver, and in my laravel's composer.json I would have:
{
"require": {
"my/package": "^1.0.0"
}
}
So, any time I made a change (and updated the version on the package), I would just run composer update my/package, so I have a new composer.lock using this new reference, that would update everything in composer.
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.
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
I have tried to publish a project with composer. The project resides at github, and are published through packagist.org.
But when I try to create my project with composer it fails with this error message:
"Could not find package madskullcreations/simplicity with stability
stable."
I use the following command:
composer create-project madskullcreations/simplicity
The composer.json contains this:
{
"name": "madskullcreations/simplicity",
"description": "Website made simple!",
"homepage": "https://madskullcreations.com",
"type": "project",
"license": "GPL-3.0-or-later",
"minimum-stability": "dev",
"require": {
"php": ">=5.6",
"cakephp/cakephp": "3.5.*"
}
}
My repository contains just one file for testing. What is wrong? I tried to remove the dependencies, the entire "require"-block, but no real change...
Beginner as I am, I don't even know where I would define the packages "stability", can't find anything at github or at packagist.
Please help me get this started!
Working solution:
I eventually got it working with the help from Flying, see his answer further down. Since I think it is a wee bit complicated to get composer up'n working, I try to put the steps I did to get it working here:
Create a repository at github.
Create a composer.json with your depencencies. Check it in.
Release it. There are a "Releases" link somewhere, use it and give the release a name.
Now, to skip the packagist.org step during your testing, follow these steps. It is not good style to publish a non-working solution (like I did) on packagist.org, and it's no fun at all to do all the steps necessary ten times over.
Create a local folder somewhere, and create a new composer.json file there.
Put something like this in it:
{
"require": {
"madskullcreations/simplicity":"dev-master#dev"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/madskullcreations/simplicity"
}
]
}
Run the following command in your new folder:
composer create-project
It should now download and install your project.
And, read the error messages given by composer and make sure you understand them. They are useful. My headache was a missing PHP-extension (intl) and that I assumed it was using php version 7.1, while it actually listened to my requirement in the composer.json file, and used v5.6. (I have several php-versions installed in iis, but my fuzzy head did not consider that.)
Packages stability requirement is defined into minimum-stability setting of composer.json of your project, not a composer.json of the external package.
Your madskullcreations/simplicity package have no releases defined so the only branch that is available in it - is dev-master "release" (it can be seen at the right side of package page on Packagist). This "release" have "dev" stability level.
Because of above if you're requiring this package into your project without either setting minimum-stability: dev or without specifying stability requirement for a package as
"require": {
"madskullcreations/simplicity":"dev-master#dev"
}
(notice #dev into version requirement) it is correct behavior of Composer to complain about lack of compatible releases.
Also it is generally bad practice to publish your test packages into public registry like Packagist. Instead you should use direct repository specification into your composer.json as explained here. In your case it will be:
"repositories": [
{
"type": "vcs",
"url": "https://github.com/madskullcreations/simplicity"
}
]
After specifying direct repository reference - it will be safe to remove your test package from Packagist unless you're really want to share it with open source community.
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.