【发布时间】:2014-01-23 10:01:39
【问题描述】:
我正在尝试为使用 Propel 查询的类编写单元测试(使用 phpunit 和 mockery)。
如何模拟查询$contact = ClientContactQuery::create()->findPK($id);
我正在努力寻找任何例子。
我的班级;
<?php
namespace MyBundle\Classes;
use MyBundle\Model\ClientContactQuery;
use MyBundle\Model\ClientContact;
class Contacts {
protected $_cache;
public function __construct($cache)
{
$this->_cache = $cache;
}
public function getContact($id)
{
$contact = ClientContactQuery::create()->findPK($id);
if (! $contact) {
throw new NotFoundHttpException('Client contact not found.');
}
return $contact;
}
}
到目前为止我的测试用例;
<?php
namespace MyBundle\Tests\Classes;
use Mockery as m;
use MyBundle\Classes\Contacts as c;
class ContactsTest extends \PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}
public function testGetValidContact()
{
// Arrange
$cache = m::mock('cache');
// Act
$contact = new c($cache);
// am lost at this point :-(
// Assert
$this->assertInstanceOf('MyBundle\Classes\Contacts', $contact);
}
}
【问题讨论】:
标签: php unit-testing symfony phpunit propel