Bundle Setup within Codeception - symfony

I tried to setup codeception for my Symfony2 project which already has 4 working bundles and their PHPUnit testcases. Now I wanted to add codeception Testcases, especially for acceptance tests, but when I "bootstrapped" codeception I found all the generated code within my main structure. Since my application is already organized in bundles I wanted to keep the concept as well for my testcases. Especially since I already have my PHPUnit testcases within my bundles. The official s2 codeception does not really cover this. So for me some questions arise:
How to setup codeception that it works within my bundles
How can I ran with one command all my existing PHPUnit testcases AND my codeception testcases?

I just set that up in a similar way.
codeception has a -c switch that will do what you need, i.e.:
vendor/bin/codecept bootstrap src/Acme/ExampleBundle
and then:
vendor/bin/codecept run -c src/Acme/ExampleBundle
I don't know about your 2nd question. At least for Unit tests, converting them to Codeception tests was trivial, especially since my tests all inhert from an abstract class, so it took maybe 10 lines of change there, mostly setup stuff.

#Tom That doesen't work because the Symfony2-Module is looking for the bootstrap.php.cache-file which is still in the root-folder of your symfony2-app! My solution for this is to create a link in your bundle to your app/ folder! than it gonna work!
In {your-root}/src/{your-app}/{your-bundle}:
ln -s ../../../app .

Related

Ability to Configure Environment Variables for Unit Tests (Specifically xUnit) in Rider

I have an xUnit project which from the IDE I run using the Unit Tests window or the run configuration All tests from Solution.
When configuring the All tests from Solution run configuration (or creating a new configuration), the name is the only configurable field and there is no way to specify environment variables. I'm using the Generic Host pattern to configure and expose my dependency injected resources. I need multi-environment support either through launchSettings or environment variables to configure the HostingEnvironment so different configurations can be tested, but there doesn't seem to be this capability in Rider. I think based on some search results I saw when searching on the problem that Visual Studio provides this capability for running xUnit / Unit Test projects.
This forum post is old but about the same / similar problem and one of the comments links to this youtrack issue which is marked fixed but then neither comments on that issue nor the comments on any of it's related "fixed" issues say how it was fixed or what the acceptable workaround is.
File | Settings | Build, Execution, Deployment | Unit Testing | Test Runner

How to test single Codeception class or method with PhpStorm

Is there any option to test a single Codeception class or method with PhpStorm/PHPUnit?
I know how to to run Test/TestCase with plain .php/PHPUnit files.
But how do I configure PhpStorm to work with Codeception?
You could give https://github.com/tsari/docker-xdebug-phpstorm a try.
That's a shell script which will be used as external tool in phpstorm and allows you to run or debug single methods, files, directories or all tests at once.
Currently it works only with docker. But you could fork and extend it. ;)

Symfony - PHPUnit list tests

I am using Symfony 2.5.4 and I am building test using PHPUnit on my Website. I was wondering if there was a way to list all the current implemented tests. I have looked in the app/console and there is no command that interact with PHPUnit. My goal is to give this list to the testers so they don't have to test was is already tested by PHPUnit. I would be ok with a way to get all the headers of the test functions or simply the name of the functions.
Symfony does not have a command that integrates with PHPUnit. To achive your goal, you can generate a code coverage report as HTML document (e.g. phpunit -c app --coverage-html=web/coverage src/) and you can show this report to your testers. You can explain there that they don't have to test functionality covered by tests.
But, in my opinion, your testers should test also the parts of application, that are covered by tests.

Symfony2: How to handle PHP Unit bootstrap file for contributed bundle?

What do I use as the autoload file for my PHP Unit configuration file for my contributed bundle?
I'm writing unit tests for my first contributed Symfony 2 bundle. I'm not sure how to handle the XML configuration file phpunit.xml.dist for PHP Unit. I know I should write my own config file(instead of relying on the one provided by Symfony Framework), but I've seen a few different ways people handle the bootstrap file that the config uses.
Symfony framework apps have a PHP Unit config file in the app/ folder and it uses bootstrap.php.cache as the autoloader, which really just defers to the composer autoloader in the vendor directory.
Several bundles I've looked at have their own bootstrap file but try and locate some other autoload file on the filesystem, making assumptions about where it may be. That doesn't seem right to me, but perhaps it is?
The best practices don't get into specifics here.
Best practices for unit testing is to test a small unit of functionality at a time, this unit should not rely on external influences and doesn't make sense to use bootstrap.php.cache as that is intended to bootstrap the entire symfony framework.
Your tests within a bundle should be able to execute on their own if someone were to checkout your bundle and contribute to it, they should be able to run tests there with only your code and dependencies. An ideal scenario would be that no bootstrap is necessary to set up the test suite, all you need is the autoloader. This is why many bundles have a bootstrap script that loads the autoloader.
There are two scenarios on the autoloader location relative to the bundle, either your bundle is installed standalone and it will be in vendor/autoload.php or your bundle is installed as a dependency, which in a standard configuration would you would fall back to a project autoloader if that exists.
This is variable depending on your setup and usage of target dir... You would need to traverse upward at least two levels to account for vendor name and package name, then if you have a target directory configuration you would need to traverse upwards the number of directories in your target path.

How to handle doctrine migrations in bundles

I'm developing an application, using Symfony 2.3, which will have to be installed for different customers. We will offer different features so the idea is to have the features/bundles separate from the main app and load the into the project using composer. As we are using Doctrine Migrations to maintain DB changes across versions I'm unsure on how to go about using them from a bundle. We're using Capifony to deploy the app to the live server.
So my question is... how can I automatize the execution of migrations from composer loaded bundles?
I ended up creating a command that will copy all migration files from predefined directories in bundles to the default location and then executing doctrine:migrations:migrate from within the command.
For a complex deployment, I used phing. He easily integrate with the Symfony console. But in the end I use of a simple code on the Synfony console.
Composer can easily call Symfony app commands as "post-install-cmd"
I don't think "composer loaded bundles" is the issue here. For instance, you could have several bundles in src/ (part of the app or submoduled) and have the exact same problem. The issue is having multiple entity managers and databases for your different bundles. Where they actually reside is trivial.
Anyway, I'm having the exact same problem. After some searching, I discovered there is actually an open pull request to fix this: https://github.com/doctrine/DoctrineMigrationsBundle/pull/46
I'm hoping it gets fixed soon!

Resources