diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 5604c4460..d83c8f0ad 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,6 +3,7 @@ parameters: paths: - src - entity-command.php + - tests excludePaths: # Polyfill files are copies of WordPress core code with GOTO control flow # that PHPStan cannot analyze correctly @@ -13,6 +14,9 @@ parameters: - vendor/wp-cli/wp-cli/php scanFiles: - vendor/php-stubs/wordpress-stubs/wordpress-stubs.php + - vendor/wp-cli/wp-cli-tests/tests/includes/TestCase.php + stubFiles: + - tests/phpstan/WP_Block_Processor.stub treatPhpDocTypesAsCertain: false ignoreErrors: - identifier: missingType.iterableValue @@ -38,6 +42,19 @@ parameters: message: '#^WP_Block_Processor::(allocate_and_return_parsed_attributes|extract_full_block_and_advance|get_block_type|get_delimiter_type|get_depth|get_span|next_block)\(\) is only available since WordPress version 6\.9\.0\.$#' path: src/Block_Processor_Helper.php + # The polyfill tests exercise the whole `WP_Block_Processor` API through the + # polyfill, independent of the WordPress version. + - + identifier: WPCompat.methodNotAvailable + message: '#^WP_Block_Processor::[a-z_]+\(\) is only available since WordPress version 6\.9\.0\.$#' + path: tests/Compat/WP_Block_ProcessorTest.php + + # WordPress marks `WP_HTML_Span` as private, but its polyfill still needs testing. + - + identifier: no.private.class + message: '#^Instantiation of private/internal class WP_HTML_Span\.$#' + path: tests/Compat/WP_HTML_SpanTest.php + # `wp font collection|face|family` aborts on WordPress < 6.5 via `before_invoke`. - identifier: WPCompat.methodNotAvailable diff --git a/tests/Block_Processor_HelperTest.php b/tests/Block_Processor_HelperTest.php index 10ef7ce7b..a70f4edce 100644 --- a/tests/Block_Processor_HelperTest.php +++ b/tests/Block_Processor_HelperTest.php @@ -7,7 +7,7 @@ namespace WP_CLI\Entity\Tests; -use PHPUnit\Framework\TestCase; +use WP_CLI\Tests\TestCase; use WP_CLI\Entity\Block_Processor_Helper; /** @@ -28,8 +28,8 @@ class Block_Processor_HelperTest extends TestCase { /** * Set up test fixtures. */ - protected function setUp(): void { - parent::setUp(); + protected function set_up() { + parent::set_up(); // Create sample content with multiple blocks. $this->sample_content = implode( diff --git a/tests/Compat/PolyfillsTest.php b/tests/Compat/PolyfillsTest.php index a4ed64271..1ab02ceea 100644 --- a/tests/Compat/PolyfillsTest.php +++ b/tests/Compat/PolyfillsTest.php @@ -7,7 +7,7 @@ namespace WP_CLI\Entity\Tests\Compat; -use PHPUnit\Framework\TestCase; +use WP_CLI\Tests\TestCase; /** * Test the PHP function polyfills. diff --git a/tests/Compat/WP_Block_ProcessorTest.php b/tests/Compat/WP_Block_ProcessorTest.php index c5d70731c..120606fe4 100644 --- a/tests/Compat/WP_Block_ProcessorTest.php +++ b/tests/Compat/WP_Block_ProcessorTest.php @@ -7,7 +7,7 @@ namespace WP_CLI\Entity\Tests\Compat; -use PHPUnit\Framework\TestCase; +use WP_CLI\Tests\TestCase; use WP_Block_Processor; use WP_HTML_Span; @@ -148,6 +148,7 @@ public function test_extract_full_block_handles_nested_blocks() { $processor->next_block(); $block = $processor->extract_full_block_and_advance(); + $this->assertNotNull( $block ); $this->assertSame( 'core/group', $block['blockName'] ); $this->assertCount( 1, $block['innerBlocks'] ); $this->assertSame( 'core/paragraph', $block['innerBlocks'][0]['blockName'] ); @@ -458,6 +459,7 @@ public function test_extract_full_block_structure() { $block = $processor->extract_full_block_and_advance(); + $this->assertNotNull( $block ); $this->assertArrayHasKey( 'blockName', $block ); $this->assertArrayHasKey( 'attrs', $block ); $this->assertArrayHasKey( 'innerBlocks', $block ); @@ -481,6 +483,7 @@ public function test_extract_full_block_with_nested() { $block = $processor->extract_full_block_and_advance(); + $this->assertNotNull( $block ); $this->assertSame( 'core/group', $block['blockName'] ); $this->assertCount( 1, $block['innerBlocks'] ); $this->assertSame( 'core/paragraph', $block['innerBlocks'][0]['blockName'] ); @@ -497,8 +500,9 @@ public function test_complex_nested_attributes() { $attrs = $processor->allocate_and_return_parsed_attributes(); + $this->assertIsArray( $attrs ); $this->assertSame( [ 1, 2, 3 ], $attrs['ids'] ); - $this->assertSame( true, $attrs['nested']['deep']['value'] ); + $this->assertTrue( $attrs['nested']['deep']['value'] ); } /** @@ -528,7 +532,9 @@ public function test_multiple_blocks_extraction() { $blocks = []; while ( $processor->next_block() ) { - $blocks[] = $processor->extract_full_block_and_advance(); + $block = $processor->extract_full_block_and_advance(); + $this->assertNotNull( $block ); + $blocks[] = $block; } $this->assertCount( 3, $blocks ); @@ -569,6 +575,7 @@ public function test_void_block_with_complex_attrs() { $attrs = $processor->allocate_and_return_parsed_attributes(); + $this->assertIsArray( $attrs ); $this->assertSame( 123, $attrs['id'] ); $this->assertSame( 'large', $attrs['sizeSlug'] ); $this->assertSame( 'media', $attrs['linkDestination'] ); @@ -586,6 +593,7 @@ public function test_unicode_content_handling() { $attrs = $processor->allocate_and_return_parsed_attributes(); + $this->assertIsArray( $attrs ); $this->assertSame( '日本語テスト', $attrs['text'] ); } @@ -603,6 +611,7 @@ public function test_span_offset_middle_block() { $span = $processor->get_span(); + $this->assertNotNull( $span ); $this->assertSame( strlen( $prefix ), $span->start ); } diff --git a/tests/Compat/WP_HTML_SpanTest.php b/tests/Compat/WP_HTML_SpanTest.php index 8bf8b7d29..c12f18c37 100644 --- a/tests/Compat/WP_HTML_SpanTest.php +++ b/tests/Compat/WP_HTML_SpanTest.php @@ -7,7 +7,7 @@ namespace WP_CLI\Entity\Tests\Compat; -use PHPUnit\Framework\TestCase; +use WP_CLI\Tests\TestCase; use WP_HTML_Span; /** diff --git a/tests/phpstan/WP_Block_Processor.stub b/tests/phpstan/WP_Block_Processor.stub new file mode 100644 index 000000000..cc178d7c7 --- /dev/null +++ b/tests/phpstan/WP_Block_Processor.stub @@ -0,0 +1,15 @@ +, innerBlocks: array>, innerHTML: string, innerContent: array}|null + */ + public function extract_full_block_and_advance(): ?array {} +}