-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to use dependency injection (a la Laravel's IoC) in a Cest? #1115
Comments
Sure you can. Just do not reinvent the wheel ) You should create a few helpers with |
Thanks. I figured out I can just put my page objects and helper classes in _bootstrap.php. Pretty good documentation in the advanced section of the site. |
I'm reopening this issue because I'm still having some trouble with it. I investigated Codeception helpers, and I believe they're intended for adding methods to AcceptanceTester.php (or FunctionalTester, depending on where you include them), as a module. This isn't quite what I'm looking for. I don't need individual methods, but rather a full class devoted to specific page elements (like NavBarHelper). Codeception builds its Helper classes into the AccepterTester.php class, but this also means that all static references point to AcceptanceTester, and not the Helper class itself. I'm trying to find SOME way of injecting my own custom classes into a Cest, but I simply can't do it. Because Cests are dynamically instantiated, they can't take a constructor function with parameters. Codeception doesn't resolve the dependencies. I'd love to be able to do something like:
Because that way I can reference static properties on the NavBarHelper class, and organize my helpers on a per-component or page element basis. I like the added control versus a simple module that contains individual helper functions. My helper class might look something like this:
or like this:
|
As it stands, I tried doing something like this: Creating a NavBarHelper module and adding it to AcceptanceTester.php via acceptance.suite.yml.
In SignupCest.php:
I get the following error: PHP Fatal error: Access to undeclared static property: SignUpCest::$logoutLink in /home/vagrant/code/myapp/tests/_support/NavBarHelper.php on line 15 Naturally, hard coding a dependency into my Cest like this isn't optimal to begin with, but I can't even do this much:) |
Did you read about PageObjects in Codeception? |
Yep. I'm using PageObjects. They're great. The only problem is that I also want to use Cests to group my tests and have access to the before and after filters. I'd like to be able to inject a PageObject INTO a Cest. I think that's really what I'm trying to accomplish here. |
@andrewhl well, probably I can't propose anything of this kind. Unfortunately neither Codeception nor PHPUnit itself provide constructor injections into test cases. Probably we could implement such thing for Cest format but that's not something I'd implement in nearest future. |
@andrewhl please take a look at #1164. Maybe it's better to use fabpot/Pimple or something similar? |
@splinter89 Your pull request is pretty much exactly what I was looking for. Thanks! I hope it gets merged in soon. |
I like to use helper classes to group various tests and provide additional support for tests. I'd like to inject these into my Cest classes via the constructor. But Codeception, unlike the Laravel IoC, doesn't automatically initialize dependencies. How can I accomplish this?
Please see the relevant gist:
https://gist.github.com/andrewhl/bb9303f0c0cd479c759a
The text was updated successfully, but these errors were encountered: