user7791590
user7791590

Reputation: 21

Testion controler method in symfony (phpUnit)

i need some help i want to write a unit test about a controler method , i have searched for examples and tested a lot of method's but none of them has worked:

Here is my controller:

class ComputerController extends Controller
{
/**
     * @Route("/list-computers.html", name="back_computer_list")
     * @return RedirectResponse|Response
     */
    function listComputerAction()
    {
        $ad = $this->get("ldap_service");
        $computers = $ad->getAllComputer();
        return $this->render('BackBundle:Computer:list.html.twig', array(
            "computers" => $computers,
        ));
    }

I have tried to test it with mock like this:

class ComputerController extends Controller
{
    /**
     * @var EngineInterface
     */
    private $templating;


    public function setTemplating($templating)
    {
        $this->templating = $templating;
    }
and i have created a test method:

class ComputerControllerTest extends  TestCase {

    public function testlistComputerAction(){
        $templating = $this->getMockBuilder('BackBundle\Controller\ComputerController')->getMock();
        $computers = [1,2];
        $templating->expects($this->once())
            ->method('render')
            ->with('BackBundle:Computer:list.html.twig', array(
                "computers" => $computers))
            ->will($this->returnValue( $computers));

        $controller = new ComputerController();
        $controller->setTemplating($templating);

        $this->assertEquals('success', $controller->listComputerAction());
    }

When i start executing phpunit , i have this warning"Trying to configure method "render" which cannot be configured because it does not exist, has not been specified, is final, or is static"

I would be thankful if someone has an idea about this

Upvotes: 1

Views: 396

Answers (2)

user7791590
user7791590

Reputation: 21

I tried to Test a method in ldapService : Here is the method's of the service that i want to test

/**
     * @return bool|resource
     */
    public function getLdapBind()
    {
        if (!$this->ldapBind) {

            if ($this->getLdapConnect()) {
                $this->ldapBind = @ldap_bind($this->ldapConnect, $this->ldapUser, $this->ldapPass);
            }
        }
        return $this->ldapBind;

    }


    /**
     * @param $ldapUser
     * @param $password
     * @return bool
     */
    function isAuthorized($ldapUser, $password)
    {
        $result = false;
        if ($this->ldapConnect) {
            $result = @ldap_bind($this->ldapConnect, $ldapUser, $password);
        }
        return $result;
    }

Here is the test (using Mock):

<?php

namespace BackBundle\Tests\Service;

use PHPUnit\Framework\TestCase;
use BackBundle\Service\LdapService;
use PHPUnit_Framework_MockObject_InvocationMocker;

class LdapServiceTest extends  TestCase {



  public function testgetLdapConnect()
    {
//        $LdapService = new LdapService();
        $ldapMock = $this->getMockBuilder( 'LdapService')->setMethods(['getLdapBind'])->disableOriginalConstructor()->getMock();
        $ldapMock->expects($this->once())
//                ->method()
                ->with(array('ldap_bind', 'mike', 'password'))
            ->will($this->returnValue(true));

        $ldapMock->isAuthorized('mike', 'password');
    }
}

But i have a warning that i can't resolve : "Method name matcher is not defined, cannot define parameter matcher without one"

If someone , has an idea about that please

Upvotes: 1

Alister Bulman
Alister Bulman

Reputation: 35149

Honestly, there is nothing useful to test in that three-line controller. #1 is the service container, and #3 is the Twig subsystem. Line #2 can be unit tested on it's own.

With more complex controllers, I have found that making them a service where all the dependencies are passed in, either by constructor, or into the action itself does make slightly more complex controllers quite easy, but very few need that anyway.

Upvotes: 0

Related Questions