如何开始测试 Zend 框架模型?

发布于 2024-08-17 09:14:49 字数 1270 浏览 4 评论 0原文

如何开始在 Zend Framework 1.8+ 应用程序中测试我的模型?

假设我已设置应用程序以开始测试。我已经测试过控制器,所以我知道它可以工作。我的所有控制器都扩展了我的 ControllerTestCase.php 文件:

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

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    public $application;

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

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

    public function appBootstrap()
    {
        $this->application->bootstrap();
    }

    public function tearDown()
    {
        Zend_Controller_Front::getInstance()->resetInstance();

        $this->resetRequest();
        $this->resetResponse();

        $this->request->setPost(array());
        $this->request->setQuery(array());
        parent::tearDown();
    }
}

但现在我想开始测试我的模型。看起来我的 ModelTestCase.php 不会扩展 Zend_Test_PHPUnit_ControllerTestCase ,而是扩展 Zend_Test_PHPUnit_ModelTestCase ,但据我所知,不存在这样的类。如何开始测试我的 Zend Framework 模型?

How do I begin testing my models in a Zend Framework 1.8+ application?

Let's say I have my application set up to start testing. I have already tested a controller, so I know it works. I have all my controllers extending my ControllerTestCase.php file:

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

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    public $application;

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

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

    public function appBootstrap()
    {
        $this->application->bootstrap();
    }

    public function tearDown()
    {
        Zend_Controller_Front::getInstance()->resetInstance();

        $this->resetRequest();
        $this->resetResponse();

        $this->request->setPost(array());
        $this->request->setQuery(array());
        parent::tearDown();
    }
}

But now I want to start testing my models. It seems like my ModelTestCase.php would not extend Zend_Test_PHPUnit_ControllerTestCase but rather a Zend_Test_PHPUnit_ModelTestCase, but no such class exists that I know of. How can I start testing my Zend Framework models?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青瓷清茶倾城歌 2024-08-24 09:14:49

为您提供了一个基本的ControllerTestCase,因为设置和拆除测试控制器的环境需要复杂的步骤。输入是模拟 HTTP 请求,输出是呈现的 HTML,您需要抓取该 HTML 来查找预期内容。

模型更像是一个普通的旧 PHP 对象。需要设置的环境较少。接口只是对对象的方法调用。

因此,我将启动一个 TestCase 类来扩展 PHPUnit 的普通 TestCase,并首先添加至少一个 test 方法(作为 空function)用于模型类中的每个方法。最终,您的模型类中的每个方法都会有许多测试方法,但创建空测试方法是防止忘记某些模型方法的好方法。

请注意,模型不是表 --模型通常使用一个或多个Table对象。通过遵循此模式,您有机会创建 表的模拟对象,这样您就可以运行测试套件,而无需实时连接到数据库。

下面是设置模拟 Table 对象的示例,该对象经过硬编码以返回合成数据集而不是数据库中的数据集。

<?php

class MyModelTest extends PHPUnit_Framework_TestCase
{
  protected $_model;

  public function setUp()
  {
    $foo = $this->getMock('FooTable', array('find'));
    $foo->expects($this->any())
        ->method('find')
        ->will($this->returnValue(array("id"=>"123")));

    $this->_model = new MyModel();
    $this->_model->setFooTable($foo);
  }

  public function testCountElements()
  {
    $this->_model->get(123);
    $n = $this->_model->countElements();
    $this->assertEquals(1, $n);
  }

  public function testAsArray()
  {
    $this->_model->get(123);
    $a = $this->_model->asArray();
    $this->assertType('array', $a);
  }

  public function testAddElement()
  {
    // ...etc.
  }

  public function testGetElement()
  {
    // ...etc.
  }

}

There is a base ControllerTestCase provided for you because there are complex steps needed to setup and tear down the environment for testing a controller. The input is a mock HTTP request, and the output is rendered HTML that you need to scrape to find expected content.

A Model is more like a plain old PHP object. There's less environment to set up. The interface is simply method calls to the object.

So I would start a TestCase class that extends PHPUnit's plain TestCase, and start by adding at least one test method (as an empty function) for each method in your Model class. You will eventually have many test methods for each method in your Model class, but creating the empty test methods is a good way to keep from forgetting some of your Model methods.

Note that a Model is not a Table -- a Model typically uses one or more Table objects. By following this pattern, you have the opportunity to create mock objects for Tables so you can run the test suite without requiring a live connection to a database.

Here's an example of setting up a mock Table object, which is hardcoded to return a synthetic data set instead of a data set from a database.

<?php

class MyModelTest extends PHPUnit_Framework_TestCase
{
  protected $_model;

  public function setUp()
  {
    $foo = $this->getMock('FooTable', array('find'));
    $foo->expects($this->any())
        ->method('find')
        ->will($this->returnValue(array("id"=>"123")));

    $this->_model = new MyModel();
    $this->_model->setFooTable($foo);
  }

  public function testCountElements()
  {
    $this->_model->get(123);
    $n = $this->_model->countElements();
    $this->assertEquals(1, $n);
  }

  public function testAsArray()
  {
    $this->_model->get(123);
    $a = $this->_model->asArray();
    $this->assertType('array', $a);
  }

  public function testAddElement()
  {
    // ...etc.
  }

  public function testGetElement()
  {
    // ...etc.
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文