Skip to content

Fix to be able to set phpunit-mock-objects stub to the return value of method - #170

Merged
kenjis merged 2 commits into
kenjis:masterfrom
ytetsuro:add-double-set-stub
Feb 13, 2017
Merged

Fix to be able to set phpunit-mock-objects stub to the return value of method#170
kenjis merged 2 commits into
kenjis:masterfrom
ytetsuro:add-double-set-stub

Conversation

@ytetsuro

Copy link
Copy Markdown
Contributor

I think that this pull request is effective when a chain method is described.

For example, in the case of the following processing

<?php
class Shop_model extends CI_Model {
...
     public function send_buy_mail()
     {
             return $this->email->to('foo@example.com', 'test')
                      ->subject('Thanks !')
                      ->message('Thank you very match!!')
                      ->send();
     }
}

It can be implemented as follows.

<?php
class Shop_model_test extends TestCase {
...
     public function test_send()
     {
             $this->shop_model->email = $this->getDouble('CI_Email', [
                   'to'      => $this->returnSelf(), //PHPUnit_Framework_MockObject_Stub_ReturnSelf
                   'subject' => $this->returnSelf(),
                   'message' => $this->returnSelf(),
                   'send'    => TRUE
             ]);
     }
}

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.05%) to 75.229% when pulling 8454743 on ytetsuro:add-double-set-stub into 70e3539 on kenjis:master.

@kenjis

kenjis commented Feb 13, 2017

Copy link
Copy Markdown
Owner

@ytetsuro Would you please add a test in https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/master/application/tests/_tests/CIPHPUnitTestDouble_test.php?

And it seems your code is not compatible with PHPUnit 6.0.
Now ci-phpunit-test supports PHPUnit 4.8, 5.7 and 6.0.
So probably the hack like this https://github.com/kenjis/ci-phpunit-test/blob/master/application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php#L11 is needed.

@kenjis

kenjis commented Feb 13, 2017

Copy link
Copy Markdown
Owner

@ytetsuro P.S. Not chain method, but method chaining.

@ytetsuro

ytetsuro commented Feb 13, 2017

Copy link
Copy Markdown
Contributor Author

@ytetsuro Would you please add a test in https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/master/application/tests/_tests/CIPHPUnitTestDouble_test.php?

@kenjis
Excuse me.
Pull Request has been sent.
kenjis/ci-app-for-ci-phpunit-test#11

And it seems your code is not compatible with PHPUnit 6.0.
Now ci-phpunit-test supports PHPUnit 4.8, 5.7 and 6.0.
So probably the hack like this https://github.com/kenjis/ci-phpunit-test/blob/master/application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php#L11 is needed.

I'm sorry.
Pull Request's my comment is bad.
It depends on phpunit-mock-objects rather than PHPUnit, i think will work without problem even though it is PHPUnit 6.0. . .

https://github.com/sebastianbergmann/phpunit/blob/6.0/composer.json#L28
https://github.com/sebastianbergmann/phpunit-mock-objects/blob/4.0.0/src/Stub.php#L19
https://github.com/ytetsuro/ci-phpunit-test/blob/845474367d98d75b4d6c954a5c4c8280fee97a4f/application/tests/_ci_phpunit_test/CIPHPUnitTestDouble.php#L60

P.S. Not chain method, but method chaining.

Thank you for your advice.

@ytetsuro ytetsuro changed the title Fix to be able to set PHPUnit stub to the return value of method Fix to be able to set phpunit-mock-objects stub to the return value of method Feb 13, 2017
kenjis added a commit to kenjis/ci-app-for-ci-phpunit-test that referenced this pull request Feb 13, 2017
@kenjis
kenjis merged commit 8454743 into kenjis:master Feb 13, 2017
@kenjis

kenjis commented Feb 13, 2017

Copy link
Copy Markdown
Owner

It depends on phpunit-mock-objects rather than PHPUnit, i think will work without problem even though it is PHPUnit 6.0. . .

Okay, you are right.

PHPUnit 6.0: https://travis-ci.org/kenjis/ci-phpunit-test/jobs/201314674
PHPUnit 5.7: https://travis-ci.org/kenjis/ci-phpunit-test/jobs/201314673

@ytetsuro

Copy link
Copy Markdown
Contributor Author

merged very thanks!!

@ytetsuro
ytetsuro deleted the add-double-set-stub branch February 14, 2017 01:22
@kenjis

kenjis commented Feb 14, 2017

Copy link
Copy Markdown
Owner

@ytetsuro

ytetsuro commented Feb 14, 2017

Copy link
Copy Markdown
Contributor Author

@kenjis

confirmed.
Thank you very much.

You can also set the mock itself as the return value of a mocked method with using $this->returnSelf().

However, with my change, you can set the return value of $this->returnXX methods other than $this->returnSelf to return, is there a problem?

The return value of $this-> returnXX is an object implements PHPUnit_Framework_MockObject_Stub.

Example)

  • PHPUnit_Framework_MockObject_Stub_ReturnValueMap
  • PHPUnit_Framework_MockObject_Stub_ReturnArgument
  • PHPUnit_Framework_MockObject_Stub_Exception
    etc...

My explanation is insufficient. very very sorry.

@kenjis

kenjis commented Feb 14, 2017

Copy link
Copy Markdown
Owner

@ytetsuro No problem. I intend only returnSelf for method chaining. So there is only one test for returnSelf.

I don't recommend you use returning like ReturnValueMap or ReturnArgument.
In that cases, I recommend you use closure.

@ytetsuro

Copy link
Copy Markdown
Contributor Author

@kenjis

Certainly.
Thank you for your response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants