Skip to content

Commit 59dbb0f

Browse files
committed
Added test for SampleRateRule.
1 parent 070ce14 commit 59dbb0f

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Tests\Logging\Rules;
6+
7+
8+
use lucatume\WPBrowser\TestCase\WPTestCase;
9+
use WPGraphQL\Logging\Logger\Rules\SamplingRateRule;
10+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
11+
12+
/**
13+
* Test cases for the SamplingRateRule
14+
*
15+
* @package WPGraphQL\Logging
16+
*
17+
* @since 0.0.1
18+
*/
19+
class SamplingRateRuleTest extends WPTestCase {
20+
21+
private SamplingRateRule $rule;
22+
23+
public function setUp(): void {
24+
parent::setUp();
25+
$this->rule = new SamplingRateRule();
26+
}
27+
28+
public function test_get_name_returns_correct_name(): void {
29+
$this->assertEquals('enabled_rule', $this->rule->get_name());
30+
}
31+
32+
public function test_passes_with_100_percent_sampling_rate(): void {
33+
$config = [
34+
BasicConfigurationTab::DATA_SAMPLING => 100,
35+
];
36+
37+
$result = $this->rule->passes($config);
38+
$this->assertTrue($result);
39+
}
40+
41+
public function test_passes_with_0_percent_sampling_rate(): void {
42+
$config = [
43+
BasicConfigurationTab::DATA_SAMPLING => 0,
44+
];
45+
46+
$result = $this->rule->passes($config);
47+
$this->assertFalse($result);
48+
}
49+
50+
public function test_passes_with_default_sampling_rate_when_not_set(): void {
51+
$config = [];
52+
53+
// Since default is 100, it should always pass
54+
$result = $this->rule->passes($config);
55+
$this->assertTrue($result);
56+
}
57+
58+
public function test_passes_with_string_sampling_rate(): void {
59+
$config = [
60+
BasicConfigurationTab::DATA_SAMPLING => '50',
61+
];
62+
63+
$result = $this->rule->passes($config);
64+
$this->assertIsBool($result);
65+
}
66+
67+
public function test_passes_with_null_query_string(): void {
68+
$config = [
69+
BasicConfigurationTab::DATA_SAMPLING => 100,
70+
];
71+
72+
$result = $this->rule->passes($config, null);
73+
$this->assertTrue($result);
74+
}
75+
76+
public function test_passes_with_query_string(): void {
77+
$config = [
78+
BasicConfigurationTab::DATA_SAMPLING => 100,
79+
];
80+
81+
$result = $this->rule->passes($config, 'query { posts { id } }');
82+
$this->assertTrue($result);
83+
}
84+
}

0 commit comments

Comments
 (0)