| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Tests\Integration; |
| 4 |
|
| 5 |
use Extendify\Insights; |
| 6 |
use WP_UnitTestCase; |
| 7 |
|
| 8 |
class InsightsTest extends WP_UnitTestCase |
| 9 |
{ |
| 10 |
public function test_setup_rolls_a_variant_for_an_active_test() |
| 11 |
{ |
| 12 |
delete_option(Insights::ACTIVE_TESTS_OPTION); |
| 13 |
|
| 14 |
Insights::setup(['AutoLaunch.ShowTitle']); |
| 15 |
|
| 16 |
$tests = get_option(Insights::ACTIVE_TESTS_OPTION); |
| 17 |
$this->assertArrayHasKey('AutoLaunch.ShowTitle', $tests); |
| 18 |
$this->assertContains($tests['AutoLaunch.ShowTitle']['variant'], ['A', 'B']); |
| 19 |
} |
| 20 |
|
| 21 |
public function test_setup_rolls_each_active_test_independently_honoring_its_own_percentage() |
| 22 |
{ |
| 23 |
// Alternate 0%/100% across the keys; each key's own percentage must |
| 24 |
// drive its own roll, regardless of the others. |
| 25 |
$active = []; |
| 26 |
$expected = []; |
| 27 |
foreach (Insights::AVAILABLE_TESTS as $i => $key) { |
| 28 |
$percentage = $i % 2 === 0 ? 0 : 100; |
| 29 |
$active[] = "{$key}:{$percentage}"; |
| 30 |
$expected[$key] = ['variant' => $percentage === 0 ? 'A' : 'B', 'percentage' => (float) $percentage]; |
| 31 |
} |
| 32 |
|
| 33 |
delete_option(Insights::ACTIVE_TESTS_OPTION); |
| 34 |
Insights::setup($active); |
| 35 |
|
| 36 |
$tests = get_option(Insights::ACTIVE_TESTS_OPTION); |
| 37 |
foreach ($expected as $key => $expect) { |
| 38 |
$this->assertSame($expect['variant'], $tests[$key]['variant']); |
| 39 |
$this->assertSame($expect['percentage'], $tests[$key]['percentage']); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public function test_setup_drops_inactive_tests() |
| 44 |
{ |
| 45 |
update_option(Insights::ACTIVE_TESTS_OPTION, [ |
| 46 |
'AutoLaunch.ShowTitle' => ['variant' => 'B'], |
| 47 |
]); |
| 48 |
|
| 49 |
Insights::setup([]); |
| 50 |
|
| 51 |
$tests = get_option(Insights::ACTIVE_TESTS_OPTION); |
| 52 |
$this->assertArrayNotHasKey('AutoLaunch.ShowTitle', $tests); |
| 53 |
} |
| 54 |
|
| 55 |
public function test_setup_keeps_the_variant_stable_across_calls() |
| 56 |
{ |
| 57 |
update_option(Insights::ACTIVE_TESTS_OPTION, [ |
| 58 |
'AutoLaunch.ShowTitle' => ['variant' => 'B'], |
| 59 |
]); |
| 60 |
|
| 61 |
Insights::setup(['AutoLaunch.ShowTitle']); |
| 62 |
Insights::setup(['AutoLaunch.ShowTitle']); |
| 63 |
|
| 64 |
$assignment = get_option(Insights::ACTIVE_TESTS_OPTION)['AutoLaunch.ShowTitle']; |
| 65 |
$this->assertSame('B', $assignment['variant']); |
| 66 |
} |
| 67 |
|
| 68 |
public function test_setup_with_zero_percentage_always_rolls_a() |
| 69 |
{ |
| 70 |
for ($i = 0; $i < 25; $i++) { |
| 71 |
delete_option(Insights::ACTIVE_TESTS_OPTION); |
| 72 |
Insights::setup(['AutoLaunch.ShowTitle:0']); |
| 73 |
$variant = get_option(Insights::ACTIVE_TESTS_OPTION)['AutoLaunch.ShowTitle']['variant']; |
| 74 |
$this->assertSame('A', $variant); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public function test_setup_with_full_percentage_always_rolls_b() |
| 79 |
{ |
| 80 |
for ($i = 0; $i < 25; $i++) { |
| 81 |
delete_option(Insights::ACTIVE_TESTS_OPTION); |
| 82 |
Insights::setup(['AutoLaunch.ShowTitle:100']); |
| 83 |
$variant = get_option(Insights::ACTIVE_TESTS_OPTION)['AutoLaunch.ShowTitle']['variant']; |
| 84 |
$this->assertSame('B', $variant); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
public function test_setup_stamps_assigned_at_and_keeps_it_stable() |
| 89 |
{ |
| 90 |
delete_option(Insights::ACTIVE_TESTS_OPTION); |
| 91 |
|
| 92 |
Insights::setup(['AutoLaunch.ShowTitle']); |
| 93 |
$assignedAt = get_option(Insights::ACTIVE_TESTS_OPTION)['AutoLaunch.ShowTitle']['assignedAt']; |
| 94 |
$this->assertMatchesRegularExpression('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+00:00$/', $assignedAt); |
| 95 |
|
| 96 |
Insights::setup(['AutoLaunch.ShowTitle']); |
| 97 |
$this->assertSame( |
| 98 |
$assignedAt, |
| 99 |
get_option(Insights::ACTIVE_TESTS_OPTION)['AutoLaunch.ShowTitle']['assignedAt'] |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
public function test_setup_stores_the_active_percentage_on_the_assignment() |
| 104 |
{ |
| 105 |
delete_option(Insights::ACTIVE_TESTS_OPTION); |
| 106 |
|
| 107 |
Insights::setup(['AutoLaunch.ShowTitle:20']); |
| 108 |
|
| 109 |
$assignment = get_option(Insights::ACTIVE_TESTS_OPTION)['AutoLaunch.ShowTitle']; |
| 110 |
$this->assertSame(20.0, $assignment['percentage']); |
| 111 |
} |
| 112 |
|
| 113 |
public function test_setup_stores_a_fractional_percentage() |
| 114 |
{ |
| 115 |
delete_option(Insights::ACTIVE_TESTS_OPTION); |
| 116 |
|
| 117 |
Insights::setup(['AutoLaunch.ShowTitle:22.5']); |
| 118 |
|
| 119 |
$assignment = get_option(Insights::ACTIVE_TESTS_OPTION)['AutoLaunch.ShowTitle']; |
| 120 |
$this->assertSame(22.5, $assignment['percentage']); |
| 121 |
} |
| 122 |
|
| 123 |
public function test_setup_leaves_unknown_keys_untouched() |
| 124 |
{ |
| 125 |
update_option(Insights::ACTIVE_TESTS_OPTION, ['SomeOther.Test' => 'A']); |
| 126 |
|
| 127 |
Insights::setup(['AutoLaunch.ShowTitle']); |
| 128 |
|
| 129 |
$tests = get_option(Insights::ACTIVE_TESTS_OPTION); |
| 130 |
$this->assertSame('A', $tests['SomeOther.Test']); |
| 131 |
} |
| 132 |
} |
| 133 |
|