Composer - how to include test cases - composer-php

I want to use the following composer package "spatie/laravel-medialibrary" (https://github.com/spatie/laravel-medialibrary)
How to include it with test cases? Neither require nor require-dev work for me:
"require": {
"spatie/laravel-medialibrary": "^5.0"
},
or
"require-dev": {
"spatie/laravel-medialibrary": "^5.0"
},
I just want to run the testcases included in the package.

You need to install this package from source - dist archive does not contain test nor documentation. Add this to your composer.json:
"config": {
"preferred-install": {
"spatie/laravel-medialibrary": "source"
}
}
And then remove this package from vendor directory and install again:
rm -rf vendor/spatie/laravel-medialibrary/
composer install

Related

icanhazstring/composer-unused (installed globally) contains a Composer plug in which is blocked by your allow-plugins config

When I try to run "Composer > Install" from PhpStorm I get the following error:
In PluginManager.php line 762:
icanhazstring/composer-unused (installed globally) contains a Composer
plug in which is blocked by your allow-plugins config. You may add it
to the list if you consider it safe.
You can run composer global config --no-plugins allow-plugins.icanhazstring/composer-unused [true|false] to enable it
(true) or disable it explicitly and suppress this exception (false)
See https://getcomposer.org/allow-plugins
What might be the reason for this error message above?
I have tried to run
/etc/php8/bin/php composer.phar global config --no-plugins allow-plugins.icanhazstrin true
and I have also tried to add the following to the composer.json file
"config": {
"allow-plugins": true,
but still I get the same error message.
My composer.json is something like:
{
"name": "test/version1",
"description":"Test Version 1",
"keywords":[],
"config": {
"allow-plugins": true,
"secure-http":false
},
"scripts": {
"test": "phpunit --config tests/phpunit.xml"
},
"require": {
"php": ">=7.3",
"monolog/monolog": "1.26.*",
...
"require-dev": {
"php": ">=8.1",
"phpunit/phpunit": "^9.5.0",
"friendsofphp/php-cs-fixer": "^2.16",
"icanhazstring/composer-unused": "^0.7.5"
}
}
Run the following command
/etc/php8/bin/php composer.phar global show -i
Then it will ask if you want to trust icanhazstring/composer-unused. Type y and press the enter button. That's it.
Then if you go to /home/yourUbuntuUsername/.config/composer/ there you will see another composer.json file with the following content:
{
"require": {
"icanhazstring/composer-unused": "^0.7.5"
},
"config": {
"allow-plugins": {
"icanhazstrin": true,
"icanhazstring/composer-unused": true
}
}
}
Apparently there is something called global Composer packages 🤷

Laravel vendor autoloading doesn't work

Currently, I am new on package development, and have a hard time on creating a new package. To the point, I want to make a new package, but the autoload (PSR-4 and classmap) seems not working. I get class not found exception. I tried to find the answer to my question everywhere, but still have not found any help.
Here is my composer.json file:
{
"name": "imamassi/tree",
"description": "Tree generator from an array/object with parent id",
"license": "MIT",
"authors": [
{
"name": "Imam Assidiqqi",
"email": "assidiqqi.imam#gmail.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload":{
"psr-4":{
"Imamassi\\": "src/"
},
"classmap":["src/"]
}
}
My package directory:
It already took me for several hours just to figure how to make this work, hope I'll find the answer here :)
NB: I already done dump-autoload for several times, still can't make this work.
The autoloading path must lead to the exact directory where the namespace is used.
That means for your composer.json:
"autoload": {
"psr-4": {
"Imamassi\\": "src/Imamassi"
}
}
After changing the main composer.json's autoload section, don't forget to rebuild autoloading cache via:
composer dump-autoload

Compser: can I specify install directory for some dependencies?

I have a composer.json file that looks like this:
{
"require": {
"laravel/laravel": "4.2",
"johnpbloch/wordpress": "4.0",
"twig/twig": "1.13.1"
}
}
After running composer install this is what my directory structure would look like:
/composer.json
/vendor/laravel
/vendor/wordpress
/vendor/twig
After running composer install I want my directory to look like this:
/composer.json
/laravel
/wordpress
/vendor/twig
How can I go about doing this?
You can use post-install-cmd to move things around. But in general this is a bad idea. It is better to keep all vendor stuff in one folder. After moving things around there will be problems with autoloader.
{
"require": {
"laravel/laravel": "4.2",
"johnpbloch/wordpress": "4.0",
"twig/twig": "1.13.1"
},
"scripts": {
"post-install-cmd": [
"mv vendor/laravel .",
"mv johnpbloch/wordpress ."
]
}
}

Cannot update yii2 via composer bower-asset/jquery could not be found

I was updating my yii2 via composer then reverted back to the old beta version.
Here is the error on my composer:
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested package bower-asset/jquery could not be found in any version, there may be a typ
o in the package name.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setti
ng
see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.
Read <http://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.
Tried searching for bower-asset/jquery at packagist but it is not found.
Thanks for the help :)
Finally fixed it, just followed the steps on the UPGRADE.md doc
If you are using Composer to upgrade Yii, you should run the following command first (once for all) to install the composer-asset-plugin:
composer global require "fxp/composer-asset-plugin:^1.2.0"
(See http://www.yiiframework.com/doc-2.0/guide-start-installation.html#installing-from-composer for latest version.)
You may also need to add the following code to your project's composer.json file :
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
Hopes this helps :)
For me helps to remove folder ~/.composer and execute command:
php composer.phar global require "fxp/composer-asset-plugin:1.*"
Then just run again
php composer.phar update
Found a cleaner solution. Just add following repository in your composer.json file
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
and watch the magic
If you don't want to use fxp/composer-asset-plugin then all you have to do is to follow these simple instructions from Yii2 documentation.
Using asset-packagist repository
This way will satisfy requirements of the majority of projects, that need NPM or Bower packages.
Note: Since 2.0.13 both Basic and Advanced application templates are
pre-configured to use asset-packagist by default, so you can skip this
section.
In the composer.json of your project, add the following lines:
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
Adjust #npm and #bower aliases in you application configuration:
$config = [
...
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
...
];
Visit asset-packagist.org to know, how it works.
If you don't need the update for bower-asset, you can require yidas/yii2-composer-bower-skip before yiisoft/yii2. in composer.json file:
"require": {
"php": ">=5.4.0",
"yidas/yii2-composer-bower-skip": "~2.0.0",
"yiisoft/yii2": "~2.0.5",
"yiisoft/yii2-bootstrap": "~2.0.0"
}
After that, you can update Composer smoothly without bower-asset.
See https://github.com/yidas/yii2-composer-bower-skip
Just in case for anyone upgrading Yii 2.0.41 - 2.0.43,
should be noted that you need to install the "external" bower-asset.
Run the following
composer require yidas/yii2-bower-asset
Then, need to update the aliases inside config (depends on your structure) for the Yii to handle the new bower-asset folder.
// here is important part
'aliases' => [
'#bower' => '#vendor/yidas/yii2-bower-asset/bower',
],
//below is just another config just ignore. example purpose don't copy
'components' => [
'db' => [
Then, reload your Yii app. Should be fine.
-Extra-
Here is the example of the composer.json for anyone who need the updates to 2.0.43
{
"name": "yiisoft/yii2-app-advanced",
"description": "Yii 2 Advanced Application Template",
"keywords": ["yii2", "framework", "advanced", "application template"],
"homepage": "http://www.yiiframework.com/",
"type": "project",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2"
},
"minimum-stability": "dev",
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "2.0.43",
"yiisoft/yii2-bootstrap": "*",
"yiisoft/yii2-swiftmailer": "~2.0#dev",
"yiisoft/yii2-redis": "~2.0.0",
"yiisoft/yii2-elasticsearch": "~2.0.0",
"bryglen/yii2-apns-gcm": "1.0.5",
"snhccm/baidu-push": "dev-master",
"google/cloud": "dev-master",
"minishlink/web-push": "6.0.7",
"understeam/yii2-fcm": "~0.1",
"yidas/yii2-bower-asset": "2.0.13"
},
"require-dev": {
"codeception/codeception": "*",
"yiisoft/yii2-debug": "*",
"yiisoft/yii2-gii": "*",
"yiisoft/yii2-faker": "*"
},
"config": {
"process-timeout": 1800
},
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
}
As described in YII2 repository documentation: https://asset-packagist.org/site/about
We can solve this problem by adding aliases on those folders in our config.
It will looks like that:
$config = [
...
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
...
];
It works perfectly!
Simple and clean solution:
In composer.json just replace the bower-asset/jquery line with:
"yidas/yii2-bower-asset":"*"
I propose we add also bower-asset/datatables to the yidas/yii2-bower-asset
My Problems with accepted solution of adding fxp/composer-asset-plugin are that the plugin is significantly slowing down the composer system, impacts everywhere, isn't always portable across operating systems and environments, has errors with PHP7.2 relating to inconsistent method names. So, I prefer my quicker to develop, faster at runtime, more local, and more compatible solution.
I tried all the mentioned steps like adding following in main.php
$config = [
...
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
...
];
composer.json
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
Doing "composer install/update" was still not installing bower packages given by yii2-bootstrap.
I found, I was using composer.phar 2x to set this up. I downgraded composer.phar to 1x and all works well without having the need of fxp/composer-asset-plugin plugin.

Custom repository doesn't read my composer.json?

I've forked https://github.com/jasonlewis/basset and is now trying to add my fork to my project. But it doesn't seem to read my forked composer.json file after I've run composer update, why? What am I doing wrong?
I've added this repository after require: {}:
"repositories": [
{
"type":"package",
"package": {
"name": "marwelln/basset",
"version":"master",
"source": {
"url": "https://github.com/Marwelln/basset.git",
"type": "git",
"reference":"master"
}
}
}
],
My forked composer.json looks like this (nothing is changed from the original except name):
{
"name": "marwelln/basset",
"description": "A better asset management package for Laravel.",
"keywords": ["assets", "basset", "laravel"],
"license": "BSD-2-Clause",
"authors": [
{
"name": "Jason Lewis",
"email": "jason.lewis1991#gmail.com"
}
],
"require": {
"php": ">=5.3.0",
"kriswallsmith/assetic": "1.1.*"
},
"require-dev": {
"mockery/mockery": ">=0.7.2",
"illuminate/config": "4.0.*",
"illuminate/console": "4.0.*",
"illuminate/filesystem": "4.0.*",
"illuminate/log": "4.0.*",
"illuminate/routing": "4.0.*",
"illuminate/support": "4.0.*",
"symfony/process": "2.3.*"
},
"suggest": {
"aws/aws-sdk-php": "Deploy static assets directly to your S3 buckets.",
"rackspace/php-cloudfiles": "Deploy static assets directly to your Cloud Files container."
},
"autoload": {
"psr-0": {
"Basset": "src/"
},
"classmap": [
"tests/Cases/FilterTestCase.php"
],
"files": ["src/helpers.php"]
},
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"minimum-stability": "dev"
}
With this, the Basset namespace isn't registered in autoload_namespace.php nor am getting "kriswallsmith/assetic": "1.1.*", but if I use the original require: { "jasonlewis/basset" : "dev-master"} it adds it just fine. What is it I'm missing?
You've done some things that are considered not the best practice with Composer.
First of all, you should try to avoid including repositories of the type "package" with all the details of where to find the stuff you need. This will become a maintenance nightmare in the long run. Packages should only be included in your composer.json if you are absolutely sure the maintainer of the software will not include a composer.json file himself. The documentation states on http://getcomposer.org/doc/04-schema.md#repositories:
package: If you depend on a project that does not have any support for composer whatsoever you can define the package inline using a package repository. You basically just inline the
composer.json object.
This case does not apply here, because you maintain the repository to be used yourself and should be able to put in a composer.json.
Now the process of forking and extending an existing library and then using that within your code is supported by Composer, but not the way you did it. First of all, you SHOULD replace the name of the vendor, because you take over that role, and your repository and the software should be distinguishable from other packages. So it is the correct thing to rename "jasonlewis/basset" into "marwelln/basset" and only use that name if you want to reference your own version.
Now the problem might be that if you forked a popular library, and are using other libraries that do require the original, you'll end up adding both packages. That's what the "replace" field is for: You can state in your own composer.json that you think your package is able to replace the original software. Some details are here: http://getcomposer.org/doc/04-schema.md#replace
I gave a detailed answer on how "replace" works here: How does the "replace" property work with composer?
In the end, I think you should do the following (and you still can change that):
Clone the original, change the composer.json to bear your own vendor name as the "name" and add a "replace" with the version of the original software you are changing. You should probably be strict about the version, because you really only replace the version you know about. If later you still know you replace a newer version, you can change the "replace" info.
Then you have to add your own repository as you did, and require the software name with your own vendor name.
You probably did most of that at some point in time, but the step with adding the replacement info is the missing link.
I changed repository to this:
"repositories": [
{
"type":"vcs",
"url": "https://github.com/Marwelln/basset"
}
],
And then used jasonlewis/basset instead of marwelln/basset. It's now working as it should.

Resources