在 CakePHP 中,我写了两个模型,一个是 Invoice,另一个是 Product。发票“hasMany”与产品关联。发票有几个验证集,如“validateCreateInvoice”和“validateUpdateInvoice”等......
我正在编写如下所示的 PHPUnit 测试用例来对验证集进行单元测试。那么如何 1)动态设置验证集?2) 验证来自 PHPUnit 的模型和相关模型?
我运行了以下代码,商家电子邮件和产品名称数据验证应该失败。但事实并非如此。错误转储为空。可能是什么问题呢 ?注意:验证规则通过模型保存方法可以正常工作,这意味着验证规则没有问题。
App::import('Model', 'Invoice');
App::import('Model', 'Product');
App::uses('Validation', 'Utility');
App::uses('CakeFixtureManager', 'TestSuite/Fixture');
App::uses('CakeTestFixture', 'TestSuite/Fixture');
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'PHPUnit/Autoload.php';
class InvoiceTest extends PHPUnit_Framework_TestCase {
public $sourceName = 'Invoice';
public function setUp() {
parent::setUp();
$this->Invoice=& ClassRegistry::init('Invoice');
$this->InvoiceValidation =& ClassRegistry::init('Validate');
}
public function tearDown() {
parent::tearDown();
unset($this->Model, $this->Source);
ConnectionManager::drop($this->sourceName);
}
public function testCreate() {
$data = array(
"merchantEmail" => "stest_1339339519_bizyahoo.com", // not a valid email address
"payerEmail" => "stest_1340973819_per@yahoo.com",
"currencyCode" => "USD",
"paymentTerms" => "Net10",
"Products" =>array(
0 => array (
"productName" => "", // product name shouldn't be empty
"description" => "From CocaCola",
"unitPrice" => 1.56,
"quantity" => 1,
"taxName" => "Tax1",
"taxRate" => 7
),
1 => array (
"productName" => "Pepsi1",
"description" => "From Pepsi",
"unitPrice" => 1.65,
"quantity" => 1,
"taxName" => "Tax2",
"taxRate" => 7
),
),
);
//Create Invoice
$invoice=new Invoice();
$invoice->set($data);
$this->Invoice->setValidation('validateCreateInvoice');
$errors = $this->Invoice->validates($data); // validation should fails because of invalid merchant email address
Debugger::dump($errors); // output is null
$errors1 = $this->Invoice->validateAssociated($data); // validation should fail because product name is empty
Debugger::dump($errors1); // output is null
}
}