I never really write any tests because I usually have to work with low budgets. So I usually just delete the demo files. However, I noticed that the test directory was moved from /src/MyBundle/Test in Symfony 2 to /tests in Symfony 3 and I wonder why?
Bundles are, or at least I thought they are, potentially "decoupable" components in an application. This means it's always possible to run/work with the same bundle in another Symfony "runtime" with little to no effort.
So, moving the tests directory out of its related bundle into the root directory doesn't make any sense for me. Making an independent piece of software dependent/incomplete for no reason. It actually makes me feel like I completely misunderstood the concept of bundles in Symfony.
I also noticed that Symfony tutorials started a few years ago to force their users to move their templates into /app/Resources. When I learned it, they had the templates in their related bundles which seems much cleaner for me. Why should I ever move anything outside if I already have everything bundled (!) in a single directory. Maybe I have some problems with the translation, but in german, a "bundle" is 1 single unit. If you split a bundle, you get 2, but not a half.
I usually see Symfony as a configured runtime for my bundles, so I don't touch anything outside /src besides composer.json, AppKernel or config files.
Well, I may admit that the bundle may react different based on its environment - but unit tests shouldn't even rely on context if I got them right.
Even if you see the AppBundle as a special, context related one - it's still an inconsistent development style when some files can be found there, where others can be found in a completely different place. At least the AppBundle shouldn't be a bundle then.
Tests
I think that this change was made just to move out the tests from the actual src folder. It is kind of best practice in other programming languages like JAVA, because tests are not a part of your actual source code. Test also are not included in the final artifact (take composer.phar as example) to minimize its size.
The test directory structure is composed out of bundles directories with their related tests so still have separated tests for bundles:
tests/ ├── AppBundle/ │ ├── Entity │ │ └── UserTest.php │ ├── Service │ ├── Other │ └── Etc ├── OtherBundle/ │ ├── Controller │ └── Entity └── EtcBundle/
Templates
As far as template are concerned, it is way easier for designers to find all the needed templates when they are in a single directory.
Anyway, that's my two cents.
If you check the composer.json files, you could see that the Tests directory is (auto)loaded only for the dev environment so in a production system you don't have this files loaded too. As example:
"autoload": {
"psr-4": { "": "src/" },
"files": [
"app/AppKernel.php"
]
},
"autoload-dev": {
"psr-4": { "Tests\\": "tests/" }
},
This will speed up autoloading in your application.
Hope this help
It's because in Symfony 3 there's a separation between application bundles and reusable bundles:
There are two types of bundles:
- Application-specific bundles: only used to build your application;
- Reusable bundles: meant to be shared across many projects.
This article is all about how to structure your reusable bundles so that they're easy to configure and extend. Many of these recommendations do not apply to application bundles because you'll want to keep those as simple as possible.
You can learn more in bundle best practices in Symfony.