seri::diary

プログラミングのこととかポエムとか

CakePHPのModelクラスのメソッドに渡す条件を組み立てるクラスを作っている


久々にPHPネタ。今作ってる。とりあえずconditionとfieldをセットするメソッドだけ作ってUT。

<?php
App::uses('ConditionBuilder','Vendor');

class ConditionBuilderTest extends CakeTestCase{
    
    
    public function setUp() {

    }
    
    public function testCreateFieldName(){
         $post = new ConditionBuilder('Post');
         $result = $post->createFullFiledName("id");
         $this->assertEqual($result, 'Post.id');
    }

    
    public function testCreateCondition() {
        $post = new ConditionBuilder('Post');
        $result = $post->setCondition('id', 3)
               ->setCondition('name', 'test')
                ->setFields('name')
                    ->build();
        print_r($result);
// Array
// (
//    [conditions] => Array
//        (
//            [Post.id] => 3
//            [Post.name] => test
//        )
//
//    [fields] => Array
//        (
//            [0] => Post.name
//        )
//
// )
        $this->assertNoErrors();
    }
}
$ Console/cake test app Vendor/ConditionBuilder

Welcome to CakePHP v2.3.6 Console
---------------------------------------------------------------
App : app
Path: /Users/seri/NetBeansProjects/digupper/app/
---------------------------------------------------------------
CakePHP Test Shell
---------------------------------------------------------------
PHPUnit 3.7.21 by Sebastian Bergmann.

..Array
(
    [conditions] => Array
        (
            [Post.id] => 3
            [Post.name] => test
        )

    [fields] => Array
        (
            [0] => Post.name
        )

)


Time: 0 seconds, Memory: 6.25Mb

OK (2 tests, 1 assertion)
macbkpro:app seri$ Console/cake test app Vendor/ConditionBuilder

PHPのstdClassが地味に便利だということに今更ながら気づいた。連想配列より綺麗に書ける。