dongxing8766 2015-11-04 10:19
浏览 35
已采纳

PHPunit测试错误

I'm new to PHP/PHPunit, I need some help! I'm working on an OpenCart project, I'm using PHPunit for Unit test. The class I need to test ModelSaleCustomer(admin\model\sale\customer.php) extend Model class(system\engine\model.php). I get that error when I'm running unit test.

Error This is class test I created:

require_once 'C:\xampp\htdocs\IDOtest\system\engine\model.php';
require_once 'C:\xampp\htdocs\IDOtest\admin\model\sale\customer.php';

class ModelSaleCustomerTest extends PHPUnit_Framework_TestCase {

    protected $object;

    protected function setUp() {


        $this->object = new ModelSaleCustomer();

    }
    protected function tearDown() {

    }

    public function testAddCustomer() {
        $expected = 3;
        $content=array ('customer_group_id'=>'1', 'firstname' =>'bnhung', 'lastname'=>'nguyen','email'=>'nhungbng@gmail.com','telephone'=>'012345670','fax'=>'123456','custom_field'=>'', 'newsletter'=>'','salt'=>'','password'=>'1234', 'status'=>'','approved'=>'','safe'=>'','date_added'=>'');
        $result = $model->addCustomer($content);
        //$actual=  $this->object->addCustomer($content);
        $this->assertEquals($expected,$actual);

This is Class Model

abstract class Model {
    protected $registry;

    public function __construct($registry) {
        $this->registry = $registry;
    }

    public function __get($key) {
        return $this->registry->get($key);
    }

    public function __set($key, $value) {
        $this->registry->set($key, $value);
    }
}

and this is class I need to test

class ModelSaleCustomer extends Model {
    public function addCustomer($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$data['customer_group_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? serialize($data['custom_field']) : '') . "', newsletter = '" . (int)$data['newsletter'] . "', salt = '" . $this->db->escape($salt = substr(md5(uniqid(rand(), true)), 0, 9)) . "', password = '" . $this->db->escape(sha1($salt . sha1($salt . sha1($data['password'])))) . "', status = '" . (int)$data['status'] . "', approved = '" . (int)$data['approved'] . "', safe = '" . (int)$data['safe'] . "', date_added = NOW()");

        $customer_id = $this->db->getLastId();

        if (isset($data['address'])) {
            foreach ($data['address'] as $address) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($address['firstname']) . "', lastname = '" . $this->db->escape($address['lastname']) . "', company = '" . $this->db->escape($address['company']) . "', address_1 = '" . $this->db->escape($address['address_1']) . "', address_2 = '" . $this->db->escape($address['address_2']) . "', city = '" . $this->db->escape($address['city']) . "', postcode = '" . $this->db->escape($address['postcode']) . "', country_id = '" . (int)$address['country_id'] . "', zone_id = '" . (int)$address['zone_id'] . "', custom_field = '" . $this->db->escape(isset($address['custom_field']) ? serialize($address['custom_field']) : '') . "'");

                if (isset($address['default'])) {
                    $address_id = $this->db->getLastId();

                    $this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
                }
            }
        }
    }
  • 写回答

2条回答 默认 最新

  • dqy012345 2015-11-04 20:56
    关注

    I wrote working exapmle with some comments:

    class ModelSaleCustomerTest extends PHPUnit_Framework_TestCase
    {
        protected $object;
    
        protected function setUp()
        {
            define('DB_PREFIX', '');
            $mockRegistry = $this->getMock('Registry', ['get']);
            $db           = $this->getMock('db', ['query', 'getLastId', 'escape']);
    
            // Stub our "$this->db"
            // 1. Db stub expects call of "query" method
            $db->expects($this->any())
               ->method('query')
               ->willReturn(true);
    
            // 2. Db stub expects call of "escape" method
            $db->expects($this->any())
               ->method('escape')
               ->willReturn('stub');
    
            // 3. Db stub expects call of "getLastId" method
            $db->expects($this->any())
               ->method('getLastId')
               ->willReturn(3);
    
            // Stub registry, which should contain $db and return it on "get" method call
            $mockRegistry->expects($this->any())
                         ->method('get')
                         ->willReturn($db);
    
            $this->object = new ModelSaleCustomer($mockRegistry);
        }
    
        public function testAddCustomer()
        {
            $content  = array(
                'customer_group_id' => '1',
                'firstname'         => 'bnhung',
                'lastname'          => 'nguyen',
                'email'             => 'nhungbng@gmail.com',
                'telephone'         => '012345670',
                'fax'               => '123456',
                'custom_field'      => '',
                'newsletter'        => '',
                'salt'              => '',
                'password'          => '1234',
                'status'            => '',
                'approved'          => '',
                'safe'              => '',
                'date_added'        => '',
            );
            $result = $this->object->addCustomer($content);
            // Next line commented, because "addCustomer" actually doesn't return anything
    //        $this->assertEquals($expected, $actual);
        }
    }
    

    You can start from here.

    P.S. Tested with PHPUnit 5.0.3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀