This is a question on proper coding of PHPUnit Tests in Zend Framework 2, using Object Oriented code principles.
I have the following PHPUnit Test Case:
namespace FedcoUserTest;
use FedcoUser\Controller\MachinistController;
class MachinistControllerTest extends Framework\TestCase
{...}
My MachinistController class is as such:
namespace FedcoUser\Controller;
class MachinistController extends \ZfcUser\Controller\UserController
{...}
When trying to run the test case, I would get this error:
Debug Error: MachinistController.php - Class 'ZfcUser\Controller\UserController' not found
That was weird, because my MachinistController class was running just fine and was finding ZfcUser's controller when ran as a web application, and not through the PHPUnit Test.
Somehow I've decided to put a
require_once 'vendor/zf-commons/zfc-user/src/ZfcUser/Controller/UserController.php';
line into my MachinistControllerTest class, and now my PHPUnit test runs without errors.
I found that to be weird.
Why: so far in my experience with ZF2, I have not had the need for use of require functions. ZF2 is fully OO, so why now? It may be correct, but is there a better way, and if so, what is it?
More specific Questions:
- (correctness) Am I right to use this require_once line in my code to make the test case pass?
- (ZF2 best practices) Why would I use a
require_once()statement in my otherwisely totally OO ZF2 code (Use ofrequireseems to me like a procedural code smell)? - (OO best practices) Why was ZfcUser's controller not being found, when my MachinistController was perfectly extending it?