Skip to content

Commit

Permalink
Remove WP_Mock from the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mallorydxw committed Aug 21, 2020
1 parent 279c8bc commit dd477ad
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 180 deletions.
6 changes: 5 additions & 1 deletion wp-content/themes/theme/app/Theme/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public function __construct(array $required = [])
{
$this->required = $required;
$this->path_to_wordpress = ABSPATH;
// For the tests
$this->requireOnce = function ($a) {
require_once($a);
};
}

public function register()
Expand All @@ -27,7 +31,7 @@ public function checkDependencies()
return;
}
if (!function_exists('get_plugin_data')) {
require_once($this->path_to_wordpress . 'wp-admin/includes/plugin.php');
call_user_func($this->requireOnce, $this->path_to_wordpress . 'wp-admin/includes/plugin.php');
}
array_map([$this, 'addNotice'], $pluginsToActivate);
}
Expand Down
2 changes: 0 additions & 2 deletions wp-content/themes/theme/peridot.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

WP_Mock::bootstrap();

return function (\Evenement\EventEmitterInterface $emitter) {
$dot = new \Peridot\Reporter\Dot\DotReporterPlugin($emitter);
};
11 changes: 7 additions & 4 deletions wp-content/themes/theme/spec/lib/whippet/template_tags.spec.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php

describe(\Theme\Lib\Whippet\TemplateTags::class, function () {
namespace Theme\Lib\Whippet;

use \phpmock\mockery\PHPMockery;

describe(TemplateTags::class, function () {
beforeEach(function () {
\WP_Mock::setUp();
$this->helpersMock = Mockery::mock(\Dxw\Iguana\Theme\Helpers::class);
$this->helpersMock = \Mockery::mock(\Dxw\Iguana\Theme\Helpers::class);
$this->templateTags = new \Theme\Lib\Whippet\TemplateTags(
$this->helpersMock
);
});

afterEach(function () {
\WP_Mock::tearDown();
\Mockery::close();
});

describe('->w_template_title()', function () {
Expand Down
9 changes: 6 additions & 3 deletions wp-content/themes/theme/spec/posts/custom_fields.spec.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

describe(\Theme\Posts\CustomFields::class, function () {
namespace Theme\Posts;

use \phpmock\mockery\PHPMockery;

describe(CustomFields::class, function () {
beforeEach(function () {
\WP_Mock::setUp();
$this->customFields = new \Theme\Posts\CustomFields();
});

afterEach(function () {
\WP_Mock::tearDown();
\Mockery::close();
});

it('is registrable', function () {
Expand Down
9 changes: 6 additions & 3 deletions wp-content/themes/theme/spec/posts/post_types.spec.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

describe(\Theme\Posts\PostTypes::class, function () {
namespace Theme\Posts;

use \phpmock\mockery\PHPMockery;

describe(PostTypes::class, function () {
beforeEach(function () {
\WP_Mock::setUp();
$this->postTypes = new \Theme\Posts\PostTypes();
});

afterEach(function () {
\WP_Mock::tearDown();
\Mockery::close();
});

it('is registrable', function () {
Expand Down
11 changes: 7 additions & 4 deletions wp-content/themes/theme/spec/theme/analytics.spec.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

describe(\Theme\Theme\Analytics::class, function () {
namespace Theme\Theme;

use \phpmock\mockery\PHPMockery;

describe(Analytics::class, function () {
beforeEach(function () {
\WP_Mock::setUp();
$this->analytics = new \Theme\Theme\Analytics();
});

afterEach(function () {
\WP_Mock::tearDown();
\Mockery::close();
});

it('is registrable', function () {
Expand All @@ -16,7 +19,7 @@

describe('->register()', function () {
it('registers actions', function () {
WP_Mock::expectActionAdded('wp_footer', [$this->analytics, 'wpFooter']);
PHPMockery::mock(__NAMESPACE__, 'add_action')->with('wp_footer', [$this->analytics, 'wpFooter'])->once();
$this->analytics->register();
});
});
Expand Down
21 changes: 9 additions & 12 deletions wp-content/themes/theme/spec/theme/media.spec.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

describe(\Theme\Theme\Media::class, function () {
namespace Theme\Theme;

use \phpmock\mockery\PHPMockery;

describe(Media::class, function () {
beforeEach(function () {
\WP_Mock::setUp();
$this->media = new \Theme\Theme\Media();
});

afterEach(function () {
\WP_Mock::tearDown();
\Mockery::close();
});

it('is registrable', function () {
Expand All @@ -16,15 +19,9 @@

describe('->register()', function () {
it('registers thumbnail sizes', function () {
\WP_Mock::wpFunction('set_post_thumbnail_size', [
'args' => [\WP_Mock\Functions::type('int'), \WP_Mock\Functions::type('int'), \WP_Mock\Functions::type('bool')],
'times' => 1
]);

\WP_Mock::wpFunction('add_image_size', [
'args' => [\WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('int'), \WP_Mock\Functions::type('int'), \WP_Mock\Functions::type('bool')],
'times' => 2
]);
PHPMockery::mock(__NAMESPACE__, 'set_post_thumbnail_size')->with(\Mockery::type('int'), \Mockery::type('int'), \Mockery::type('bool'))->times(1);

PHPMockery::mock(__NAMESPACE__, 'add_image_size')->with(\Mockery::type('string'), \Mockery::type('int'), \Mockery::type('int'), \Mockery::type('bool'))->times(2);

$this->media->register();
});
Expand Down
14 changes: 7 additions & 7 deletions wp-content/themes/theme/spec/theme/menus.spec.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

describe(\Theme\Theme\Menus::class, function () {
namespace Theme\Theme;

use \phpmock\mockery\PHPMockery;

describe(Menus::class, function () {
beforeEach(function () {
\WP_Mock::setUp();
$this->menus = new \Theme\Theme\Menus();
});

afterEach(function () {
\WP_Mock::tearDown();
\Mockery::close();
});

it('is registrable', function () {
Expand All @@ -16,10 +19,7 @@

describe('->register()', function () {
it('registers nav menus', function () {
\WP_Mock::wpFunction('register_nav_menu', [
'args' => [\WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('string')],
'times' => 2
]);
PHPMockery::mock(__NAMESPACE__, 'register_nav_menu')->with(\Mockery::type('string'), \Mockery::type('string'))->times(2);

$this->menus->register();
});
Expand Down
12 changes: 6 additions & 6 deletions wp-content/themes/theme/spec/theme/pagination.spec.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

describe(\Theme\Theme\Pagination::class, function () {
beforeEach(function () {
\WP_Mock::setUp();
});
namespace Theme\Theme;

use \phpmock\mockery\PHPMockery;

describe(Pagination::class, function () {
afterEach(function () {
\WP_Mock::tearDown();
\Mockery::close();
});

it('registers the function in the constructor', function () {
$helpersMock = Mockery::mock(\Dxw\Iguana\Theme\Helpers::class);
$helpersMock = \Mockery::mock(\Dxw\Iguana\Theme\Helpers::class);
$helpersMock->shouldReceive('registerFunction')->once();
$pagination = new \Theme\Theme\Pagination($helpersMock);
});
Expand Down
107 changes: 49 additions & 58 deletions wp-content/themes/theme/spec/theme/plugins.spec.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
<?php

describe(\Theme\Theme\Plugins::class, function () {
namespace Theme\Theme;

use \phpmock\mockery\PHPMockery;

describe(Plugins::class, function () {
beforeEach(function () {
\WP_Mock::setUp();
\WP_Mock::wpFunction('esc_html', [
'return' => function ($a) {
return '_'.$a.'_';
},
]);
PHPMockery::mock(__NAMESPACE__, 'esc_html')->andReturnUsing(function ($a) {
return '_'.$a.'_';
});
PHPMockery::mock(__NAMESPACE__, 'apply_filters')->andReturnUsing(function ($a, $b) {
return $b;
});
if (!defined('WP_PLUGIN_DIR')) {
define('WP_PLUGIN_DIR', '/path/to/plugins');
}
if (!defined('ABSPATH')) {
define('ABSPATH', '/abspath');
}
});

afterEach(function () {
\WP_Mock::tearDown();
\Mockery::close();
});

it('is registrable', function () {
Expand All @@ -25,43 +32,33 @@
describe('->register()', function () {
it('registers theme activation hook', function () {
$plugins = new \Theme\Theme\Plugins([]);
\WP_Mock::expectActionAdded('after_switch_theme', [$plugins, 'checkDependencies']);
PHPMockery::mock(__NAMESPACE__, 'add_action')->with('after_switch_theme', [$plugins, 'checkDependencies'])->once();
$plugins->register();
});
});

describe('->checkDependencies()', function () {
it('flags any required plugins that aren\'t activated', function () {
WP_Mock::wpFunction('get_option', [
'args' => ['active_plugins'],
'times' => 1,
'return' => [
'some-other/plugin.php'
]
]);
WP_Mock::wpFunction('get_plugin_data', [
'args' => [WP_PLUGIN_DIR.'/path-to/a-required-plugin.php'],
'times' => 1,
'return' => [
'Name' => 'A plugin'
]
PHPMockery::mock(__NAMESPACE__, 'get_option')->with('active_plugins')->times(1)->andReturn([
'some-other/plugin.php',
]);
WP_Mock::wpFunction('get_plugin_data', [
'args' => [WP_PLUGIN_DIR.'/advanced-custom-fields-pro/acf.php'],
'times' => 1,
'return' => [
'Name' => 'Advanced Custom Fields Pro'
]
$getPluginData = PHPMockery::mock(__NAMESPACE__, 'get_plugin_data');
$getPluginData->with(WP_PLUGIN_DIR.'/path-to/a-required-plugin.php')->times(1)->andReturn([
'Name' => 'A plugin',
]);
WP_Mock::wpFunction('admin_url', [
'args' => ['plugins.php'],
'times' => 2,
'return' => 'http://localhost/wp-admin/plugins.php'
$getPluginData->with(WP_PLUGIN_DIR.'/advanced-custom-fields-pro/acf.php')->times(1)->andReturn([
'Name' => 'Advanced Custom Fields Pro',
]);
PHPMockery::mock(__NAMESPACE__, 'admin_url')->with('plugins.php')->times(2)->andReturn('http://localhost/wp-admin/plugins.php');
$plugins = new \Theme\Theme\Plugins([
'path-to/a-required-plugin.php',
'advanced-custom-fields-pro/acf.php'
]);

// Prevent checkDependencies() from running require_once()
$plugins->requireOnce = function () {
};

ob_start();
$plugins->checkDependencies();
$result = ob_get_contents();
Expand All @@ -76,19 +73,20 @@

context('when the plugins are already active', function () {
it('doesn\'t print anything', function () {
WP_Mock::wpFunction('get_option', [
'args' => ['active_plugins'],
'times' => 1,
'return' => [
'some-other/plugin.php',
'path-to/a-required-plugin.php',
'advanced-custom-fields-pro/acf.php'
]
PHPMockery::mock(__NAMESPACE__, 'get_option')->with('active_plugins')->times(1)->andReturn([
'some-other/plugin.php',
'path-to/a-required-plugin.php',
'advanced-custom-fields-pro/acf.php',
]);
$plugins = new \Theme\Theme\Plugins([
'path-to/a-required-plugin.php',
'advanced-custom-fields-pro/acf.php'
]);

// Prevent checkDependencies() from running require_once()
$plugins->requireOnce = function () {
};

ob_start();
$plugins->checkDependencies();
$result = ob_get_contents();
Expand All @@ -99,30 +97,23 @@

context('when there\'s no plugin data available', function () {
it('displays the path of the plugin instead', function () {
WP_Mock::wpFunction('get_option', [
'args' => ['active_plugins'],
'times' => 1,
'return' => [
'some-other/plugin.php',
'advanced-custom-fields-pro/acf.php'
]
PHPMockery::mock(__NAMESPACE__, 'get_option')->with('active_plugins')->times(1)->andReturn([
'some-other/plugin.php',
'advanced-custom-fields-pro/acf.php',
]);
WP_Mock::wpFunction('get_plugin_data', [
'args' => [WP_PLUGIN_DIR.'/path-to/a-required-plugin.php'],
'times' => 1,
'return' => [
'Name' => ''
]
]);
WP_Mock::wpFunction('admin_url', [
'args' => ['plugins.php'],
'times' => 1,
'return' => 'http://localhost/wp-admin/plugins.php'
PHPMockery::mock(__NAMESPACE__, 'get_plugin_data')->with(WP_PLUGIN_DIR.'/path-to/a-required-plugin.php')->times(1)->andReturn([
'Name' => '',
]);
PHPMockery::mock(__NAMESPACE__, 'admin_url')->with('plugins.php')->times(1)->andReturn('http://localhost/wp-admin/plugins.php');
$plugins = new \Theme\Theme\Plugins([
'path-to/a-required-plugin.php',
'advanced-custom-fields-pro/acf.php'
]);

// Prevent checkDependencies() from running require_once()
$plugins->requireOnce = function () {
};

ob_start();
$plugins->checkDependencies();
$result = ob_get_contents();
Expand Down
Loading

0 comments on commit dd477ad

Please sign in to comment.