1
votes

In CakePHP I wrote two models one is Invoice and other one is Product. Invoice "hasMany" association with Product. Invoice has couple of validation sets like "validateCreateInvoice" and "validateUpdateInvoice" etc...

I'm writing PHPUnit test case like below to unit test the validation sets. So How to 1) set the validation sets dynamically? 2) validate models and associated models from PHPUnit ?

I ran the following code, The Merchant email and product name data validation should fail. But it is not. The errors dump is null. What could be the problem ? Note: The validation rules are perfectly working fine through model save method, it means no issue with validation rules.

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" => "[email protected]",
                "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

    }
}
1

1 Answers

0
votes

you seem to use non-core multiple validation behaviour from the bakery and the test suite seems not to honor the behaviour

I would ditch the multiple validation behaviour in favour of standard 'on' attribute of std validation: http://book.cakephp.org/2.0/en/models/data-validation.html#on