Skip to content

Commit b042e22

Browse files
authored
Merge pull request #64 from codebtech/chore/unit-tests
Chore/unit tests
2 parents ee9faa4 + c397c0d commit b042e22

File tree

8 files changed

+131
-9
lines changed

8 files changed

+131
-9
lines changed

.github/workflows/e2e.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: E2E Tests
2-
on: [push]
2+
on:
3+
push:
4+
branches-ignore:
5+
- 'main-built'
36

47
jobs:
58
test:

.github/workflows/js.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: JS lint & test
2-
on: [push]
2+
on:
3+
push:
4+
branches-ignore:
5+
- 'main-built'
36

47
jobs:
58
build:

.github/workflows/php.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: PHP lint & test
2-
on: [push]
2+
on:
3+
push:
4+
branches-ignore:
5+
- 'main-built'
36

47
jobs:
58
lint-test:

phpunit-integration.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/integration/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage processUncoveredFiles="true">
3+
<coverage processUncoveredFiles="true">
44
<include>
5-
<directory suffix=".php">./includes/Api</directory>
5+
<directory suffix=".php">./includes</directory>
66
</include>
77
</coverage>
88
<testsuites>

tests/integration/FlagTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
use MR\FeatureFlags\Flag;
3+
use Yoast\WPTestUtils\WPIntegration\TestCase;
4+
5+
class FlagTest extends TestCase{
6+
7+
public function test_is_enabled_returns_true_if_flags_exists_and_enabled(): void {
8+
// Set up test data
9+
$optionValue = [['id' => 1, 'name' => 'test', 'enabled' => true ]];
10+
update_option(Flag::$option_name, $optionValue);
11+
12+
$result = Flag::is_enabled('test');
13+
14+
$this->assertTrue($result);
15+
}
16+
17+
public function test_is_enabled_returns_false_if_flags_exists_and_disabled(): void {
18+
// Set up test data
19+
$optionValue = [['id' => 1, 'name' => 'test', 'enabled' => false ]];
20+
update_option(Flag::$option_name, $optionValue);
21+
22+
$result = Flag::is_enabled('test');
23+
24+
$this->assertFalse($result);
25+
}
26+
27+
public function test_is_enabled_returns_false_if_flags_not_exists(): void {
28+
// Set up test data
29+
$optionValue = [['id' => 1, 'name' => 'test2', 'enabled' => true ]];
30+
update_option(Flag::$option_name, $optionValue);
31+
32+
$result = Flag::is_enabled('test');
33+
34+
$this->assertFalse($result);
35+
}
36+
37+
public function test_is_enabled_returns_false_if_option_is_empty(): void {
38+
// Set up test data
39+
$optionValue = [];
40+
update_option(Flag::$option_name, $optionValue);
41+
42+
$result = Flag::is_enabled('test');
43+
44+
$this->assertFalse($result);
45+
}
46+
47+
public function test_is_enabled_returns_false_if_option_not_exists(): void {
48+
49+
$result = Flag::is_enabled('test');
50+
51+
$this->assertFalse($result);
52+
}
53+
}

tests/integration/FlagsApiTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function test_create_item_with_default_max_allowed_filter() {
184184
$response_message = $response->get_data()['message'];
185185

186186
$this->assertErrorResponse( 'flag_limit_exceeded', $response, 400 );
187-
$this->assertEquals('Maximum allowed flags are 20', $response_message);
187+
$this->assertEquals('Cannot add more than 20 flags', $response_message);
188188

189189
}
190190

@@ -206,8 +206,7 @@ public function test_create_item_with_custom_max_allowed_filter() {
206206
$response_message = $response->get_data()['message'];
207207

208208
$this->assertErrorResponse( 'flag_limit_exceeded', $response, 400 );
209-
$this->assertEquals('Maximum allowed flags are 3', $response_message);
210-
209+
$this->assertEquals('Cannot add more than 3 flags', $response_message);
211210
}
212211

213212
public function test_create_item_without_input() {

tests/unit/FlagTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,4 @@ public function test_is_enabled_method_should_return_false_if_flag_name_not_exis
4141
$this->assertFalse($result);
4242
}
4343

44-
4544
}

tests/unit/SettingsTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
use Yoast\WPTestUtils\BrainMonkey\TestCase;
3+
use MR\FeatureFlags\Settings;
4+
5+
class SettingsTest extends TestCase {
6+
7+
public function setUp(): void {
8+
parent::setUp();
9+
10+
\Brain\Monkey\setUp();
11+
12+
// Mock the __ function for translation
13+
\Brain\Monkey\Functions\expect('__')
14+
->andReturnUsing(function ($string) {
15+
return $string; // Simulate translation for testing purposes
16+
});
17+
}
18+
19+
public function tearDown(): void {
20+
\Brain\Monkey\tearDown();
21+
parent::tearDown();
22+
}
23+
24+
public function testRegisterFeatureSettings() {
25+
$settings = new Settings();
26+
27+
\Brain\Monkey\Functions\expect('add_action')
28+
->once()
29+
->with('admin_menu', [$settings, 'register_settings']);
30+
31+
$settings->register_feature_settings();
32+
}
33+
34+
public function testRegisterSettings() {
35+
$settings = new Settings();
36+
37+
\Brain\Monkey\Functions\expect('add_menu_page')
38+
->once()
39+
->with(
40+
'Feature Flags',
41+
'Feature Flags',
42+
'manage_options',
43+
'mr-feature-flags',
44+
[$settings, 'render_page'],
45+
'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>' )
46+
);
47+
48+
$settings->register_settings();
49+
}
50+
51+
public function testRenderPage() {
52+
$settings = new Settings();
53+
54+
ob_start();
55+
56+
$settings->render_page();
57+
58+
$output = ob_get_clean();
59+
60+
$this->assertStringContainsString('<div id="mr_feature_flags_settings_screen"></div>', $output);
61+
}
62+
}

0 commit comments

Comments
 (0)