From de1a7c16f923ac1863d298038e1f6ab8aaf1a4db Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Tue, 27 Feb 2024 20:56:26 +0000 Subject: [PATCH 1/4] adds settings unit test --- tests/unit/FlagTest.php | 1 - tests/unit/SettingsTest.php | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/unit/SettingsTest.php diff --git a/tests/unit/FlagTest.php b/tests/unit/FlagTest.php index eb36615..67736f3 100644 --- a/tests/unit/FlagTest.php +++ b/tests/unit/FlagTest.php @@ -41,5 +41,4 @@ public function test_is_enabled_method_should_return_false_if_flag_name_not_exis $this->assertFalse($result); } - } diff --git a/tests/unit/SettingsTest.php b/tests/unit/SettingsTest.php new file mode 100644 index 0000000..c8ad20e --- /dev/null +++ b/tests/unit/SettingsTest.php @@ -0,0 +1,62 @@ +andReturnUsing(function ($string) { + return $string; // Simulate translation for testing purposes + }); + } + + public function tearDown(): void { + \Brain\Monkey\tearDown(); + parent::tearDown(); + } + + public function testRegisterFeatureSettings() { + $settings = new Settings(); + + \Brain\Monkey\Functions\expect('add_action') + ->once() + ->with('admin_menu', [$settings, 'register_settings']); + + $settings->register_feature_settings(); + } + + public function testRegisterSettings() { + $settings = new Settings(); + + \Brain\Monkey\Functions\expect('add_menu_page') + ->once() + ->with( + 'Feature Flags', + 'Feature Flags', + 'manage_options', + 'mr-feature-flags', + [$settings, 'render_page'], + 'data:image/svg+xml;base64,' . base64_encode( '' ) + ); + + $settings->register_settings(); + } + + public function testRenderPage() { + $settings = new Settings(); + + ob_start(); + + $settings->render_page(); + + $output = ob_get_clean(); + + $this->assertStringContainsString('
', $output); + } +} From 0f6e6bf35126d516e656e9268b9809e715b7c773 Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Wed, 28 Feb 2024 11:42:24 +0000 Subject: [PATCH 2/4] update workflows --- .github/workflows/e2e.yml | 6 +++++- .github/workflows/js.yml | 6 +++++- .github/workflows/php.yml | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 0170670..06947ba 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -1,5 +1,9 @@ name: E2E Tests -on: [push] +on: + push: + branches: + - '*' + - '!main-built' jobs: test: diff --git a/.github/workflows/js.yml b/.github/workflows/js.yml index b5db942..7e5cdcd 100644 --- a/.github/workflows/js.yml +++ b/.github/workflows/js.yml @@ -1,5 +1,9 @@ name: JS lint & test -on: [push] +on: + push: + branches: + - '*' + - '!main-built' jobs: build: diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 36d3190..221efb5 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -1,5 +1,9 @@ name: PHP lint & test -on: [push] +on: + push: + branches: + - '*' + - '!main-built' jobs: lint-test: From 0ce6c455956b7a8c509940518beed44dd000783e Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Wed, 28 Feb 2024 12:26:23 +0000 Subject: [PATCH 3/4] adds more integration tests --- phpunit-integration.xml | 4 +-- tests/integration/FlagTest.php | 53 ++++++++++++++++++++++++++++++ tests/integration/FlagsApiTest.php | 5 ++- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 tests/integration/FlagTest.php diff --git a/phpunit-integration.xml b/phpunit-integration.xml index ba53656..bf755a2 100644 --- a/phpunit-integration.xml +++ b/phpunit-integration.xml @@ -1,8 +1,8 @@ - + - ./includes/Api + ./includes diff --git a/tests/integration/FlagTest.php b/tests/integration/FlagTest.php new file mode 100644 index 0000000..37c7b2a --- /dev/null +++ b/tests/integration/FlagTest.php @@ -0,0 +1,53 @@ + 1, 'name' => 'test', 'enabled' => true ]]; + update_option(Flag::$option_name, $optionValue); + + $result = Flag::is_enabled('test'); + + $this->assertTrue($result); + } + + public function test_is_enabled_returns_false_if_flags_exists_and_disabled(): void { + // Set up test data + $optionValue = [['id' => 1, 'name' => 'test', 'enabled' => false ]]; + update_option(Flag::$option_name, $optionValue); + + $result = Flag::is_enabled('test'); + + $this->assertFalse($result); + } + + public function test_is_enabled_returns_false_if_flags_not_exists(): void { + // Set up test data + $optionValue = [['id' => 1, 'name' => 'test2', 'enabled' => true ]]; + update_option(Flag::$option_name, $optionValue); + + $result = Flag::is_enabled('test'); + + $this->assertFalse($result); + } + + public function test_is_enabled_returns_false_if_option_is_empty(): void { + // Set up test data + $optionValue = []; + update_option(Flag::$option_name, $optionValue); + + $result = Flag::is_enabled('test'); + + $this->assertFalse($result); + } + + public function test_is_enabled_returns_false_if_option_not_exists(): void { + + $result = Flag::is_enabled('test'); + + $this->assertFalse($result); + } +} diff --git a/tests/integration/FlagsApiTest.php b/tests/integration/FlagsApiTest.php index 4180429..ed799ec 100644 --- a/tests/integration/FlagsApiTest.php +++ b/tests/integration/FlagsApiTest.php @@ -184,7 +184,7 @@ public function test_create_item_with_default_max_allowed_filter() { $response_message = $response->get_data()['message']; $this->assertErrorResponse( 'flag_limit_exceeded', $response, 400 ); - $this->assertEquals('Maximum allowed flags are 20', $response_message); + $this->assertEquals('Cannot add more than 20 flags', $response_message); } @@ -206,8 +206,7 @@ public function test_create_item_with_custom_max_allowed_filter() { $response_message = $response->get_data()['message']; $this->assertErrorResponse( 'flag_limit_exceeded', $response, 400 ); - $this->assertEquals('Maximum allowed flags are 3', $response_message); - + $this->assertEquals('Cannot add more than 3 flags', $response_message); } public function test_create_item_without_input() { From c397c0dbbdd772af2cbefca84f6f3a2e79370c5f Mon Sep 17 00:00:00 2001 From: Mohan Raj Date: Wed, 28 Feb 2024 12:29:28 +0000 Subject: [PATCH 4/4] ignore main-built branch --- .github/workflows/e2e.yml | 5 ++--- .github/workflows/js.yml | 5 ++--- .github/workflows/php.yml | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 06947ba..9c239ee 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -1,9 +1,8 @@ name: E2E Tests on: push: - branches: - - '*' - - '!main-built' + branches-ignore: + - 'main-built' jobs: test: diff --git a/.github/workflows/js.yml b/.github/workflows/js.yml index 7e5cdcd..18e19ee 100644 --- a/.github/workflows/js.yml +++ b/.github/workflows/js.yml @@ -1,9 +1,8 @@ name: JS lint & test on: push: - branches: - - '*' - - '!main-built' + branches-ignore: + - 'main-built' jobs: build: diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 221efb5..ee81930 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -1,9 +1,8 @@ name: PHP lint & test on: push: - branches: - - '*' - - '!main-built' + branches-ignore: + - 'main-built' jobs: lint-test: