Getting started with laravel - php

I just started my laravel course with laracast. I dont quite understand yet all the enviornment-related things.
I know that Composer is a kind of a program that downloads pre-written scripts to use in your project. But where does it work? On my local machine or on my vagrant homestead box VM? On which of these is it supposed to be installed?
I installed myself vagrant homestead box already but does it contain composer? When I go ssh into my guest machine and go to vagrant#homestead:/vagrant$ path I can see composer.json and composer.lock files, but does it mean that I have composer installed?

Composer is a PHP package manager, like npm for javascript or pip for python. There are many examples of package managers. It's useful, because adding dependencies to your php projects can be a pain, but composer makes it really easy. You just add the dependency to composer.json and you can use it right off the bat.
Composer isn't laravel specific, you can use it in any php project, laravel uses it to manage it's dependencies, laravels dependencies use it to manage their dependencies and so forth.
If nothing else, the composer autoloader is great, so you can use it even if you don't plan on using external packages.
Homestead should come with composer installed. A composer.lock file is generated when you run a composer install or composer update. If you plan on creating or using other php projects on your machine, it's probably a good idea to have composer installed on your machine as well.

Related

How can I install a single specific package from composer.json, to install developer tools?

I'm using PHP_CodeSniffer in my GitLab CI/CD pipelines to ensure my code is properly formatted. The job looks like follows:
stages:
- test
- build
- deploy
coding_standard:
stage: test
script:
- curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
- php phpcs.phar --extensions=php .
That's working as expected. However, the exact version of the tool is not specified here. So if there's suddenly a new major version of PHP_Codesniffer, the CI/CD job might fail, although my PHP code hasn't changed.
Furthermore, I currently have the tool installed globally on my local machine. In that way, I cannot have a specific version of the tool for every PHP project.
Now I'd like to add the tool as Composer dev-dependency (require-dev).
In the CI/CD job I would then call composer install instead of downloading the tool via curl.
The problem: That will download all packages needlessly, instead of just PHP_Codesniffer and its dependencies. Can I prevent that?
You can't do this with composer. You can't even install "only the dev dependencies". It's all the dependencies, all the non-dev dependencies, and that's all.
And it's generally a bad idea to install this kind of dependency as a project dependency, since very easily you can enter in dependency hell for reasons beyond your actual application needs. Development tools should not bring that level of complexity and danger to your deployment strategy.
To get around this, you could use something like the Composer Bin Plugin to isolate these dependencies and yet install them through composer. Then on CI you'd run composer install on this directory only, and run the tool from this location (or symlink it to bin, which is what the plugin does when it's installed, but you wouldn't have it installed in CI if you are not installing all the dependencies anyway).
Why not download any tagged version from Github through https://github.com/squizlabs/PHP_CodeSniffer/releases, like https://github.com/squizlabs/PHP_CodeSniffer/releases/download/3.6.0/phpcs.phar?
Using a PHAR is better than installing such stuff using Composer, as you might install other incompatible dependencies that way (this is not the case with phpcs, but other tools like phpmd install other dependencies from Symfony)

Benefits of installing PHPUnit with composer on Ubuntu?

I am developing a PHP website. I have a version on my laptop where I develop everything and my web server which runs the site.
I have found that I can use composer to install PHPUnit only on my laptop and not on my web server using the "require-dev" option Using "require-dev" to install packages in composer
However, this comes with some downsides:
From now on I have to call php composer update --no-dev on the webserver, and if I forget --no-dev then its also installed on the web server
I have to use $ ./vendor/bin/phpunit to call phpunit
I have to do install phpunit for each project on my laptop.
Would't it be much better to just install phpunit on Ubuntu sudo apt-get install phpunit? This way I would not have to worry about using the --no-dev option on the server and I could simply call it by $ phpunit. Am I missing anything important here?
Fast answer is:
You can have a version of phppunit you want in your project and another in another. And --no-dev should be used in production anyway, because you don't want to install all the dev dependencies in Production
if you don't want to call ./vendor/bin/phpunit add a script to your composer.json and then run the tests by composer test or anything you create
Explained in the first one. It really makes sense, especially when you work with some legacy code that works only with some particular versions of php/phpunit etc.
I usually install phpunit, and other tools in the 'require-dev' section, but another entirely reasonable option is to download the phpunit.phar file from the website, and check it in with the rest of your code - updating it manually occasionally.
A local (or global) Composer install will allow for better control of exactly which version is available though, and you can see when it, or your other dependencies are out of date with composer outdated.
As for a production deployment, you should be automating it as much as possible, to make sure that exactly the same thing happens every time. With that, it's just another few characters in your deployment script or other mechanism.

Can't run phpunit tests from command line

I am trying to run unit tests in a new laravel 5 application, using the phpunit framework. In the root path of my laravel application I ru the following command:
./vendor/bin/phpunit /tests/ExampleTest.php
And then I get the following message:
You need to set up the project dependencies using the following commands:
wget http://getcomposer.org/composer,phar
php composer.phar install
I already have composer installed in may system and I install Laravel 5 using composer. Isn't phpunit installed when I install a new laravel 5 application? If not, how can I install it in a existent laravel 5 application?
I known that I can also install phpunit globaly and resolve the problem. But maybe it will be a duplication since I have all the phpunit code already in may laravel application.
I had the same problem and it had a specific solution that may apply to other people. I store my files in Dropbox, and the vendor/bin/phpunit file should be an symlink like this
$ ls -lo vendor/bin/phpunit
lrwxr-xr-x vendor/bin/phpunit -> ../phpunit/phpunit/phpunit
However, Dropbox will occasionally replace symlinks with the original file, and this will cause the error above. The behaviour is inconsistent, and with relative symlinks seems to break when 2 machines are accessing Dropbox at the same time. Fixing the symlink worked for me or you could make a new symlink directly to the vendor/phpunit/phpunit/phpunit outside of Dropbox and run that.
Edit: Nowadays I exclude Vendor and node_modules from Dropbox - and simply run composer install when necessary. This works really well, and also deals with the hassle of syncing so many files on Dropbox. What you can do is go into the folder and delete all the files. Wait for Dropbox to sync. Then mark the folder as excluded. Finally, run composer install and you get the contents as you need. (Delete + composer install often solves other issues too).
Running composer install did nothing in my case. However, removing vendor folder and then calling composer install fixed it.
You need to have Composer installed and run composer install or composer update in your application to install the different packages listed in your composer.json.
When you install your Laravel application it doesn't install the packages right away.
You can verify the packages are installed by looking in the vendor directory of your application and check that phpunit is in there.
did you install phpunit globally? I recommend you do it.
just type in your laravel's root directory (e.g. /var/www)
cd /var/www
phpunit
if you what to test just one file, you can do something like this:
phpunit tests/ExampleTest.php
Unit Test:
D:\xampp\htdocs\Samplemed\vendor\bin>
phpunit ../../tests/Unit/Company/CompanyUnitTest
Feature Test:
D:\xampp\htdocs\Samplemed\vendor\bin>phpunit
../../tests/Feature/Company/CompanyFeatureTest
Please try this. its working fine.

Newest symfony installer vs composer

I'd like to know whats the difference when creating new symfony project with new symfony installer that has appeared last time and old-way composer.
I've installed latest version of symfony (2.6.1) with both, and result was different, for example when I install symfony with composer, i get .gitignore file.
When I install with new symfony installer script, gitignore is missing.
Here is amount of catalogs and files in fresh project:
symfony installer: 1498 directories, 7136 files
symfony installer + composer update: 1571 directories, 7749 files
composer create-project: 1615 directories, 7905 files
I suppose I'll stick to old way - composer, since new installer seems to be bugged or at least not complete yet, however I'd like to understand more on this topic, whats the difference, is it safe to use new installer etc?
As Leggendario already explained, the installer downloads the dist files from the website (a .tar.gz or .zip file). This speeds up the installation process quite a bit.
However, when building the dist files, symfony.com uses a custom build script which removes some files and changes some things. On the other hand, composer simply downloads the repository for you.
The main differences:
Composer downloads the latest dependencies (as Leggendario pointed out), while the build script contains the latest files at the moment of building.
Composer uses the dev versions and thus uses git clone to download the packages. The build script uses only stable packages, which will make Composer use the dist version. Some packages remove test and doc files from their dist files.
Composer contains all project related information, like a .gitignore. The build script previously assumed the person installing it didn't have git, so removed this file and other git related files like the .gitkeep files in app/cache and app/logs.
I any case, both the installer and composer always give you a working version of the Symfony Standard Edition.
At last, the build script was changed now the installer became the official way of installing. It'll now contain the git related files. On the other hand, it'll not contain the LICENSE file, UPGRADE-*.md files and README.md file. So in the end, we can say that the one installed by the installer is more usable, as it removes useless files.
Symfony2 Installer will downloaded it from the web site ( in this case: http://symfony.com/download?v=Symfony_Standard_Vendors_2.6.1.zip ).
To see the differences between symfony installer and the classic composer create-project is enough to take a look at both composer.lock: https://www.diffchecker.com/oig86oki
On the left the composer.lock generated after composer create-project, on the right symfony installer. It was obvious to everyone that Symfony2 downloaded from an archive could not have the lastest packages. So, do the update with composer update.
Again, on the left the composer.lock of composer create-project, on the right the new composer.lock after the update: https://www.diffchecker.com/lj5j2eap
As we expected. But in the vendor dir there are not the same number of file. Some folders are not there. Some folders with functional tests are not downloaded with symfony installer. You need to force composer to update all packages, or reinstall them.
Did you update installer as well with :
symfony self-update
or in windows :
php symfony.phar self-update
As stated here ?
That's perhaps one part of the answer.
Among differences, the installer seems to deal better with different symfony versions.

Using PHPUnit in CakePHP (Windows 7)

I've installed the latest version of CakePHP, then installed PHPUnit using PEAR and tried to get access using such link as
http://localhost/[project_name]/test.php
But as a result I've got a message that PHPUnit is not installed. I can't understand what the problem is. Are there any other ways to solve this problem ?
And is it possible not to install PHPUnit, but just copy all its files to, for example, Vendor directory of CakePHP and to use it locally just for one separate project ?
I disagree with Mark. :)
The most easy way to install phpunit systemwide is using composer as described on their installation page:
For a system-wide installation via Composer, you can run:
composer global require 'phpunit/phpunit=3.7.*'
The easiest way in Win is https://github.com/dereuromark/cakephp-phpunit
That is standalone and will all work out of the box with
cake Phpunit.Phpunit install
It has no dependencies outside of CakePHP itself. No composer, no pear no other 3rdparty issues.
If you do have one of those dependencies available, use that one, though.
Note:
As of now the pear channel has been shut down and as such there is only the composer solution to it now. (See other answer)

Categories