Skip to content

Commit

Permalink
Small qol improvements (#583)
Browse files Browse the repository at this point in the history
* Ensure you can pass blocks as arguments to block factory

* Change return type of dd() methods to never

* Make hookable call register_hooks() on construct

* Allow returning falsy

* Add a test to validate an edge case facing elsewhere
  • Loading branch information
srtfisher authored Sep 17, 2024
1 parent 994a328 commit f9d052c
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Dropped support for Redis as a cache backend in favor of the default object
cache drop-in.
- Allow returning falsey from `Collection::map_to_dictionary()`.

## v1.1.3 - 2024-08-14

Expand Down
2 changes: 1 addition & 1 deletion src/mantle/database/query/class-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ public function dump(): static {
/**
* Dump the query variables being passed to WP_Query and die.
*/
public function dd(): void {
public function dd(): never {
$this->dump();
die;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/http-client/class-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function dump() {
/**
* Dump the request to the screen and die.
*/
public function dd(): void {
public function dd(): never {
$this->dump();
exit( 1 );
}
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/http-client/class-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public function dump() {
/**
* Dump the response to the screen and exit.
*/
public function dd(): void {
public function dd(): never {
$this->dump();
exit( 1 );
}
Expand Down
8 changes: 8 additions & 0 deletions src/mantle/support/class-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,10 @@ public function map_to_dictionary( callable $callback ) {
foreach ( $this->items as $key => $item ) {
$pair = $callback( $item, $key );

if ( ! $pair || ! is_array( $pair ) ) {
continue;
}

$key = key( $pair );

$value = reset( $pair );
Expand Down Expand Up @@ -786,6 +790,10 @@ public function map_with_keys( callable $callback ) {
foreach ( $this->items as $key => $value ) {
$assoc = $callback( $value, $key );

if ( ! is_array( $assoc ) ) {
continue;
}

foreach ( $assoc as $map_key => $map_value ) {
$result[ $map_key ] = $map_value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/support/class-stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ public function dump() {
/**
* Dump the string and end the script.
*/
public function dd(): void {
public function dd(): never {
$this->dump();

exit( 1 );
Expand Down
3 changes: 1 addition & 2 deletions src/mantle/support/interface-enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ public function contains( $key, $operator = null, $value = null );
* Dump the collection and end the script.
*
* @param mixed ...$args
* @return void
*/
public function dd( ...$args );
public function dd( ...$args ): never;

/**
* Dump the collection.
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/support/traits/trait-enumerates-values.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function contains_strict( $key, $value = null ) {
*
* @param mixed ...$args
*/
public function dd( ...$args ): void {
public function dd( ...$args ): never {
$this->dump( ...$args );

exit( 1 );
Expand Down
7 changes: 7 additions & 0 deletions src/mantle/support/traits/trait-hookable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
* the respective WordPress hooks.
*/
trait Hookable {
/**
* Constructor (can be overridden by the trait user).
*/
public function __construct() {
$this->register_hooks();
}

/**
* Boot all actions and attribute methods on the service provider.
*
Expand Down
12 changes: 10 additions & 2 deletions src/mantle/testing/class-block-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,17 @@ public function __call( string $name, array $arguments ): string {
/**
* Generate a collection of blocks.
*
* @param array $blocks Blocks to generate.
* Blocks can be passed as an array or as individual arguments.
*
* @param array|string ...$blocks Blocks to generate.
*/
public function blocks( array $blocks ): string {
public function blocks( array|string ...$blocks ): string {
$blocks = isset( $blocks[0] ) && is_array( $blocks[0] ) ? $blocks[0] : $blocks;

if ( ! is_array( $blocks ) ) {
$blocks = [ $blocks ];
}

return collect( $blocks )
->map( fn ( $block ) => is_array( $block ) ? serialize_blocks( $block ) : $block )
->implode( "\n\n" );
Expand Down
46 changes: 29 additions & 17 deletions tests/Support/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2343,23 +2343,35 @@ public function testFlatMap($collection)
* @dataProvider collectionClassProvider
*/
#[DataProvider( 'collectionClassProvider' )]
// public function testMapToDictionary($collection)
// {
// $data = new $collection([
// ['id' => 1, 'name' => 'A'],
// ['id' => 2, 'name' => 'B'],
// ['id' => 3, 'name' => 'C'],
// ['id' => 4, 'name' => 'B'],
// ]);
//
// $groups = $data->map_to_dictionary(function ($item, $key) {
// return [$item['name'] => $item['id']];
// });
//
// $this->assertInstanceOf($collection, $groups);
// $this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->to_array());
// $this->assertIsArray($groups->get('A'));
// }
public function testMapToDictionary($collection)
{
$data = new $collection([
['id' => 1, 'name' => 'A'],
['id' => 2, 'name' => 'B'],
['id' => 3, 'name' => 'C'],
['id' => 4, 'name' => 'B'],
]);

$groups = $data->map_to_dictionary(function ($item, $key) {
return [$item['name'] => $item['id']];
});

$this->assertInstanceOf($collection, $groups);
$this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->to_array());
$this->assertIsArray($groups->get('A'));
}

public function testMapToDictionaryReturnFalsy() {
$groups = ( new Collection([1, 2, 3, 4, 5]) )->map_to_dictionary(function ($item, $key) {
if (2 === $item) {
return null;
}

return ['key' => $item];
});

$this->assertEquals(['key' => [1, 3, 4, 5]], $groups->to_array());
}

/**
* @dataProvider collectionClassProvider
Expand Down
37 changes: 37 additions & 0 deletions tests/Testing/BlockFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,43 @@ public function test_it_can_generate_blocks() {
"<!-- wp:image -->\n<figure class=\"wp-block-image\"><img src=\"https://picsum.photos/353/580\"/></figure>\n<!-- /wp:image -->",
block_factory()->image( 'https://picsum.photos/353/580' ),
);

$this->assertEquals(
'<!-- wp:heading {"level":2} -->
<h2>Heading Here</h2>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>Paragraph here.</p>
<!-- /wp:paragraph -->',
block_factory()->blocks( [
block_factory()->heading( 'Heading Here' ),
block_factory()->paragraph( 'Paragraph here.' ),
] ),
);

$this->assertEquals(
'<!-- wp:heading {"level":2} -->
<h2>Heading Here</h2>
<!-- /wp:heading -->',
block_factory()->blocks(
block_factory()->heading( 'Heading Here' ),
),
);

$this->assertEquals(
'<!-- wp:heading {"level":2} -->
<h2>Heading Here</h2>
<!-- /wp:heading -->
<!-- wp:paragraph -->
<p>Paragraph here.</p>
<!-- /wp:paragraph -->',
block_factory()->blocks(
block_factory()->heading( 'Heading Here' ),
block_factory()->paragraph( 'Paragraph here.' ),
),
);
}

public function test_it_throws_an_exception_for_invalid_blocks() {
Expand Down
9 changes: 9 additions & 0 deletions tests/Testing/Concerns/BlockAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public function setUp(): void {
$this->faker->paragraph_block,
$this->faker->heading_block( 4 ),
$this->faker->paragraph_block,
$this->faker->block(
'vendor/example-name',
'',
[
'moduleId' => 1234,
]
),
] ),
] );
}
Expand All @@ -33,13 +40,15 @@ public function test_string_has_content() {
$this->assertStringHasBlock( $this->post->post_content, 'core/paragraph' );
$this->assertStringHasBlock( $this->post->post_content, 'core/heading' );
$this->assertStringHasBlock( $this->post->post_content, 'core/heading', [ 'level' => 3 ] );
$this->assertStringHasBlock( $this->post->post_content, 'vendor/example-name', [ 'moduleId' => 1234 ] );
$this->assertStringNotHasBlock( $this->post->post_content, 'core/heading', [ 'level' => 5 ] );
}

public function test_post_has_content() {
$this->assertPostHasBlock( $this->post, 'core/paragraph' );
$this->assertPostHasBlock( $this->post, 'core/heading' );
$this->assertPostHasBlock( $this->post, 'core/heading', [ 'level' => 3 ] );
$this->assertPostHasBlock( $this->post, 'vendor/example-name', [ 'moduleId' => 1234 ] );
$this->assertPostNotHasBlock( $this->post, 'core/heading', [ 'level' => 5 ] );
}
}

0 comments on commit f9d052c

Please sign in to comment.