PHP Mockery partial test double not calling method from parent class2019 Community Moderator ElectionHow do I test a private function or a class that has private methods, fields or inner classes?Reference — What does this symbol mean in PHP?Reference - What does this error mean in PHP?How to partially mock a class using Mockery?Mockery: how does a passive partial mock differ from the default mock?Passing type to SplObserver update methodTesting that one class method calls anothermocking out database queries laravel mockeryMockery: mock publicaly overridden protected methodMockery not able to call my method in testing method
Is there a way to find out the age of climbing ropes?
Can inspiration allow the Rogue to make a Sneak Attack?
The need of reserving one's ability in job interviews
Under what conditions would I NOT add my Proficiency Bonus to a Spell Attack Roll (or Saving Throw DC)?
Can you run a ground wire from stove directly to ground pole in the ground
How do we objectively assess if a dialogue sounds unnatural or cringy?
Are Wave equations equivalent to Maxwell equations in free space?
Is being socially reclusive okay for a graduate student?
Is "cogitate" an appropriate word for this?
Ultrafilters as a double dual
Replacing tantalum capacitor with ceramic capacitor for Op Amps
Did Amazon pay $0 in taxes last year?
Convert an array of objects to array of the objects' values
Should we avoid writing fiction about historical events without extensive research?
Is divide-by-zero a security vulnerability?
Professor forcing me to attend a conference
What is the purpose of a disclaimer like "this is not legal advice"?
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
Are small insurances worth it
If nine coins are tossed, what is the probability that the number of heads is even?
Has a sovereign Communist government ever run, and conceded loss, on a fair election?
Why is my explanation wrong?
When to use the term transposed instead of modulation?
Why is there an extra space when I type "ls" on the Desktop?
PHP Mockery partial test double not calling method from parent class
2019 Community Moderator ElectionHow do I test a private function or a class that has private methods, fields or inner classes?Reference — What does this symbol mean in PHP?Reference - What does this error mean in PHP?How to partially mock a class using Mockery?Mockery: how does a passive partial mock differ from the default mock?Passing type to SplObserver update methodTesting that one class method calls anothermocking out database queries laravel mockeryMockery: mock publicaly overridden protected methodMockery not able to call my method in testing method
I'm confused why my partial test double is not calling a method correctly. These are my classes:
abstract class Model
public function update(array $attributes)
$this->performUpdate($attributes);
abstract protected function performUpdate(array $attributes);
class Product extends Model
protected function performUpdate(array $attributes)
print_r($attributes);
$mock = Mockery::mock(Product::class)->makePartial();
$mock->update([]); // the 'performUpdate' method never gets called
When the code is run, performUpdate
in the Product
class is never called. The 'update' method is called as expected, but the real performUpdate
method is never called. Instead, it just returns null as if it were not a partial mock. Partial mocks are supposed to defer to the method in the mocked class if no expectation is set up, so why isn't my method getting called?
php unit-testing mockery
add a comment |
I'm confused why my partial test double is not calling a method correctly. These are my classes:
abstract class Model
public function update(array $attributes)
$this->performUpdate($attributes);
abstract protected function performUpdate(array $attributes);
class Product extends Model
protected function performUpdate(array $attributes)
print_r($attributes);
$mock = Mockery::mock(Product::class)->makePartial();
$mock->update([]); // the 'performUpdate' method never gets called
When the code is run, performUpdate
in the Product
class is never called. The 'update' method is called as expected, but the real performUpdate
method is never called. Instead, it just returns null as if it were not a partial mock. Partial mocks are supposed to defer to the method in the mocked class if no expectation is set up, so why isn't my method getting called?
php unit-testing mockery
add a comment |
I'm confused why my partial test double is not calling a method correctly. These are my classes:
abstract class Model
public function update(array $attributes)
$this->performUpdate($attributes);
abstract protected function performUpdate(array $attributes);
class Product extends Model
protected function performUpdate(array $attributes)
print_r($attributes);
$mock = Mockery::mock(Product::class)->makePartial();
$mock->update([]); // the 'performUpdate' method never gets called
When the code is run, performUpdate
in the Product
class is never called. The 'update' method is called as expected, but the real performUpdate
method is never called. Instead, it just returns null as if it were not a partial mock. Partial mocks are supposed to defer to the method in the mocked class if no expectation is set up, so why isn't my method getting called?
php unit-testing mockery
I'm confused why my partial test double is not calling a method correctly. These are my classes:
abstract class Model
public function update(array $attributes)
$this->performUpdate($attributes);
abstract protected function performUpdate(array $attributes);
class Product extends Model
protected function performUpdate(array $attributes)
print_r($attributes);
$mock = Mockery::mock(Product::class)->makePartial();
$mock->update([]); // the 'performUpdate' method never gets called
When the code is run, performUpdate
in the Product
class is never called. The 'update' method is called as expected, but the real performUpdate
method is never called. Instead, it just returns null as if it were not a partial mock. Partial mocks are supposed to defer to the method in the mocked class if no expectation is set up, so why isn't my method getting called?
php unit-testing mockery
php unit-testing mockery
asked Mar 5 at 4:19
flyingL123flyingL123
2,96352773
2,96352773
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have to use the shouldAllowMockingProtectedMethods
when creating the mock. This will resolve the issue of you not seeing the output.
That’s not true. The method is not called. Did you try it yourself?
– flyingL123
yesterday
Yes I tried it and it works fine. Since you don't return anything from the functionperformUpdate
you're gettingnull
back. If you try the same thing in the console you'll see the text
– Borisu
yesterday
But I’m not taking about returning anything. Running that code should print an empty array. For me it doesn’t. It does for you? I’ll have to check again.
– flyingL123
yesterday
Sorry about that, I was testing with a class where the method was public. I've edited the answer to show how to use it with protected methods.
– Borisu
yesterday
But I’m not mocking the protected method. The protected method is an internal call. Since it’s a partial mock, the call to the public update method should call performUpdate internally as normal, but it’s not.
– flyingL123
yesterday
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54995324%2fphp-mockery-partial-test-double-not-calling-method-from-parent-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to use the shouldAllowMockingProtectedMethods
when creating the mock. This will resolve the issue of you not seeing the output.
That’s not true. The method is not called. Did you try it yourself?
– flyingL123
yesterday
Yes I tried it and it works fine. Since you don't return anything from the functionperformUpdate
you're gettingnull
back. If you try the same thing in the console you'll see the text
– Borisu
yesterday
But I’m not taking about returning anything. Running that code should print an empty array. For me it doesn’t. It does for you? I’ll have to check again.
– flyingL123
yesterday
Sorry about that, I was testing with a class where the method was public. I've edited the answer to show how to use it with protected methods.
– Borisu
yesterday
But I’m not mocking the protected method. The protected method is an internal call. Since it’s a partial mock, the call to the public update method should call performUpdate internally as normal, but it’s not.
– flyingL123
yesterday
|
show 4 more comments
You have to use the shouldAllowMockingProtectedMethods
when creating the mock. This will resolve the issue of you not seeing the output.
That’s not true. The method is not called. Did you try it yourself?
– flyingL123
yesterday
Yes I tried it and it works fine. Since you don't return anything from the functionperformUpdate
you're gettingnull
back. If you try the same thing in the console you'll see the text
– Borisu
yesterday
But I’m not taking about returning anything. Running that code should print an empty array. For me it doesn’t. It does for you? I’ll have to check again.
– flyingL123
yesterday
Sorry about that, I was testing with a class where the method was public. I've edited the answer to show how to use it with protected methods.
– Borisu
yesterday
But I’m not mocking the protected method. The protected method is an internal call. Since it’s a partial mock, the call to the public update method should call performUpdate internally as normal, but it’s not.
– flyingL123
yesterday
|
show 4 more comments
You have to use the shouldAllowMockingProtectedMethods
when creating the mock. This will resolve the issue of you not seeing the output.
You have to use the shouldAllowMockingProtectedMethods
when creating the mock. This will resolve the issue of you not seeing the output.
edited yesterday
answered yesterday
BorisuBorisu
520210
520210
That’s not true. The method is not called. Did you try it yourself?
– flyingL123
yesterday
Yes I tried it and it works fine. Since you don't return anything from the functionperformUpdate
you're gettingnull
back. If you try the same thing in the console you'll see the text
– Borisu
yesterday
But I’m not taking about returning anything. Running that code should print an empty array. For me it doesn’t. It does for you? I’ll have to check again.
– flyingL123
yesterday
Sorry about that, I was testing with a class where the method was public. I've edited the answer to show how to use it with protected methods.
– Borisu
yesterday
But I’m not mocking the protected method. The protected method is an internal call. Since it’s a partial mock, the call to the public update method should call performUpdate internally as normal, but it’s not.
– flyingL123
yesterday
|
show 4 more comments
That’s not true. The method is not called. Did you try it yourself?
– flyingL123
yesterday
Yes I tried it and it works fine. Since you don't return anything from the functionperformUpdate
you're gettingnull
back. If you try the same thing in the console you'll see the text
– Borisu
yesterday
But I’m not taking about returning anything. Running that code should print an empty array. For me it doesn’t. It does for you? I’ll have to check again.
– flyingL123
yesterday
Sorry about that, I was testing with a class where the method was public. I've edited the answer to show how to use it with protected methods.
– Borisu
yesterday
But I’m not mocking the protected method. The protected method is an internal call. Since it’s a partial mock, the call to the public update method should call performUpdate internally as normal, but it’s not.
– flyingL123
yesterday
That’s not true. The method is not called. Did you try it yourself?
– flyingL123
yesterday
That’s not true. The method is not called. Did you try it yourself?
– flyingL123
yesterday
Yes I tried it and it works fine. Since you don't return anything from the function
performUpdate
you're getting null
back. If you try the same thing in the console you'll see the text– Borisu
yesterday
Yes I tried it and it works fine. Since you don't return anything from the function
performUpdate
you're getting null
back. If you try the same thing in the console you'll see the text– Borisu
yesterday
But I’m not taking about returning anything. Running that code should print an empty array. For me it doesn’t. It does for you? I’ll have to check again.
– flyingL123
yesterday
But I’m not taking about returning anything. Running that code should print an empty array. For me it doesn’t. It does for you? I’ll have to check again.
– flyingL123
yesterday
Sorry about that, I was testing with a class where the method was public. I've edited the answer to show how to use it with protected methods.
– Borisu
yesterday
Sorry about that, I was testing with a class where the method was public. I've edited the answer to show how to use it with protected methods.
– Borisu
yesterday
But I’m not mocking the protected method. The protected method is an internal call. Since it’s a partial mock, the call to the public update method should call performUpdate internally as normal, but it’s not.
– flyingL123
yesterday
But I’m not mocking the protected method. The protected method is an internal call. Since it’s a partial mock, the call to the public update method should call performUpdate internally as normal, but it’s not.
– flyingL123
yesterday
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54995324%2fphp-mockery-partial-test-double-not-calling-method-from-parent-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown