From 6f76df1ba54617932d7d457dd9afd76c860bb9bb Mon Sep 17 00:00:00 2001 From: peptiderepo Date: Thu, 11 Jun 2026 12:45:43 +0700 Subject: [PATCH] fix(ci): yoda condition + un-shadow capability-cache test expectations - class-image-composer.php: swap the fingerprint comparison operands (WordPress.PHP.YodaConditions flagged the parenthesized null-coalesce expression on the right-hand side). - ImageComposerTest: the two capability-cache tests asserted update_option calls via Functions\expect(), which the setUp Functions\when() stub shadows (Mockery dispatches the call to the first matching expectation), so the with()-matcher saw 0 calls once the CI runner's real imagick made the probe path live. Capture calls with a when() alias instead (the suite's house pattern) and assert on the captured writes - including autoload=false and the rewritten fingerprint. The matching-fingerprint test now genuinely asserts zero writes instead of a trivially-satisfied never(). No composer behavior change beyond the operand swap. Agent-Session: engineer-prautoblogger-image-composer-20260611 --- includes/core/class-image-composer.php | 2 +- tests/unit/Core/ImageComposerTest.php | 42 +++++++++++++++----------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/includes/core/class-image-composer.php b/includes/core/class-image-composer.php index 640f814..2f59c88 100644 --- a/includes/core/class-image-composer.php +++ b/includes/core/class-image-composer.php @@ -176,7 +176,7 @@ class PRAutoBlogger_Image_Composer implements PRAutoBlogger_Image_Composer_Inter ); $cached = get_option( self::OPTION_CAPABILITY, array() ); - if ( is_array( $cached ) && $fingerprint === ( $cached['fingerprint'] ?? '' ) && isset( $cached['capability'] ) ) { + if ( is_array( $cached ) && ( $cached['fingerprint'] ?? '' ) === $fingerprint && isset( $cached['capability'] ) ) { $capability = (string) $cached['capability']; } else { $capability = $this->probe_capability(); diff --git a/tests/unit/Core/ImageComposerTest.php b/tests/unit/Core/ImageComposerTest.php index 0842126..5c6b204 100644 --- a/tests/unit/Core/ImageComposerTest.php +++ b/tests/unit/Core/ImageComposerTest.php @@ -237,7 +237,8 @@ class ImageComposerTest extends BaseTestCase { /** * A cached probe with a matching environment fingerprint is reused — - * no re-probe, no option write. + * no re-probe, no option write. (update_option calls are captured via a + * when() alias: an expect() here would be shadowed by the setUp stub.) */ public function test_matching_capability_cache_skips_probe_write(): void { $fingerprint = md5( @@ -251,7 +252,13 @@ class ImageComposerTest extends BaseTestCase { ], ] ); - Functions\expect( 'update_option' )->never(); + $writes = []; + Functions\when( 'update_option' )->alias( + function ( $key, $value = null, $autoload = null ) use ( &$writes ) { + $writes[] = $key; + return true; + } + ); $composer = new \PRAutoBlogger_Image_Composer( $this->createMock( \PRAutoBlogger_Image_Composer_Imagick::class ) @@ -259,11 +266,13 @@ class ImageComposerTest extends BaseTestCase { $variants = $composer->compose( $this->image_data, $this->context() ); $this->assertSame( 'featured', $variants[0]['role'] ); + $this->assertSame( [], $writes, 'A matching cache fingerprint must not trigger a probe rewrite.' ); } /** * A stale fingerprint (host changed) triggers a fresh probe and a cache - * rewrite — the auto-invalidation the spec requires. + * rewrite — the auto-invalidation the spec requires. (Captured via a + * when() alias: an expect() here would be shadowed by the setUp stub.) */ public function test_stale_capability_cache_reprobes_and_rewrites(): void { $this->stub_get_option( @@ -274,20 +283,13 @@ class ImageComposerTest extends BaseTestCase { ], ] ); - Functions\expect( 'update_option' ) - ->once() - ->with( - \PRAutoBlogger_Image_Composer::OPTION_CAPABILITY, - \Mockery::on( - static function ( $value ): bool { - return is_array( $value ) - && isset( $value['fingerprint'], $value['capability'] ) - && 'old-host-fingerprint' !== $value['fingerprint']; - } - ), - false - ) - ->andReturn( true ); + $writes = []; + Functions\when( 'update_option' )->alias( + function ( $key, $value = null, $autoload = null ) use ( &$writes ) { + $writes[] = [ $key, $value, $autoload ]; + return true; + } + ); $composer = new \PRAutoBlogger_Image_Composer( $this->createMock( \PRAutoBlogger_Image_Composer_Imagick::class ) @@ -295,6 +297,12 @@ class ImageComposerTest extends BaseTestCase { $variants = $composer->compose( $this->image_data, $this->context() ); $this->assertSame( 'featured', $variants[0]['role'] ); + $this->assertCount( 1, $writes, 'A stale fingerprint must rewrite the capability cache.' ); + $this->assertSame( \PRAutoBlogger_Image_Composer::OPTION_CAPABILITY, $writes[0][0] ); + $this->assertIsArray( $writes[0][1] ); + $this->assertArrayHasKey( 'capability', $writes[0][1] ); + $this->assertNotSame( 'old-host-fingerprint', $writes[0][1]['fingerprint'] ?? '' ); + $this->assertFalse( $writes[0][2], 'Capability cache must not autoload.' ); } /**