How to test yii2 module as a composer package

I’ve used yii2 for years, and I’m familiar to test yii2 application. As yii2-basic-app does, the tests directory is put under the application directory.

Since our project is becoming larger and larger, more and more modules are created. But because there’s no explicit dependency definition between modules, the module developers tend to use classes and functions from other module arbitrarily, which can greatly increase the project complexity that can make the project unmaintainable in the future.

So, currently, our team is shifting modules to composer packages. With explicitly defined dependency, it’s easy to constrain developer only call classes and functions in pre-defined packages.

But, the testing problem emerges.

In the yii2-basic-app, there’s only one tests directory, every testing and config file should be put under that directory.

However, when modules and features are developed in isolated packages, it’s apparent that we should put the testing file under its own package.

The question is, how to manage the testing files and successfully running the tests under such a circumstance?

Normally, since you’re extracting modules into their own composer packages, you should add unit tests for each packages and have a CI system run them.
These tests ensure that the package works as expected in isolation.

Then, in your main app, you could do some acceptance/functional testing to ensure that all packages are working together as expected.

Thank you. I looked into the new packages of yii, such as yiisoft/yii-core, all of them are using phpunit to do unit test. I find the configuration is much easier than codeception, so I will use phpunit to test my packages.

And you are right, because the packages are used by the application, it’s the application’s responsibility to do acceptance/functional testing.

1 Like

Acceptance testing for packages/modules is a tricky thing. You can have a look at i.e. giiant’s composer.json, to get an impression how we do that: https://github.com/schmunk42/yii2-giiant/blob/master/tests/_app/composer.json

Basically we install the package into a very basic yii2-app from a Docker image. Actually we do not install it, but merge the config with the one from the app, since this is the only way we’ve found to do that without having it on packagist first (but it has to be committed locally)

The basic yii2-app has acceptance tests set up and the package can run its tests.

It would be great if we could have a standardized process for that.

1 Like

Hi schmunk, happy to meet you here. Thank you for giant, I’m using it a lot, and have made several pull request to it too.

I’ve looked into the testing files of giiant, and find it helpful to me.

I think I will create a package to be included in require-dev of my packages, and this package will require yiisoft/yii and codeception, and provide basic utility functions. Then I can add acceptance testing files to my own package.

Which extension are you testing, is it available as open-source? Do you use Docker?
This is the basic app template I am referring to.

It’s the package of our company.

Yes, we use docker.

If the practice is successful, we will write another open source package to show how to test yii2 module implemented as a package.