-
Notifications
You must be signed in to change notification settings - Fork 27
Introduce Functions\stubWpUrlFunctions() helper #129
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
842ed35
0e63888
fe08d2a
d2f3281
9a56920
1921ed1
1991a83
dfffa27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ function tearDown() | |
| use Brain\Monkey\Container; | ||
| use Brain\Monkey\Expectation\EscapeHelper; | ||
| use Brain\Monkey\Expectation\FunctionStubFactory; | ||
| use Brain\Monkey\Expectation\UrlsHelper; | ||
| use Brain\Monkey\Name\FunctionName; | ||
|
|
||
| /** | ||
|
|
@@ -182,6 +183,39 @@ function stubEscapeFunctions() | |
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Stub URL-related functions with default behavior. | ||
| */ | ||
| function stubWpUrlFunctions($domain = 'example.org', $use_https = null) | ||
| { | ||
| $helper = new UrlsHelper($domain, $use_https); | ||
|
|
||
| stubs([ | ||
| 'home_url' => $helper->stubUrlCallback(), | ||
| 'get_home_url' => $helper->stubUrlForSiteCallback(), | ||
| 'site_url' => $helper->stubUrlCallback(), | ||
| 'get_site_url' => $helper->stubUrlForSiteCallback(), | ||
| 'admin_url' => $helper->stubUrlCallback('wp-admin', 'admin'), | ||
| 'get_admin_url' => $helper->stubUrlForSiteCallback('wp-admin', 'admin'), | ||
| 'content_url' => $helper->stubUrlCallback('wp-content', null, false), | ||
| 'rest_url' => $helper->stubUrlCallback('wp-json'), | ||
| 'get_rest_url' => $helper->stubUrlForSiteCallback('wp-json'), | ||
| 'includes_url' => $helper->stubUrlCallback('wp-includes'), | ||
| 'network_home_url' => $helper->stubUrlCallback(), | ||
| 'network_site_url' => $helper->stubUrlCallback(), | ||
| 'network_admin_url' => $helper->stubUrlCallback('wp-admin/network', 'admin'), | ||
| 'user_admin_url' => $helper->stubUrlCallback('wp-admin/user', 'admin'), | ||
| 'wp_login_url' => static function ($redirect = '', $force_reauth = false) use ($helper) { | ||
|
Comment on lines
+195
to
+209
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like most of these don't take passed parameters to the original function into account, while WP does support passing parameters. Stubbing these in the framework without taking the passes parameters into account will probably be confusing and lead to support overhead or to people just not using these stubs as they don't do what they expect.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jrfnl I think you need to look at it again :)
There are 2 exceptions:
To be noted:
TL;DR: all the stubbed functions accept the same parameters and work very similarly to the WP counterparts. For some edge cases, it might be needed to stub You can even see the parameters used with success in tests: https://github.com/Brain-WP/BrainMonkey/blob/feature/stub-wp-urls/tests/cases/unit/Api/FunctionsTest.php#L439-L455 |
||
| $callback = $helper->stubUrlCallback(); | ||
| $url = $callback('/wp-login.php', 'login'); | ||
gmazzap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $has_redirect = ($redirect !== '') && is_string($redirect); | ||
| $has_redirect and $url .= '?redirect_to=' . urlencode($redirect); | ||
| $force_reauth and $url .= ($has_redirect ? '&reauth=1' : '?reauth=1'); | ||
gmazzap marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return $url; | ||
| }, | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| namespace Brain\Monkey\Actions { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| <?php | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All If we would add Psalm or similar, as soon I would declare I prefer to keep the check at runtime so all the parameters are mixed, and IMO there's no point in typing "mixed" all over the place. Anyway, I added |
||
| /* | ||
| * This file is part of the BrainMonkey package. | ||
| * | ||
| * (c) Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Brain\Monkey\Expectation; | ||
|
|
||
| class UrlsHelper | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| private $domain; | ||
|
|
||
| /** | ||
| * @var bool|null | ||
| */ | ||
| private $use_https; | ||
|
|
||
| /** | ||
| * @param $domain | ||
| * @param $use_https | ||
| */ | ||
| public function __construct($domain = 'example.org', $use_https = null) | ||
| { | ||
| $this->domain = (is_string($domain) && $domain) ? $domain : 'example.org'; | ||
gmazzap marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $this->use_https = ($use_https === null) | ||
| ? null | ||
| : (bool)filter_var($use_https, FILTER_VALIDATE_BOOLEAN); | ||
| } | ||
|
|
||
| /** | ||
| * @param $base_path | ||
| * @param $for_admin | ||
| * @return \Closure | ||
| */ | ||
| public function stubUrlForSiteCallback($base_path = '', $def_schema = null) | ||
| { | ||
| return function ($site_id, $path = '', $schema = null) use ($base_path, $def_schema) { | ||
| ($def_schema && $schema === null) and $schema = $def_schema; | ||
| return $this->build_url( | ||
| $this->build_relative_path($base_path, $path), | ||
| $this->determineSchema($schema) | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param $base_path | ||
| * @param $for_admin | ||
| * @param $use_schema_arg | ||
| * @return \Closure | ||
| */ | ||
| public function stubUrlCallback($base_path = '', $def_schema = null, $use_schema_arg = true) | ||
| { | ||
| return function ($path = '', $schema = null) use ($base_path, $def_schema, $use_schema_arg) { | ||
| ($def_schema && $schema === null) and $schema = $def_schema; | ||
| return $this->build_url( | ||
| $this->build_relative_path($base_path, $path), | ||
| $this->determineSchema($use_schema_arg ? $schema : null) | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param $relative | ||
| * @param $schema | ||
| * @return mixed|string | ||
| */ | ||
| private function build_url($relative, $schema) | ||
| { | ||
| return ($schema === null) ? ($relative ?: '/') : $schema . $this->domain . $relative; | ||
| } | ||
|
|
||
| /** | ||
| * @param $base_path | ||
| * @param $path | ||
| * @return string | ||
| */ | ||
| private function build_relative_path($base_path, $path) | ||
| { | ||
| $path = (($path !== '') && is_string($path)) | ||
| ? '/' . ltrim($path, '/') | ||
| : ''; | ||
| $base_path = (($base_path !== '') && is_string($base_path)) | ||
| ? '/' . trim($base_path, '/') | ||
| : ''; | ||
|
|
||
| return $base_path . $path; | ||
| } | ||
|
|
||
| /** | ||
| * @param $schema_argument | ||
| * @return string|null | ||
| */ | ||
| private function determineSchema($schema_argument = null) | ||
| { | ||
| if ($schema_argument === 'relative') { | ||
| return null; | ||
| } | ||
|
|
||
| $use_https = $this->use_https; | ||
| $is_ssl = function_exists('is_ssl') ? is_ssl() : true; | ||
| if ($use_https === null && !in_array($schema_argument, ['http', 'https'], true)) { | ||
| $use_https = $is_ssl; | ||
| if ( | ||
| !$use_https | ||
| && in_array($schema_argument, ['admin', 'login', 'login_post', 'rpc']) | ||
| && function_exists('force_ssl_admin') | ||
| ) { | ||
| $use_https = force_ssl_admin(); | ||
| } | ||
| } | ||
| if ($schema_argument === 'http') { | ||
| $use_https = false; | ||
| } | ||
|
|
||
| return $use_https ? 'https://' : 'http://'; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.