samedi 4 juillet 2015

how to unit test a database insert function PDO

in below code how should i write unit tests ? I mean how should i mock the database ? what's the point in mocking ? imagine the in the same save class we have a setDb method which is inject database dependency.

public function save($stackContents){

            if( !is_array($stackContent) || count($stackContents) == 0 )
                return false; 

            $this->db->query("INSERT INTO 
                `mo` (msisdn, operatorid, shortcodeid, text, auth_token, created_at)
                 VALUES (:msisdn, :operatorid, :shortcodeid, :text , :auth_token, NOW())"
            );
            $this->db->beginTransaction();
            foreach($stackContents as $item) {

                $auth_token = $this->getAuthToken($stackContents);
                $this->db->bind(':msisdn', $item['msisdn']); 
                $this->db->bind(':operatorid', $item['operatorid']); 
                $this->db->bind(':shortcodeid', $item['shortcodeid']); 
                $this->db->bind(':text', $item['text']); 
                $this->db->bind(':auth_token', $auth_token); 
                $this->db->execute();
            }
            $this->db->endTransaction();

            echo 'A new job stack has been inserted into database ' . PHP_EOL ; 
        }

Aucun commentaire:

Enregistrer un commentaire