Skip to content
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

Add tests for API_Rewrite::__construct() #217

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions tests/phpunit/tests/API_Rewrite/APIRewrite_ConstructTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Class APIRewrite_ConstructTest
*
* @package AspireUpdate
*/

/**
* Tests for API_Rewrite::__construct()
*
* @covers \AspireUpdate\API_Rewrite::__construct
*/
class APIRewrite_ConstructTest extends WP_UnitTestCase {
/**
* Test that hooks are added.
*
* @dataProvider data_hooks_and_methods
*
* @string $hook The hook's name.
* @string $method The method to hook.
*/
public function test_should_add_hooks( $hook, $method ) {
$api_rewrite = new AspireUpdate\API_Rewrite( 'debug', false );
$this->assertIsInt( has_action( $hook, [ $api_rewrite, $method ] ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_hooks_and_methods() {
return [
'pre_http_request -> pre_http_request' => [
'hook' => 'pre_http_request',
'method' => 'pre_http_request',
],
];
}

/**
* Test that properties are set to the expected value.
*
* @dataProvider data_properties_and_values
*
* @param string $property_name The property's name.
* @param string $passed_value The value passed to the constructor.
* @param string $expected_value The expected stored value after processing.
*/
public function test_should_set_properties( $property_name, $passed_value, $expected_value ) {
$api_rewrite = new AspireUpdate\API_Rewrite(
'redirected_host' === $property_name ? $passed_value : 'debug',
'disable_ssl' === $property_name ? $passed_value : false
);

if ( '%DEFAULT_HOST%' === $expected_value ) {
static $default_host_value;

if ( ! $default_host_value ) {
$api_rewrite_reflection = new ReflectionClass( 'AspireUpdate\API_Rewrite' );
$default_host_value = $api_rewrite_reflection->getDefaultProperties()['default_host'];
}

$expected_value = $default_host_value;
}

$property = new ReflectionProperty( $api_rewrite, $property_name );
$property->setAccessible( true );
$actual_value = $property->getValue( $api_rewrite );
$property->setAccessible( false );

$this->assertSame( $expected_value, $actual_value );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_properties_and_values() {
return [
'redirected_host as "debug" (should be the default host)' => [
'property_name' => 'redirected_host',
'passed_value' => 'debug',
'expected_value' => '%DEFAULT_HOST%',
],
'redirected_host in mixed case (should convert to lowercase)' => [
'property_name' => 'redirected_host',
'passed_value' => 'mY.aPi.OrG',
'expected_value' => 'my.api.org',
],
'malformed redirected_host (should be stored as-is)' => [
'property_name' => 'redirected_host',
'passed_value' => 'my#api..org/https://',
'expected_value' => 'my#api..org/https://',
],
'disable_ssl as (bool) true' => [
'property_name' => 'disable_ssl',
'passed_value' => true,
'expected_value' => true,
],
'disable_ssl as (bool) false' => [
'property_name' => 'disable_ssl',
'passed_value' => false,
'expected_value' => false,
],
];
}
}