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.

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

So how this works is:

  • all declarations are stored inside src folder even those used for tests. 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.

I think this way I can start building something… that makes sense!




Enjoy Reading This Article?

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

  • How integrate GitHub Copilot to generate commit messages automatically?
  • 🤓 Why do PHP developers love the empty line at the end of the file? PHP Review #6
  • 🧹 How to Remove Recipes from the `extra` Section in composer.json
  • 🧐 Floats and the Mystery of -0.0 PHP Review #5
  • PhpUnit willReturnMap - PHP Review #4