2

I scrapped the earlier form of my question because it was too convoluted. Here's the new version.

I want to use phpspec with my psr-4 formatted projects.

Here's the way I tried to set up a test project:

  1. Created a new folder for the project:

    cd ~/Desktop/
    mkdir TestPhpSpec
    cd TestPhpSpec
    
  2. create a new composer.json file and require phpspec:

    composer require phpspec/phpspec
    

Which creates my composer.json file:

    {
        "require": {
            "phpspec/phpspec": "^2.3"
        }
    }
  1. I add my psr-4 namespace to the autoload property of my composer.json file:

    {
        "require": {
            "phpspec/phpspec": "^2.3"
        },
        "autoload": {
            "psr-4": {
                "Acme\\": "src/Acme"
            }
        }
    }
    
  2. Then I dump my autoload to make sure my namespace is loaded: composer dumpautoload

  3. After that, I create my phpspec.yml to describe the namespace to phpspec:

    suites:
      acme_suite:
          namespace: Acme
          psr4_prefix: Acme
    
  4. Then I describe the class I want to start building:

    phpspec describe Acme/Markdown
    

This is where I run into the first problem. Even though I specify the Acme namespace in my describe command, the spec does not get placed in a folder matching the namespace:

Incorrect namespaced spec

Though the class it creates is namespaced correctly:

<?php

namespace spec\Acme; // correct namespace

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class MarkdownSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType('Acme\Markdown');
    }
}

Then if I try to run the test to start TDD-ing.

    phpspec run

It offers to create the class for me and I let it. From there I get the second problem; I get the error message:

[PhpSpec\Process\Prerequisites\PrerequisiteFailedException] The type Acme\Markdown was generated but could not be loaded. Do you need to configure an autoloader?

enter image description here

And the class it creates is not in it's namespaced folder:

incorrect namespaced class

The class it creates is also namespaced correctly:

<?php

namespace Acme; // correct namespace

class Markdown
{
}

I've looked over the docs and can't figure out what I'm doing wrong. Any suggestions?

Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137

1 Answers1

4

Try with

suites:
  acme_suite:
      src_path: Acme/src
      spec_path: Acme/spec
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • I gave that a try. Now my specs are being created in an Acme namespace folder at the root level (I don't mind that), but when I run the spec and I'm asked if I would like it to create the class it is still being put at the root level of my `src` folder. The class created has the correct namespace declaration, but it's not in the right namespaced folder and phpspec still can't autoload the class :/ – Chris Schmitz Nov 24 '15 at 14:46
  • @ChrisSchmitz are you sure you need to specify both namespace/psr4_prefix in configuration and in composer? I'm not sure about that... – DonCallisto Nov 24 '15 at 16:21
  • 1
    Hmm, I'm not sure (still tying to learn how to use phpspec), but I would suspect you do need both. I need it in composer for the project autoloading, and the phpspec docs have a specific section about [using psr-4 with phpspec](http://phpspec.readthedocs.org/en/latest/cookbook/configuration.html#psr-4). That said, I'll give it a try really quick without the suite specification. – Chris Schmitz Nov 24 '15 at 16:27
  • holy...shit... I just emptied my phpspec.yml file, deleted all of the invalid spec and src files, did a whole new `desc` and `run` and it worked. I can't believe I burned so much time trying to configure this when it doesn't need it * *facepalm* *. Thank you SO MUCH for helping me figure this out! – Chris Schmitz Nov 24 '15 at 16:31
  • So this is the solution without any modification? – DonCallisto Nov 24 '15 at 16:33
  • Well, the solution is that you don't seem to need the psr-4 configuration at all for phpspec. I think the doc page I linked may have been for a previous version ( I don't see any kind of version information on the page, though the google search leading me in said it was for v 2.1 and I'm using 2.3). – Chris Schmitz Nov 24 '15 at 16:37
  • @ChrisSchmitz so you removed it from composer.json and you left the file as I wrote it here? – DonCallisto Nov 24 '15 at 16:38
  • Other way around, I left the psr-4 autoload in the composer.json file and removed suite information from the phpspec.yml file. The correct solution coming from when you asked "are you sure you need to specify both namespace/psr4_prefix in configuration and in composer?" and the answer being "no, you only need it in `composer.json` apparently". – Chris Schmitz Nov 24 '15 at 16:40
  • @ChrisSchmitz yes, because I didn't noticed that you had setted it into composer.json; i'll update my answer :) – DonCallisto Nov 24 '15 at 16:42
  • Try this =>$vendor/bin/phpspec run – Muhammad Atallah Mar 25 '16 at 06:32
  • 3
    I didn't get it working even after doing above mentioned, until I did that popular `composer dumpautoload` – Hafiz Jun 23 '16 at 22:33