Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/Block_Processor_HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/Compat/PolyfillsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace WP_CLI\Entity\Tests\Compat;

use PHPUnit\Framework\TestCase;
use WP_CLI\Tests\TestCase;

/**
* Test the PHP function polyfills.
Expand Down
15 changes: 12 additions & 3 deletions tests/Compat/WP_Block_ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'] );
Expand Down Expand Up @@ -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 );
Expand All @@ -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'] );
Expand All @@ -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'] );
}

/**
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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'] );
Expand All @@ -586,6 +593,7 @@ public function test_unicode_content_handling() {

$attrs = $processor->allocate_and_return_parsed_attributes();

$this->assertIsArray( $attrs );
$this->assertSame( '日本語テスト', $attrs['text'] );
}

Expand All @@ -603,6 +611,7 @@ public function test_span_offset_middle_block() {

$span = $processor->get_span();

$this->assertNotNull( $span );
$this->assertSame( strlen( $prefix ), $span->start );
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Compat/WP_HTML_SpanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace WP_CLI\Entity\Tests\Compat;

use PHPUnit\Framework\TestCase;
use WP_CLI\Tests\TestCase;
use WP_HTML_Span;

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/phpstan/WP_Block_Processor.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* Corrects the return type that WordPress documents for this method.
*
* WordPress documents `extract_full_block_and_advance()` as returning a list of
* blocks (`array[]|null`), but it returns the single block it extracted, as
* `Block_Processor_Helper` and the polyfill tests rely on.
*/
class WP_Block_Processor {
/**
* @return array{blockName: string|null, attrs: array<mixed>, innerBlocks: array<array<mixed>>, innerHTML: string, innerContent: array<mixed>}|null
*/
public function extract_full_block_and_advance(): ?array {}
}