Finally well written tests with composer loading structure

I stumbled, crawled on ground while having not proper setup in my old project that I should work on. and then I figured how I should structure my loading properties

here is what I had in my project

{
  "autoload": {
    "psr-4": {
      "Ziumper\\App\\": "src/",
      "Ziumper\\App\\": "tests/"
    }
  }
}

but this way all may classes seems to be loaded inside composer autload map. I knew there must be better way to solve that. Here is what I figured out

{
  "autoload": {
    "psr-4": {
      "Ziumper\\App\\": "src/"
    }
  },
  "autoload-dev": {
    "psr-4": {
      "Ziumper\\App\\": "tests/"
    }
  }
}

Durring that time I got some really serious issues, I couldn’t declare Traits in my tests folder and it was quite hard feeling to copy paste all that code, then I moved forward with next version. Later I figured out that I had an issues with PS-4 autoloading inside shopware plugins. So I decided to move with following.

{
  "autoload": {
    "psr-4": {
      "Ziumper\\App\\": "src"
    },
    "exclude-from-classmap": ["tests"]
  },
  "autoload-dev": {
    "psr-4": {
      "Ziumper\\App\\Tests\\Utils\\": "tests/utils",
      "Ziumper\\App\\Tests\\Unit\\": "tests/unit",
      "Ziumper\\App\\Tests\\Integration\\": "tests/integration"
    }
  }
}

So how this works is:

  • all base code declarations are stored inside src folder and those used for tests are inside tests\utils folder. Base test cases traits, data providers too.
  • integration and tests are using src references and are giving me some nice working cases.
  • exclude from classmap is a guard which stands in front of my testing code and doesn’t allow it to get into production loading flow.
  • PSR-4 autoload in Composer loads classes by namespace → folder → filename.
  • PHPUnit use PS-4 not auto generated class map.
  • exclude-from-classmap = guards from loading my utility classes in production
  • no need to write custom bootstrap loading

The class loading process may vary depending on the framework or rules used. For example, in shopware, plugins do not want to load Utils classes, for which you have to write with your own bootstrapper class.




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • New n8n Tool!
  • Alternative IDE for PHP? Apache NetBeans, but not only!
  • Comma at the end of function arguments in PHP (trailing comma)
  • Using array_map() in a Programmer's Daily Life. PHP Review #7
  • How integrate GitHub Copilot to generate commit messages automatically?