FeedCacheWpdbDouble.php
62 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SmashBalloon\Reviews\Tests\Unit\Doubles; |
| 4 | |
| 5 | /** |
| 6 | * Minimal `wpdb` test double for FeedCacheUpdateServiceTest. |
| 7 | * |
| 8 | * Captures the SQL handed to prepare()/get_results() so the dedupe + ordering |
| 9 | * contract on `FeedCacheUpdateService::feed_caches_query()` can be asserted |
| 10 | * without a live database. Exists in its own file (PSR1 — one class per file) |
| 11 | * and uses untyped properties (PHP 7.1+ baseline per phpcs.xml). |
| 12 | */ |
| 13 | class FeedCacheWpdbDouble |
| 14 | { |
| 15 | /** @var string */ |
| 16 | public $prefix = 'wp_'; |
| 17 | |
| 18 | /** @var string|null */ |
| 19 | public $last_prepared_sql = null; |
| 20 | |
| 21 | /** @var array<int, mixed> */ |
| 22 | public $last_prepared_args = array(); |
| 23 | |
| 24 | /** @var string|null */ |
| 25 | public $last_get_results_sql = null; |
| 26 | |
| 27 | /** @var array<int, array<string, mixed>> */ |
| 28 | public $next_results = array(); |
| 29 | |
| 30 | /** |
| 31 | * @param string $sql |
| 32 | * @param mixed ...$args |
| 33 | * @return string |
| 34 | */ |
| 35 | public function esc_like($text) |
| 36 | { |
| 37 | return addcslashes((string) $text, '_%\\'); |
| 38 | } |
| 39 | |
| 40 | public function prepare($sql, ...$args) |
| 41 | { |
| 42 | $this->last_prepared_sql = $sql; |
| 43 | $this->last_prepared_args = $args; |
| 44 | // We don't need a real interpolation — the production code reads the |
| 45 | // returned string and immediately hands it to get_results, which we |
| 46 | // also intercept. Returning the raw template is enough for the |
| 47 | // behavioral assertions in this suite. |
| 48 | return $sql; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param string $sql |
| 53 | * @param string $output_type |
| 54 | * @return array<int, array<string, mixed>> |
| 55 | */ |
| 56 | public function get_results($sql, $output_type = 'OBJECT') |
| 57 | { |
| 58 | $this->last_get_results_sql = $sql; |
| 59 | return $this->next_results; |
| 60 | } |
| 61 | } |
| 62 |