mardi 26 juillet 2016

Pass data variable to function when Unit Testing

I am new to unit testing in PHP and I'm having a little trouble. Whether that is because I'm using the Cake framework or because I'm used to the Java way, point it I'm having issues.

I'm writing tests for a Model function that gets called on the submit of a form. The function receives two parameters, which I think I'm passing through correctly, and a data object that is not received as a parameter. My question is how do I populate that "data" object? I keep getting and "undefined index" error when I run the tests.

I've tried both mocking the data and using fixtures, but in all honesty, I don't get this stuff. Below is my model function, followed by my test code.

public function isUniqueIfVerified($check, $unverified){
    $found = false;
    if ($this->data['Client']['client_type_id'] == 5) {
        $found = $this->find ( 'first', array (
            'conditions' => array (
                $check,
                $this->alias . '.' . $this->primaryKey . ' !=' => $this->id,
                'client_type_id <>' => 5
            ),
            'fields' => array (
                'Client.id'
            )
        ) );
    } else {
        $found = $this->find ( 'first', array (
                'conditions' => array (
                        $check,
                        $this->alias . '.' . $this->primaryKey . ' !=' => $this->id
                ),
                'fields' => array (
                        'Client.id'
                )
        ) );
    }

    if ($found) {
        return false;
    } else {
        return true;
    }
}

This is like the 52 version of my test function, so feel free to just do whatever you want with it. I was thinking that mocking the data would be easier and faster, since I only really need the 'client_type_id' for the condition inside my Model function, but I couldn't get that 'data' object to work, so I switched to fixtures... with no success.

public function testIsUniqueIfVerified01() {
    $this->Client = $this->getMock ( 'Client', array (
            'find' 
    ) );
    $this->Client->set(array(
                'client_type_id' => 1,
                'identity_no' => 1234567890123
    ));
    //$this->Client->log($this->Client->data);
    $check = array (
            'identity_no' => '1234567890123' 
    );
    $unverified = null;

    $this->Client = $this->getMockforModel("Client",array('find'));
    $this->Client->expects($this->once())
        ->method("find")
        ->with('first', array (
                'conditions' => array (
                        "identity_no" => "1234567890123",
                        "Client.id" => "7711883306236",
                        'client_type_id <>' => 5
                ),
                'fields' => array (
                        'Client.id'
                )
        ))
        ->will($this->returnValue(false));      

    $this->assertTrue($this->Client->isUniqueIfVerified($check, $unverified));

    unset ( $this->Client );
}

Again, I'm very green when it comes to Cake, and more specifically PHP Unit Testing, so feel free to explain where I went wrong.

Thanks!

Aucun commentaire:

Enregistrer un commentaire