PHPunit测试错误
我是PHP/PHPunit新手,我需要一些帮助! 我正在从事一个OpenCart项目,我正在使用PHPunit进行单元测试。我需要测试ModelSaleCustomer(admin\model\sale\customer.php)扩展模型类(system\engine\model.php)的类。我在运行单元测试时遇到了这个错误 这是我创建的类测试:PHPunit测试错误,php,unit-testing,phpunit,Php,Unit Testing,Phpunit,我是PHP/PHPunit新手,我需要一些帮助! 我正在从事一个OpenCart项目,我正在使用PHPunit进行单元测试。我需要测试ModelSaleCustomer(admin\model\sale\customer.php)扩展模型类(system\engine\model.php)的类。我在运行单元测试时遇到了这个错误 这是我创建的类测试: require_once 'C:\xampp\htdocs\IDOtest\system\engine\model.php'; require_o
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);
这是班级模式
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);
}
}
这是我需要测试的课程
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 . "'");
}
}
}
}
它应该对您有所帮助。 我认为您可以模拟一个类模型来隔离它,或者您也可以像在您的评论中那样 说书人: 所述添加
新模型salecustomer($regsitry)代码>
它应该对您有所帮助。
我认为您可以模拟一个类模型来隔离它,或者您也可以像在您的评论中那样
说书人:
所述添加新模型salecustomer($regsitry)代码>
我写了一篇关于工作的论文,其中有一些评论:
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);
}
}
你可以从这里开始
用PHPUnit 5.0.3测试的p.S.我写了一个工作示例,其中有一些评论:
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);
}
}
你可以从这里开始
使用PHPUnit 5.0.3测试的p.S.您是否尝试在设置()中将$registry
参数传递给modelsalecuster
的构造函数?如下:newmodelsalecuster($regsitry)代码>-用$registry
填充了这个类需要的任何对象,我以前尝试过,但它不起作用=((我得到了新的错误:“致命错误:在第10行的C:\xampp\htdocs\IDOtest\system\engine\model.php中的非对象上调用成员函数get())而且没有执行任何测试您是否尝试在setUp()
方法中将$registry
参数传递给modelsalecuster
的构造函数?例如:newmodelsalecuster($regsitry);
-在$registry
中填充了该类需要的任何对象我以前尝试过,但它不起作用=((我遇到了新错误:“致命错误:在第10行的C:\xampp\htdocs\IDOtest\system\engine\model.php中对非对象调用成员函数get()”没有执行任何测试你好!我遇到了另一个问题!当我运行测试用例时,它是通过的,但测试数据没有插入数据库???;你好!我遇到了另一个问题!当我运行测试用例时,它是通过的,但测试数据没有插入数据库;