Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Klorox's avatar

GUIDE Laravel 5 and phpspec Setup Instructions

This is not in the phpspec docs.

Here's how to get phpspec working with psr4:

  1. run app:name Namespace to setup Laravel 5 namespace.
  2. add "phpspec/phpspec": "2.1.*@dev" to composer.json
  3. run composer update.
  4. write a simple phpspec.yml file in project root it should look like this:
suites:
    my_suite:
        namespace: Namespace (this is the same as step 1)
        psr4_prefix: Namespace (again this is the same as step 1)
        src_path: app
        spec_prefix: spec

*note the tabs are important. Suites is left aligned. my_suite is one tab. and the remaining are double tabbed.

  1. test it by describing a spec. To do this run: phpspec desc Namespace/Test. This should create a spec folder in your root with Namespace/TestSpec.php inside.

*Note phpspec SHOULD create a Namespace folder here.

  1. run phpspec run. Say yes when it asks to create the file. This should create a file called Test.php in your app folder.

*Note phpspec SHOULD NOT create a Namespace folder here. Composer.json is setup to autoload files placed in the app folder with the appropriate namespace. In other words app:name sets this up automatically.

If all is well Test.php will be created and your test will return green. Code on.

0 likes
11 replies
lukasoppermann's avatar

Thank you, this did help me a lot. I don't see why the docs for phpspec are so bad aka not in depth at all.

Klorox's avatar

No problem. I agree the docs lack depth, I'm hoping that will change when 2.* gets more stable. They are not terrible by any means, but I've learned more about this framework by trying and failing than by reading.

For example sometimes you will want to setup predictable variables to pass to your Object under test. Like $string = 'something'. You can then use $this->beConstructedWith($string), and it will NOT be mocked. If on the other hand you do:

function let(SomeStringClass $string)
{
    $string = 'something';
    $this->beConstructedWith($string);
}

than this will be mocked. If you try to assert like $this->getString()->shouldBe('something'), you will be asserting against the double, not the actual string, and the test will fail. In fact, get used to anything you typehint being mocked.

Makes total sense, however not super obvious to beginners. Despite the shortcomings, this tool has made me an infinitely more confident coder, it's worth the pain.

1 like
codedungeon's avatar

@klorox So, have you tried to put your source code somewhere else (ie in a new directory under 'app') and get it to work? I can ONLY make this work when my code is "loose" at the 'app' directory level. Any other place and phpspec cant find it

  • I have tried namespacing correctly
  • I have definitely composer dump many times
Klorox's avatar

Ya it works fine on my end. Post your composer.json, and some code. Just to make sure...Say I have an implementation class called Imp.php in app/Virus/Imp.php with namespace Netrunner\Virus. Composer.json looks like

 
"require-dev": {
  "phpunit/phpunit": "~4.0",
  "phpspec/phpspec": "2.1.*@dev",
 },
 "autoload": {
  "classmap": [
   "database",
   "tests/TestCase.php"
  ],
  "psr-4": {
   "Netrunner\\": "app/"
  }
 },.....

then my spec folder would be spec/Netrunner/Virus/ImpSpec.php, with namespace spec\Netrunner\Virus. That file would be generated by typing
phpspec desc Netrunner/Virus/ImpSpec. My phpspec.yml would look like:


suites:
  netrunner_suite:
    namespace: Netrunner
    psr4_prefix: Netrunner
    src_path: app
    spec_prefix: spec

If your following bdd, then the implementation (app/Virus/Imp.php) would be generated by running the test suite. Hope this helps.

Klorox's avatar

This just happened to me as well. Not sure what changed. Oddly, when I removed psr4_prefix it worked again.


Composer:


"autoload": {
  "classmap": [
   "database/migrations",
   "database/seeds"
  ],
  "psr-4": {
    "Namespace\\": "src/"
  }
 },


Phpspec.yml:


formatter.name: pretty
suites:
  my_suite:
    namespace: Namespace
    src_path: src
    spec_prefix: spec
NoorDeen's avatar

why any one use the development branch not the master branch from phpspec repository ?

NoorDeen's avatar

this not working for me . when I run the tests it's fails . but when I remove

psr4-prefix

it is return to run but it is not generate the class in the proper way .

Please or to participate in this conversation.