We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Mocking Phalcon Classes

Are there any tools that can mock phalcon classes? I use codeception/aspectMock, but unless the method in question exists in my class (UserModels extends \Phalcon\Mvc\Model). So for example, I can't mock find, save, etc... because those typically arent in my extended classes.

I've seen a compiled extension in the past that looked capable of redifining ANY method calls... but cant remember the name.



7.9k

you should depend on abstraction than specific implementation, it should be easier for you to swap object during testing.



16.1k

Could you expound on this?



7.9k

for example creat interface for your models

<?php

interface MyModelInterface {
    public function save(MyModelInterface $model);
}

and implement it in your model

<?php

class MyModel extends Model Implement ModelInterface {
    public function save(MyModelInterface $model) {
        ...
    }
}

note: only use public method that defined in interface, this will allow you to create mock / double object based on interface