I am new to testing with PHPUnit, and what I am trying to do is test a method called returnOnLogin()
that accepts a parameter Enlight_Event_EventArgs $args
and returns true
.
Here's the method I want to test:
public function returnOnLogin(\Enlight_Event_EventArgs $args)
{
$controller = $args->get('subject');
$view = $controller->View();
$controller->redirect([
'controller' => 'verification'
]);
// $view->addTemplateDir(
// __DIR__ . '/Views'
// );
return true;
}
Here's my test:
class MyFirstTestPluginTest extends TestCase
{
public function testReturnOnLogin()
{
$my_plugin = new MyFirstTestPlugin(true);
$expected = true;
//I tried following but it did not work
$this->assertEquals($expected, $my_plugin->returnOnLogin(//here is the problem it requires this array that I dont know));
}
}
Assuming that your controller class is
Controller
, and assuming that we don't care thatview()
is invoked in$controller
, this should cover what you are looking for:What does this test do?
Arrange
This test first arranges test doubles for use with the system under test (your plugin).
The first test double is your controller, we set it up in such a way that we expect that a method
redirect()
is invoked once with an argument that is identical to the specified array.The second test double is the argument, we set it up in such a way that we expect that a method 'subject()` is invoked one, and will return the controller.
Then, we set up the system under test, simply by creating an instance of
MyFirstTestPlugin
, passingtrue
to the constructor.Unfortunately, you haven't shared the constructor with us, we have no idea what the argument
true
stands for. If it affects the behaviour ofreturnLogin()
, then we clearly need to add more tests to assert the behaviour when the argument takes different values.Act
Then this test invokes the method
returnLogin()
on the system under test, and passes in one of the test doubles.Assert
Eventually, this test asserts that the method
returnLogin()
returnstrue
.Note Take a look at