UsageReporterWpdbDouble.php
55 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SmashBalloon\Reviews\Tests\Unit\Doubles; |
| 4 | |
| 5 | /** |
| 6 | * Minimal `wpdb` test double for the usage-tracking ReviewsReporter. |
| 7 | * |
| 8 | * get_var() answers the `SHOW TABLES LIKE %s` existence probe (every table |
| 9 | * exists) and get_results() returns canned feed rows, so the reporter's |
| 10 | * feed-data SQL and row-shape handling can be asserted without a live |
| 11 | * database. One class per file per PSR1. |
| 12 | */ |
| 13 | class UsageReporterWpdbDouble |
| 14 | { |
| 15 | /** @var string */ |
| 16 | public $prefix = 'wp_'; |
| 17 | |
| 18 | /** @var array<int, array<string, mixed>> */ |
| 19 | public $next_results = array(); |
| 20 | |
| 21 | /** @var string|null */ |
| 22 | public $last_get_results_sql = null; |
| 23 | |
| 24 | /** @var mixed Last value handed to prepare(), i.e. the probed table name. */ |
| 25 | public $last_prepared_arg = null; |
| 26 | |
| 27 | /** @var int Value returned for COUNT(*) queries. */ |
| 28 | public $next_count = 0; |
| 29 | |
| 30 | public function prepare($sql, ...$args) |
| 31 | { |
| 32 | $this->last_prepared_arg = $args[0] ?? null; |
| 33 | return $sql; |
| 34 | } |
| 35 | |
| 36 | public function get_var($sql) |
| 37 | { |
| 38 | // A COUNT(*) is a real scalar query, not the existence probe. Returning the |
| 39 | // probed table name for it would cast to (int) 0 and quietly satisfy any |
| 40 | // assertion on connected_count / feed_caches_count / reviews_count. |
| 41 | if (false !== stripos((string) $sql, 'COUNT(')) { |
| 42 | return $this->next_count; |
| 43 | } |
| 44 | |
| 45 | // `SHOW TABLES LIKE` probe — report the probed table as existing. |
| 46 | return $this->last_prepared_arg; |
| 47 | } |
| 48 | |
| 49 | public function get_results($sql, $output_type = 'OBJECT') |
| 50 | { |
| 51 | $this->last_get_results_sql = $sql; |
| 52 | return $this->next_results; |
| 53 | } |
| 54 | } |
| 55 |