Brain-WP / BrainMonkey

Mocking utility for PHP functions and WordPress plugin API
https://giuseppe-mazzapica.gitbook.io/brain-monkey/
MIT License
301 stars 29 forks source link

Expect Applied not working #50

Closed inonkp closed 5 years ago

inonkp commented 5 years ago

Running the following: \Brain\Monkey\Filters\expectApplied('some_filter')->once()->with('arg1', 1); apply_filters('some_filter','arg1',1) Yields: Mockery\Exception\InvalidCountException: Method apply_filters_some_filter('arg1', 1) from Mockery_1 should be called exactly 1 times but called 0 times.

C:\Users\qwerty\AppData\Local\Temp\wordpress-tests-lib\vendor\mockery\mockery\library\Mockery\CountValidator\Exact.php:38 C:\Users\qwerty\AppData\Local\Temp\wordpress-tests-lib\vendor\mockery\mockery\library\Mockery\Expectation.php:310 C:\Users\qwerty\AppData\Local\Temp\wordpress-tests-lib\vendor\mockery\mockery\library\Mockery\ExpectationDirector.php:119 C:\Users\qwerty\AppData\Local\Temp\wordpress-tests-lib\vendor\mockery\mockery\library\Mockery\Container.php:303 C:\Users\qwerty\AppData\Local\Temp\wordpress-tests-lib\vendor\mockery\mockery\library\Mockery\Container.php:288 C:\Users\qwerty\AppData\Local\Temp\wordpress-tests-lib\vendor\mockery\mockery\library\Mockery.php:204 C:\Users\qwerty\AppData\Local\Temp\wordpress-tests-lib\vendor\brain\monkey\inc\api.php:38 C:\xampp72\htdocs\mca\wp-content\plugins\etsy_woocommerce\tests\EtsyProductTest.php:29

My setup: phpunit 7.5.11 php 7.2.18

widoz commented 5 years ago

@inonkp just to be sure did you setup it https://brain-wp.github.io/BrainMonkey/docs/wordpress-setup.html ?

inonkp commented 5 years ago

This is my setup:

    public function setUp(): void  {
    \Brain\Monkey\setUp()
    $this->mock = \Mockery::mock("SomeOtherClass")->makePartial()->shouldAllowMockingProtectedMethods();
    $this->mock->mock_init();
    $this->utils = $this->mock->mock_get_utils();
    parent::setUp();

}

public function tearDown(): void {
    parent::tearDown();
    \Brain\Monkey\tearDown();
}
gmazzap commented 5 years ago

Hi @inonkp

I tried to reproduce your issue using your exact same version of PHP and PHPUnit.

I wanted to also use your setup, but this->mock->mock_init() and $this->mock->mock_get_utils() look like custom methods of your SomeOtherClass so I could not use those.

Moreover, normally you should probably call parent::setUp(); before anything else.

This is the setup I used (literal copy & paste):

class TestCase extends \PHPUnit\Framework\TestCase
{
    use \Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

    private $mock;
    private $utils;

    public function setUp(): void
    {
        parent::setUp();
        Brain\Monkey\setUp();

        $this->mock = \Mockery::mock("SomeOtherClass")
            ->makePartial()
            ->shouldAllowMockingProtectedMethods();
    }

    public function tearDown(): void
    {
        parent::tearDown();
        \Brain\Monkey\tearDown();
    }
}

And this is the test code I used (again c&p):

class MyTest extends TestCase {

    public function testIssue50()
    {
        Brain\Monkey\Filters\expectApplied('some_filter')->once()->with('arg1', 1);

        apply_filters('some_filter', 'arg1', 1);
    }
}

When I run the test I get:

Time: 240 ms, Memory: 8.00 MB

OK (1 test, 1 assertion)

So I could not reproduce the issue.

Can you please double check that what you pasted here is your exact code? Can you also check that the custom methods used in your setup are not the cause of the issue?

inonkp commented 5 years ago

Thanks for taking the time to answer my question. I will double check.

On Mon, Jul 15, 2019 at 9:46 AM Giuseppe Mazzapica notifications@github.com wrote:

Hi @inonkp https://github.com/inonkp

I tried to reproduce your issue using your exact same version of PHP and PHPUnit.

I wanted to also use your setup, but this->mock->mock_init() and $this->mock->mock_get_utils() look like custom methods of your SomeOtherClass so I could not use those.

Moreover, normally you should probably call parent::setUp(); before anything else.

This is the setup I used (literal copy & paste):

class TestCase extends \PHPUnit\Framework\TestCase{ use \Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; private $mock; private $utils; public function setUp(): void { parent::setUp(); Brain\Monkey\setUp(); $this->mock = \Mockery::mock("SomeOtherClass") ->makePartial() ->shouldAllowMockingProtectedMethods(); } public function tearDown(): void { parent::tearDown(); \Brain\Monkey\tearDown(); }}

And this is the test code I used (again c&p):

class MyTest extends TestCase { public function testIssue50() { Brain\Monkey\Filters\expectApplied('some_filter')->once()->with('arg1', 1); apply_filters('some_filter', 'arg1', 1); }}

When I run the test I get:

Time: 240 ms, Memory: 8.00 MB

OK (1 test, 1 assertion)

So I could not reproduce the issue.

Can you please double check that what you pasted here is your exact code? Can you also check that the custom methods used in your setup are not the cause of the issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Brain-WP/BrainMonkey/issues/50?email_source=notifications&email_token=ADF64TU77D4MGKSFK76E2VDP7QMN3A5CNFSM4ICFFRQ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZ42WRQ#issuecomment-511290182, or mute the thread https://github.com/notifications/unsubscribe-auth/ADF64TT4S5WURAQNUEVUAI3P7QMN3ANCNFSM4ICFFRQQ .

inonkp commented 5 years ago

I have commented out all unrelevant code to the test. This is what im running:

<?php

class SomeTest extends \PHPUnit\Framework\TestCase{ /*

\Brain\Monkey\Filters\expectApplied('some_filter')->once()->with('arg1', 1);

    apply_filters('some_filter', 'arg1', 1);
}

}

But the problem persists. The problem must be in my setup, composer-wise, and/or how I bootstrap phphunit..

On Mon, Jul 15, 2019 at 9:54 AM Inon inonkp@gmail.com wrote:

Thanks for taking the time to answer my question. I will double check.

On Mon, Jul 15, 2019 at 9:46 AM Giuseppe Mazzapica < notifications@github.com> wrote:

Hi @inonkp https://github.com/inonkp

I tried to reproduce your issue using your exact same version of PHP and PHPUnit.

I wanted to also use your setup, but this->mock->mock_init() and $this->mock->mock_get_utils() look like custom methods of your SomeOtherClass so I could not use those.

Moreover, normally you should probably call parent::setUp(); before anything else.

This is the setup I used (literal copy & paste):

class TestCase extends \PHPUnit\Framework\TestCase{ use \Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; private $mock; private $utils; public function setUp(): void { parent::setUp(); Brain\Monkey\setUp(); $this->mock = \Mockery::mock("SomeOtherClass") ->makePartial() ->shouldAllowMockingProtectedMethods(); } public function tearDown(): void { parent::tearDown(); \Brain\Monkey\tearDown(); }}

And this is the test code I used (again c&p):

class MyTest extends TestCase { public function testIssue50() { Brain\Monkey\Filters\expectApplied('some_filter')->once()->with('arg1', 1); apply_filters('some_filter', 'arg1', 1); }}

When I run the test I get:

Time: 240 ms, Memory: 8.00 MB

OK (1 test, 1 assertion)

So I could not reproduce the issue.

Can you please double check that what you pasted here is your exact code? Can you also check that the custom methods used in your setup are not the cause of the issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Brain-WP/BrainMonkey/issues/50?email_source=notifications&email_token=ADF64TU77D4MGKSFK76E2VDP7QMN3A5CNFSM4ICFFRQ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZ42WRQ#issuecomment-511290182, or mute the thread https://github.com/notifications/unsubscribe-auth/ADF64TT4S5WURAQNUEVUAI3P7QMN3ANCNFSM4ICFFRQQ .

gmazzap commented 5 years ago

In the zip file below:

brain-monkey-issue-50.zip

You can fin the test code I used.

Unzip, open a terminal to the folder in it and run composer install.

It will install dependencies and then automatically run the test.

It works perfectly for me. If it does not for you, then it might be something on the machine, e.g. some global Composer plugin.

inonkp commented 5 years ago

Thanks will try.

On Wed, Jul 17, 2019 at 1:46 PM Giuseppe Mazzapica notifications@github.com wrote:

In the zip file below:

brain-monkey-issue-50.zip https://github.com/Brain-WP/BrainMonkey/files/3401441/brain-monkey-issue-50.zip

You can fin the test code I used.

Unzip, open a terminal to the folder in it and run composer install.

It will install dependencies and then automatically run the test.

It works perfectly for me. If it does not for you, then it might be something on the machine, e.g. some global Composer plugin.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Brain-WP/BrainMonkey/issues/50?email_source=notifications&email_token=ADF64TWKVMXCD47BPKVHZNLP73Z7TA5CNFSM4ICFFRQ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2DZQMI#issuecomment-512202801, or mute the thread https://github.com/notifications/unsubscribe-auth/ADF64TQLBFXO57CWYG3K2RLP73Z7TANCNFSM4ICFFRQQ .

inonkp commented 5 years ago

Works. Do you think its because of the phpunit version? Probably not... Please note im running wordpress plugin tests according to this tutorial: https://make.wordpress.org/cli/handbook/plugin-unit-tests/ Im pretty sure the reason is somewhere there.

On Wed, Jul 17, 2019 at 1:47 PM Inon inonkp@gmail.com wrote:

Thanks will try.

On Wed, Jul 17, 2019 at 1:46 PM Giuseppe Mazzapica < notifications@github.com> wrote:

In the zip file below:

brain-monkey-issue-50.zip https://github.com/Brain-WP/BrainMonkey/files/3401441/brain-monkey-issue-50.zip

You can fin the test code I used.

Unzip, open a terminal to the folder in it and run composer install.

It will install dependencies and then automatically run the test.

It works perfectly for me. If it does not for you, then it might be something on the machine, e.g. some global Composer plugin.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Brain-WP/BrainMonkey/issues/50?email_source=notifications&email_token=ADF64TWKVMXCD47BPKVHZNLP73Z7TA5CNFSM4ICFFRQ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2DZQMI#issuecomment-512202801, or mute the thread https://github.com/notifications/unsubscribe-auth/ADF64TQLBFXO57CWYG3K2RLP73Z7TANCNFSM4ICFFRQQ .

gmazzap commented 5 years ago

@inonkp I used the exact same PHPUnit version that you mentioned in your first comment,7.5.11

I think the problem is the WordPress plugin unit tests.

Brain Monkey is designed to run tests without WordPress. If you follow the tutorial you linked, you will run tests with WordPress.

The two ways are not meant to be used together.

You can run unit tests without WordPress, using Brain Monkey, and then run integration/system/e2e tests with WordPress.

inonkp commented 5 years ago

I understand, thanks for taking the time for me.

On Mon, Jul 22, 2019 at 10:04 AM Giuseppe Mazzapica < notifications@github.com> wrote:

@inonkp https://github.com/inonkp I used the exact same PHPUnit version that you mentioned in your first comment,7.5.11

I think the problem is the WordPress plugin unit tests.

Brain Monkey is designed to run tests without WordPress. If you follow the tutorial you linked, you will run tests with WordPress.

The two ways are not meant to be used together.

You can run unit tests without WordPress, using Brain Monkey, and then run integration/system/e2e tests with WordPress.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Brain-WP/BrainMonkey/issues/50?email_source=notifications&email_token=ADF64TQM4SBNQLEZIGZJWE3QAVLXTA5CNFSM4ICFFRQ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2O7CTA#issuecomment-513667404, or mute the thread https://github.com/notifications/unsubscribe-auth/ADF64TVOHC2QJVRMELZ56LTQAVLXTANCNFSM4ICFFRQQ .

gmazzap commented 5 years ago

No problem.

I'm closing this. @inonkp Feel free to re-open in case you need more info on this.