我正在嘗試爲使用Propel查詢的類編寫單元測試(使用phpunit和mockery)。
如何模擬查詢$contact = ClientContactQuery::create()->findPK($id);
嘲笑函數中的Propel查詢(Symfony2)
我很努力地找到任何這方面的例子。
我的班級;
<?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);
}
}