New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need some help. #33
Comments
Monkey patching only works if you are calling explicitly a "built-in function" or instantiating a "built-in class". But it doesn't work for "built-in inheritance" unfortunately. The only solution would be to introduce a new patcher imo (like described here). |
It looks like in solution line links is broken. |
It's just a comment of the issue n°5 |
You mean write a patcher something like Quit patcher and inject it into patcher namespace. https://github.com/crysalead/kahlan/blob/master/src/cli/Kahlan.php#L352-L365 like here? |
Yup, I mean a patcher which rewrite all: class MyModel extends \Phalcon\Mvc\Model to something like: class MyModel extends \a\custom\layer\Class The |
Just updated the Stub class to allow a So the deal is to have a patcher which is able to replace all |
And I sketched up the principle of a layer patcher in this commit f74002c in the dev branch. |
So on the dev branch adding the following: ...
$patchers->add('layer', new Layer([
'override' => [
'Phalcon\Mvc\Model'
]
])); inside a patchers endpoint should allow you to stub any kind of model's methods extended from Feel free to keep me updated if this patcher still have room for improvement according to your specific usage with Phalcon. |
Iside config i set: Filter::register('app.patchers', function($chain) {
$interceptor = Interceptor::instance();
$patchers = $interceptor->patchers();
$patchers->add('layer', new Layer([
'override' => [
'Phalcon\Mvc\Model'
]
]));
return $chain->next();
}); Then i have a model: <?php
namespace Api\Models;
class ApiLog extends \Phalcon\Mvc\Model {
} And spec: <?php
use kahlan\plugin\Stub;
describe("Stub with a layer filter", function() {
it("should stub a layer", function() {
Stub::on('Api\Models\ApiLog')->method('create', function($data=null, $whiteList=null) {
return true;
});
$model = new \Api\Models\ApiLog();
expect($model->create())->toBe(true);
});
}); And it fails. (It return |
After i clean-up PHP Fatal error: Cannot override final method Phalcon\Mvc\Model::__construct() in /vagrant/crm/phalcon_test/temp/vendor/crysalead/kahlan/src/plugin/Stub.php(226) : eval()'d code on line 73 It seems that no need in layer to patch-up a |
Fixed this adding on if ($method->getModifiers() & ReflectionMethod::IS_FINAL) continue; And got new error: PHP Fatal error: Declaration of kahlan\spec\plugin\stub\Stub0::assign() must be compatible with Phalcon\Mvc\ModelInterface::assign($data, $columnMap = NULL) in /vagrant/crm/phalcon_test/temp/vendor/crysalead/kahlan/src/plugin/Stub.php(226) : eval()'d code on line 72 |
Fixed this. I will prepare a patch |
@jails it works now perfectly. |
On a second run i have:
This line is: class Cards extends \kahlan\spec\plugin\stub\Stub0 { |
Ah yeah indeed, didn't think about that, I guess we will need to embed the sub inside the cached file. |
I think it will be more handy to you. |
I thought about something like that 8a52782 |
@jails it broken now. Just failing and that's all. I have a file, and it seems to be normal, but it just failing tests. |
@m1ome what the cached file in |
Now PHP Parse error: syntax error, unexpected '<' in /tmp/kahlan/vagrant/crm/phalcon_test/temp/app/models/ApiLog.php on line 6 It looks like you generate |
Ah yeah I fucked up the last dev commit. |
Should work better now but you will need to update https://github.com/crysalead/jit to its last master. |
@jails yes, now it's working as it should. |
@jails thank 👍 ! |
Cool, I'm merging it to master then. |
@jails Can u please help us with the undermentioned. When I am trying to stub phalcon's Functions (FINDFIRST, GetRelated etc) it is not working. I even put the API Patcher code in my kahlan-config.php which is given in PATCHER CODE IN kahlan-config.php Filter::register('api.patchers', function($chain) { Filter::apply($this, 'patchers', 'api.patchers'); PFA my model and test files. |
@shivshankarsomu you need multiple steps for testing Filter::register('app.namespaces', function($chain) {
$this->_autoloader->addPsr4('Api\\Models\\', __DIR__ . '/app/models/');
$this->_autoloader->addPsr4('Api\\Controllers\\V1\\', __DIR__ . '/app/controllers/v1/');
}); In e.g. we loading here models & controllers from directories, moving them to namespaces. Second, you should add a Filter::register('app.patchers', function($chain) {
$interceptor = Interceptor::instance();
$patchers = $interceptor->patchers();
$patchers->add('layer', new Layer([
'override' => [
'Phalcon\Mvc\Model'
]
]));
return $chain->next();
}); Third you should be CORRECT when passing parameters to stubbed method: Stub::on('Api\Models\Cards')->method('::findFirst', function($params = null) {
$card = new stdClass();
$card->active = 1;
return $card;
}); As you see i am providing |
@m1ome Thanks for the reply I changed the code to this then also it is not working
Please help me with this |
@shivshankarsomu do you loading models like in my first step? |
@m1ome yes PFB the code.
Its not working yet |
Can you please create a github repo with your code sample so we can clone it ? It'll be easier for us to get the issue by playing with your code. |
Your stub should look like this: $cards = Api\Models\Cards::findFirst();
Stub::on($cards)->method('getRelated', function($alias, $arguments = null) {
$card = new stdClass();
$card->active = 1;
return $card;
}); |
@shivshankarsomu you are welcome. Next time please use another issue to ask a question. Feel free to do it. |
I am trying to override a Phalcon model based method "find, findFirst" and e.t.c.
They are inside c-extension. Could them be override over Monkey patching ?
Or there is some way to do this overload default model namespace through Interceptor?
The text was updated successfully, but these errors were encountered: