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
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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.' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user