Search code examples
phpautoloadphpspecpsr-4

phpspec creates files in wrong folders per psr-4 namespace specification


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?


Solution

  • Try with

    suites:
      acme_suite:
          src_path: Acme/src
          spec_path: Acme/spec