Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
build
vendor
yarn-error.log
.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"scripts": {
"lint:php": "phpcs .",
"lint:php-fix": "phpcbf .",
"test:php": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml.dist"
"test:php": "phpunit --dont-report-useless-tests --configuration ./phpunit.xml --testdox"
}
}
7 changes: 0 additions & 7 deletions helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
*/
class Helper {

/**
* Name of flag environment.
*
* @var string $env_option_name
*/
public static $env_option_name = 'mr_feature_flags_env';

/**
* Flag search helper.
*
Expand Down
61 changes: 1 addition & 60 deletions includes/Api/FlagOptions.php → includes/Api/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @package mr-feature-flags
* @since 1.0.0
*/
class FlagOptions {
class Flags {

/**
* Name in options table.
Expand All @@ -25,13 +25,6 @@ class FlagOptions {
*/
public static $option_name = 'mr_feature_flags';

/**
* Name of flag environment.
*
* @var string $env_option_name
*/
public static $env_option_name = 'mr_feature_flags_env';

/**
* Register feature flag endpoints.
*
Expand All @@ -58,19 +51,6 @@ function () {
],
]
);

register_rest_route(
'feature-flags/v1',
'flags/env',
[
[
'methods' => \WP_REST_SERVER::READABLE,
'callback' => [ $this, 'get_flag_env' ],
'permission_callback' => '__return_true',
],
]
);

}
);
}
Expand Down Expand Up @@ -114,43 +94,4 @@ public function post_flags( $request ) {
}
}

/**
* Get Feature Flag environment.
*
* @return mixed List of flags.
*/
public function get_flag_env() {
$env = get_option( self::$env_option_name );

if ( empty( $env ) ) {
return rest_ensure_response( [ 'env' => 'prod' ] );
}

return rest_ensure_response( $env );
}

/**
* Register settings action method.
*
* @return void
* @since 1.0.0
*/
public function register_settings() {

add_menu_page(
'Feature Flags',
'Feature Flags',
'manage_options',
'mr-feature-flags',
[ $this, 'render_page' ],
'data:image/svg+xml;base64,' . base64_encode( '<svg width="15" height="18" viewBox="0 0 2000 1792" xmlns="http://www.w3.org/2000/svg"><path fill="black" d="M0 896q0-130 51-248.5t136.5-204 204-136.5 248.5-51h768q130 0 248.5 51t204 136.5 136.5 204 51 248.5-51 248.5-136.5 204-204 136.5-248.5 51h-768q-130 0-248.5-51t-204-136.5-136.5-204-51-248.5zm1408 512q104 0 198.5-40.5t163.5-109.5 109.5-163.5 40.5-198.5-40.5-198.5-109.5-163.5-163.5-109.5-198.5-40.5-198.5 40.5-163.5 109.5-109.5 163.5-40.5 198.5 40.5 198.5 109.5 163.5 163.5 109.5 198.5 40.5z"/></svg>' )
);
}

/**
* Render page
*/
public function render_page() {
echo '<div id="mr_feature_flags_settings_screen"></div>';
}
}
6 changes: 3 additions & 3 deletions includes/FeatureFlags.php → includes/Utils.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* This is the init file for the plugin
* Utility class to expose flag methods.
*
* @package mr-feature-flags
* @since 1.0.0
Expand All @@ -11,12 +11,12 @@
namespace MR\FeatureFlags;

/**
* Class FeatureFlags
* Class Utils
*
* @package mr-feature-flags
* @since 1.0.0
*/
class FeatureFlags {
class Utils {

/**
* Name in options table.
Expand Down
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="unit">
<directory suffix=".php">./tests/Unit/</directory>
</testsuite>

</testsuites>
</phpunit>
13 changes: 5 additions & 8 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
declare( strict_types = 1 );

namespace MR\FeatureFlags;
use MR\FeatureFlags\Api\FlagOptions;
use MR\FeatureFlags\Api\Flags;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
Expand Down Expand Up @@ -141,31 +141,28 @@ function(string $page): void {
$mr_feature_flags_admin_settings = new Settings();
$mr_feature_flags_admin_settings->register_feature_settings();

$mr_feature_flags_register_api = new FlagOptions();
$mr_feature_flags_register_api = new Flags();
$mr_feature_flags_register_api->register_flags_endpoints();



add_filter( 'plugin_action_links_mr-feature-flags/plugin.php', function ( $links )
{
// Build and escape the URL.

$url = esc_url(
add_query_arg(
'page',
'mr-feature-flags',
get_admin_url() . 'admin.php'
)
);
// Create the link.

$settings_link = "<a href='$url'>" . __( 'Settings', 'mr-feature-flags' ) . '</a>';
// Adds the link to the end of the array.

array_push(
$links,
$settings_link
);
return $links;
}
);


// update_option( 'mr_feature_flags', [ ["id" => 1, "name" => "login", "enabled" => false],["id" => 2, "name" => "Reg", "enabled" => false]] );
99 changes: 99 additions & 0 deletions tests/Unit/FlagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace MR\FeatureFlags\Api;
use \PHPUnit\Framework\TestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use \Brain\Monkey;
use \Brain\Monkey\Functions;


class FlagsTest extends \PHPUnit\Framework\TestCase
{

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

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

public function test_get_all_flags_method_should_return_all_flags_from_options_table() {
$mock_option_value = [['id'=>1, 'name'=>'Test','enabled'=>true]];

\Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value);
\Brain\Monkey\Functions\when('rest_ensure_response')->returnArg();

$flags = new Flags();
$result = $flags->get_all_flags();
$this->assertEquals($result, $mock_option_value);
}

public function test_get_all_flags_method_should_return_empty_array_if_value_is_not_set() {
$mock_option_value = '';

\Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value);
\Brain\Monkey\Functions\when('rest_ensure_response')->returnArg();

$flags = new Flags();
$result = $flags->get_all_flags();
$this->assertEquals($result, []);
}

public function test_get_all_flags_method_should_return_multiple_flags_from_options_table() {
$mock_option_value = [['id'=>1, 'name'=>'Test','enabled'=>true],['id'=>2, 'name'=>'Test2','enabled'=>false]];

\Brain\Monkey\Functions\when('get_option')->justReturn($mock_option_value);
\Brain\Monkey\Functions\when('rest_ensure_response')->returnArg();

$flags = new Flags();
$result = $flags->get_all_flags();
$this->assertEquals($result, $mock_option_value);
}

public function test_post_flags_methods_should_return_success_if_input_is_array() {

$request_mock = \Mockery::mock('WP_Request');
$request_mock->shouldReceive('get_json_params')->andReturn(['param1' => 'value1']);

\Brain\Monkey\Functions\when('update_option')->justReturn(true);
\Brain\Monkey\Functions\when('rest_ensure_response')->returnArg();

global $wp;
$wp = new \stdClass();
$wp->request = $request_mock;

$flags = new Flags();
$result = $flags->post_flags($request_mock);

$this->assertEquals(['status'=>200, 'success' => true], $result);

unset($GLOBALS['wp']);
}

public function test_post_flags_methods_should_throw_error_if_input_is_not_an_array() {

$request_mock = \Mockery::mock('WP_Request');
$request_mock->shouldReceive('get_json_params')->andReturn('test');

global $wp;
$wp = new \stdClass();
$wp->request = $request_mock;

$error_mock = \Mockery::mock('WP_Error');

\Brain\Monkey\Functions\expect('post_flags')->andReturn($error_mock);


$flags = new Flags();
$result = $flags->post_flags($request_mock);

$this->assertInstanceOf('WP_Error', $result);

}


}
41 changes: 41 additions & 0 deletions tests/Unit/HelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace MR\FeatureFlags;

use \PHPUnit\Framework\TestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Brain\Monkey;



class HelperTest extends \PHPUnit\Framework\TestCase
{
// Adds Mockery expectations to the PHPUnit assertions count.
use MockeryPHPUnitIntegration;

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

public function test_search_flag_method_should_return_true_if_flag_name_present_and_enabled() {
$result = Helper::search_flag([['id'=>1, 'name'=>'Test','enabled'=>true]],'name','Test');
$this->assertTrue($result);
}

public function test_search_flag_method_should_return_false_if_flags_are_empty() {
$result = Helper::search_flag([],'name','Test');
$this->assertFalse($result);
}

public function test_search_flag_method_should_return_false_if_flag_name_present_but_disabled() {
$result = Helper::search_flag([],'name','Test');
$this->assertFalse($result);
}

public function test_search_flag_method_should_return_false_if_flag_not_present() {
$result = Helper::search_flag([['id'=>1, 'name'=>'Test','enabled'=>true]],'name','Test1');
$this->assertFalse($result);
}
}
Loading