How to test a model in a zend project

I want to know how to test models in the zend framework, I already tested the controller in the zend project.

my phpunit.xml:

<phpunit bootstrap="./bootstrap.php"  colors="true">         
    <testsuite>
        <directory>./</directory>       
    </testsuite>
    <filter>
        <whitelist>
            <directory suffix="MyApp.php">../application/</directory>
            <exclude>
                <file>../application/Bootstrap.php</file>
                <file>../application/controllers/ErrorController.php</file>
                <directory suffix=".phtml">../application/</directory>
            </exclude>
        </whitelist>
    </filter>


    <logging>
        <log type="coverage-html" target="./log/report" charset="UTF-8"
            yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/>
        <log type="testdox-html" target="./log/testdox.html" />
    </logging>         
</phpunit>

bootstrap.php is located in the same folder as phpunit.xml

<?php
error_reporting(E_ALL);

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/../tests'),
    get_include_path(),
)));

require_once 'Zend/Application.php';
require_once './application/controllers/ControllerTestCase.php';

ControllerTestCase.php:

<?php
    require_once 'Zend/Application.php';
    require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

    class ControllerTestCase 
        extends Zend_Test_PHPUnit_ControllerTestCase 
    {
        protected $application;

        public function setUp() {
            $this->bootstrap = array($this,'appBootstrap');
            parent::setUp();

        }

        public function appBootstrap() {
            $this->application = 
                new Zend_Application(APPLICATION_ENV,
                                     APPLICATION_PATH.'/configs/application.ini');

            $this->application->bootstrap();


        }
    }

I used ControllerTestCase as the base and wrote a test case for the controller, and it works, but I don’t know how to write a test case for models, should the test case for the model extend ControllerTestCase as well? or should it extend Zend_Test_PHPUnit_Db? And since the model will connect to the database, so how can I check it? can anyone help me with this? For example, I have a model:

<?php class Application_Model_User2 extends Custom_Model_Base {
public function __construct() {
        parent::__construct();
    }

    static function create(array $data) {
        return parent::_create(
                $_table,
                get_class(),
                $data,
                self::$_columns,
                true
        );
    }
}

How to check it?

+3
source share
1 answer

PHPUnit_Framework_TestCase, ControllerTestCase.

, , , .

, Zend_Test_PHPUnit_Db.

PHPUnit , Zend . .

, Application_Model_User2:

static function find($name, $order=null, $limit=null, $offset=null)
{ 
     return self::_selectAndBind(get_class(), 
                                 self::getDefaultAdapter()
                                 ->select()
                                 ->from($_table)
                                 ->where('name = ?', array($name))
                                 ->order($order)
                                 ->limit($limit, $offset)
                               ); 
}

, PHPUnit_Framework_TestCase, how-to docs Asset Functions, - :

require_once 'PHPUnit/Framework.php';

class Application_Model_User2Test extends PHPUnit_Framework_TestCase
{
    public function testFind()
    {
         // Get a result from the function we are testing
         $result = Application_Model_User2::find("foo", "date", 10, 0);

         // Test that there are 10 results
         $this->assertEquals(10, count($result));        
    }

}

, assertGreaterThan(), , .

. .

+1

All Articles