-
Can someone shed some light on how to mock models in Phalcon? I can't find any official documentation or examples online. I want to mock a model so that I don't need a real database during the test. The test calls a controller action which uses models. I'm using PHPUnit (and Mockery). Here's what I've tried so far, based off this link I found: final class MyControllerTest extends TestCase
{
public function setUp()
{
parent::setUp();
$this->di = new FactoryDefault();
DI::reset();
DI::setDefault($this->di);
$this->di->setShared('modelsManager', new Manager());
// This produces a single 'map-mymodel.php' file. There is no meta-* file generated.
$this->di->setShared(
'modelsMetadata',
new Files([
'metaDataDir' => __DIR__ . '/../test-resources/metadata/',
])
);
$con = $this->createPartialMock('\\Phalcon\\Db\\Adapter\\Pdo\\Mysql', [
'getDialect',
'query',
'execute',
]);
$dialect = $this->createPartialMock('\\Phalcon\\Db\\Dialect\\Mysql', [
'select',
]);
$results = $this->createPartialMock('\\Phalcon\\Db\\Result\\Pdo', [
'numRows',
'setFetchMode',
'fetchall',
]);
$results
->expects($this->any())
->method('numRows')
->will($this->returnValue(1));
$results
->expects($this->any())
->method('fetchall')
->will($this->returnValue([[]]));
$dialect
->expects($this->any())
->method('select');
$con->expects($this->any())
->method('getDialect')
->will($this->returnValue($dialect));
$con->expects($this->any())
->method('query')
->will($this->returnValue($results));
$con->expects($this->any())->method('execute');
$this->di->setShared('db', $con);
}
public function test_myAction()
{
// ...
$actual = $this->controller->myAction()->getContent();
// ...
}
} This results in the error:
If I mock |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Some progress, I added the Files Metadata service to the actual website (i.e. where I bootstrap/register services and stuff) and it dumped out the meta-* php files for models when I navigate to relevant pages. I guess it needed a real database connection to initially generate those, which makes sense. What I'm still confused about is that my tests worked in Phalcon 2 without mocking modelsManager, modelsMetadata and the database. Only after upgrading to Phalcon 3 did I run into this issue of the models trying to look up metadata in tests. Also, some tests still work in Phalcon 3 without mocking those services, and they too use models. The inconsistency is very confusing. |
Beta Was this translation helpful? Give feedback.
-
Confirmed that this fixes it for me, so marking as answered. However, I'd love to know if there is an easier / more standard way to mock models / databases. |
Beta Was this translation helpful? Give feedback.
Some progress, I added the Files Metadata service to the actual website (i.e. where I bootstrap/register services and stuff) and it dumped out the meta-* php files for models when I navigate to relevant pages. I guess it needed a real database connection to initially generate those, which makes sense.
What I'm still confused about is that my tests worked in Phalcon 2 without mocking modelsManager, modelsMetadata and the database. Only after upgrading to Phalcon 3 did I run into this issue of the models trying to look up metadata in tests. Also, some tests still work in Phalcon 3 without mocking those services, and they too use models. The inconsistency is very confusing.