previous = $GLOBALS['wpdb'] ?? null; $GLOBALS['wpdb'] = new class() { public string $prefix = 'wp_'; public function prepare( string $q, ...$args ): string { return $q; } public function get_results( string $q, $output = null ): array { return array( array( 'id' => '1', 'goal_id' => '1', 'type' => 'add_time', 'config' => '{"seconds_per_unit":60,"unit_minor":100,"max_timer_seconds":5400}', 'enabled' => '1', 'sort' => '0' ), array( 'id' => '2', 'goal_id' => '1', 'type' => 'counter', 'config' => 'not json', 'enabled' => '0', 'sort' => '1' ), array( 'id' => '3', 'goal_id' => '1', 'type' => 'bonus', 'config' => '{"min_minor":5000}', 'enabled' => '1', 'sort' => '2' ), ); } }; } protected function tearDown(): void { $GLOBALS['wpdb'] = $this->previous; } public function test_every_rule_config_is_a_decoded_array(): void { $rules = Store::rules( 1 ); $this->assertCount( 3, $rules ); foreach ( $rules as $r ) { $this->assertIsArray( $r['config'], 'rule ' . $r['id'] . ' config was not decoded' ); } $this->assertSame( 5400, $rules[0]['config']['max_timer_seconds'] ); $this->assertSame( array(), $rules[1]['config'], 'invalid JSON becomes an empty config, not a crash' ); $this->assertSame( 5000, $rules[2]['config']['min_minor'] ); } public function test_rule_lookup_returns_decoded_config(): void { $r = Store::rule( 3, 1 ); $this->assertIsArray( $r['config'] ); $this->assertNull( Store::rule( 99, 1 ) ); } }