-
Notifications
You must be signed in to change notification settings - Fork 29
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
Introduce Functions\stubWpUrlFunctions() helper #129
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
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 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 <[email protected]> | ||
* | ||
* 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 | ||
{ | ||
const DEFAULT_DOMAIN = 'example.org'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $domain; | ||
|
||
/** | ||
* @var bool|null | ||
*/ | ||
private $use_https; | ||
|
||
/** | ||
* @param mixed $domain | ||
* @param mixed $use_https | ||
*/ | ||
public function __construct($domain = null, $use_https = null) | ||
{ | ||
$this->domain = (is_string($domain) && $domain !== '') ? $domain : self::DEFAULT_DOMAIN; | ||
$this->use_https = ($use_https === null) | ||
? null | ||
: (bool)filter_var($use_https, FILTER_VALIDATE_BOOLEAN); | ||
} | ||
|
||
/** | ||
* @param mixed $base_path | ||
* @param mixed $def_schema | ||
* @return \Closure | ||
*/ | ||
public function stubUrlForSiteCallback($base_path = '', $def_schema = null) | ||
{ | ||
return function ($site_id, $path = '', $schema = null) use ($base_path, $def_schema) { | ||
if (is_string($def_schema) && ($def_schema !== '') && ($schema === null)) { | ||
$schema = $def_schema; | ||
} | ||
return $this->build_url( | ||
$this->build_relative_path($base_path, $path), | ||
$this->determineSchema($schema) | ||
); | ||
}; | ||
} | ||
|
||
/** | ||
* @param mixed $base_path | ||
* @param mixed $def_schema | ||
* @param mixed $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 string $relative | ||
* @param string|null $schema | ||
* @return string | ||
*/ | ||
private function build_url($relative, $schema) | ||
{ | ||
return ($schema === null) | ||
? (($relative === '') ? '/' : $relative) | ||
: $schema . $this->domain . $relative; | ||
} | ||
|
||
/** | ||
* @param mixed $base_path | ||
* @param mixed $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 mixed $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://'; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrfnl I think you need to look at it again :)
$helper->stubUrlCallback()
and$helper->stubUrlForSiteCallback()
return callbacks that take respectively 2 ($path
,$schema
) and 3 parameters ($blog_id
,$path
,$schema
), that are the same core functions take.There are 2 exceptions:
content_url()
andwp_login_url()
.wp_login_url()
takes still 2 arguments, but different from the others, and that is why it is stubbed differently.content_url()
takes 1 parameter:$path
. That is whystubUrlCallback()
accepts a 3rd parameter$use_schema_arg
that defaults totrue
, but it is passed asfalse
forcontent_url()
.To be noted:
$blog_id
parameter for functions that accepts it is just ignored in stubbed functions because the base URL is taken from what is passed to thestubWpUrlFunctions()
API function.$schema
param is something like'admin'
or'login'
it is taken into account only if HTTPs is not forced (passingtrue
asstubWpUrlFunctions()
's 2nd parameter) andforce_ssl_admin()
is available (likely because stubbed separately).rest_url()
andget_rest_url()
use'rest'
as scheme. That is not relevant for the generation of the URL (in WP just like in the stubbed version), it is used in WP only as the 3rd parameter,$orig_scheme
, passed to the'set_url_scheme'
hook, something that I think is pretty safe to ignore for the stubbed version of the function.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
is_ssl()
and/orforce_ssl_admin()
separately.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