Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Adding backed enum support to query builder for meta #478

Merged
merged 8 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ vendor
# Cache files
.phpcs/*.json
.phpunit.result.cache
.phpunit.cache/

# Composer files
composer.lock
Expand All @@ -26,4 +27,4 @@ Thumbs.db
# IDE files
*.code-workspace
.idea
.vscode
.vscode
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v0.12.9 - 2023-11-27
## v1.0.0 - YYYY-MM-DD

### Added

- Added PHPUnit 10 support.
- Adds database-specific collections with storage of the `found_rows` value.

### Changed

- Overhauled queue performance and added admin interface.

## v0.12.10 - 2023-11-27

### Changed

- Removed PHPUnit 10 support to prevent a breaking change. Moved to 1.x.

## v0.12.8 - 2023-11-21
## v0.12.9 - 2023-11-21

### Changed

Expand Down
49 changes: 49 additions & 0 deletions bin/convert-psr4-dir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Helper script to convert the tests/ folder to a PSR-4 folder structure.
*
* Moves tests/example/sub/File.php to tests/Example/Sub/FileTest.php. Only
* convert the sub-part of the path (ignore anything before and including
* tests/).
*
* phpcs:disable
*/

use Mantle\Support\Str;
use Symfony\Component\Finder\Finder;

use function Mantle\Support\Helpers\str;

require_once __DIR__ . '/../vendor/autoload.php';

$finder = ( new Finder() )
->in( realpath( __DIR__ . '/../tests' ) )
->directories()
->notPath( '#fixtures|__snapshots__|template-parts#' );

$base = Str::trailing_slash( realpath( __DIR__ . '/../tests/' ) );

foreach ( $finder as $dir ) {
$old_dir = $dir->getRealPath();

if ( ! is_dir( $old_dir ) ) {
continue;
}

$parts = str( $old_dir )->after( $base )->explode( '/' );

$parts = $parts->map(
fn ( string $part ) => str( $part )->studly()->value(),
);

$new_dir = $base . $parts->implode( '/' );

dump( "Moving {$old_dir} to {$new_dir}" );

shell_exec( "git mv {$old_dir} {$old_dir}-tmp" );
shell_exec( "git mv {$old_dir}-tmp {$new_dir}" );
}

echo "\nDONE!\n";

exit( 0 );
132 changes: 132 additions & 0 deletions bin/convert-psr4-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Helper script to convert the tests/ package to PSR-4 coding standards.
*
* The eventual plan is to use this for the rest of the project when that can be
* converted over. Ideally this can be a reusable command in the future.
*
* phpcs:disable
*/

use Symfony\Component\Finder\Finder;

use function Mantle\Support\Helpers\str;

require_once __DIR__ . '/../vendor/autoload.php';

$finder = ( new Finder() )
->in( realpath( __DIR__ . '/../tests' ) )
->name( '*.php' )
->notName( [ 'bootstrap.php', 'sub-example.php', 'base-example.php' ] )
->notPath( '#fixtures|__snapshots__|template-parts#' );

$index = [];
$pass = true;

foreach ( $finder as $file ) {
$filename = str( $file->getFilename() )->lower();

if ( $filename->startsWith( 'test-' ) ) {
$new_filename = $filename
->after( 'test-' )
->before( '.php' )
->studly()
->append( 'Test.php' )
->replace( 'Wordpress', 'WordPress' );

$old_class_name = $filename
->before( '.php' )
->studlyUnderscore()
->replace( 'Wordpress', 'WordPress' );

$new_class_name = $filename
->after( 'test-' )
->before( '.php' )
->studly()
->append( 'Test' )
->replace( 'Wordpress', 'WordPress' );
} else {
foreach ( [ 'trait', 'class' ] as $type ) {
if ( ! $filename->startsWith( "{$type}-" ) ) {
continue;
}

$new_filename = $filename
->after( "{$type}-" )
->before( '.php' )
->studly()
->append( '.php' )
->replace( 'Wordpress', 'WordPress' );

$old_class_name = $filename
->after( "{$type}-" )
->before( '.php' )
->studlyUnderscore();

$new_class_name = $filename
->after( "{$type}-" )
->before( '.php' )
->studly()
->replace( 'Wordpress', 'WordPress' );
}
}

// Check if the file contains the class.
$contents = str( file_get_contents( $file->getRealPath() ) );

$type = $filename->startsWith( 'trait-' ) ? 'trait' : 'class';

if ( ! $contents->contains( "{$type} {$old_class_name->value()} ", true ) ) {
echo $file->getRealPath() . ' does not contain the expected legacy ' . $type . ' ' . $old_class_name->value() . PHP_EOL;

$pass = false;

continue;
}

$index[] = [
$type,
[
$file->getRealPath(),
$old_class_name->value(),
],
[
$file->getPath() . '/' . $new_filename->value(),
$new_class_name->value(),
],
];
}

if ( ! $pass ) {
echo "\n\nPlease fix the above errors before continuing.\n";

exit( 1 );
}

echo 'Processing ' . count( $index ) . ' files...';

foreach ( $index as $item ) {
[ $type, $old, $new ] = $item;

[ $old_file, $old_class ] = $old;
[ $new_file, $new_class ] = $new;

// Update the file with the new class name.
file_put_contents(
$old_file,
str( file_get_contents( $old_file ) )->replace( "{$type} {$old_class} ", "{$type} {$new_class} ", false )->value(),
);

// Update the file name.
if ( ! rename( $old_file, $new_file ) ) {
echo "Failed to rename {$old_file} to {$new_file}.\n";

exit( 1 );
}

echo "Updated {$old_file} to {$new_file} ({$old_class} to {$new_class}).\n";
}

echo "\nDONE!\n";

exit( 0 );
21 changes: 13 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^8.0",
"alleyinteractive/composer-wordpress-autoloader": "^1.0",
"alleyinteractive/wp-asset-manager": "^1.3.3",
"alleyinteractive/wp-asset-manager": "^1.3.4",
"alleyinteractive/wp-caper": "^2.0.1",
"alleyinteractive/wp-concurrent-remote-requests": "^1.0.2",
"alleyinteractive/wp-filter-side-effects": "^2.0",
Expand All @@ -28,13 +28,13 @@
"monolog/monolog": "^2.9.1",
"nesbot/carbon": "^2.68.1",
"nette/php-generator": "^3.6.9",
"nunomaduro/collision": "^6.4",
"nunomaduro/collision": "^6.0 || ^7.0",
"nunomaduro/termwind": "^1.15.1",
"psr/container": "^1.1.1 || ^2.0.2",
"psr/log": "^1.0.1 || ^2.0 || ^3.0",
"psr/simple-cache": "^3.0",
"ramsey/uuid": "^4.7.4",
"spatie/phpunit-snapshot-assertions": "^4.2",
"spatie/phpunit-snapshot-assertions": "^4.2 || ^5.1",
"symfony/console": "^6.0.19",
"symfony/finder": "^6.0.19",
"symfony/http-foundation": "^6.0.20",
Expand All @@ -54,8 +54,8 @@
"mockery/mockery": "^1.6.6",
"php-stubs/wp-cli-stubs": "^2.8",
"phpstan/phpdoc-parser": "^1.23.1",
"phpstan/phpstan": "1.10.43",
"phpunit/phpunit": "^9.3.3",
"phpstan/phpstan": "1.10.44",
"phpunit/phpunit": "^9.3.3 || ^10.0.7",
"predis/predis": "^2.2.0",
"squizlabs/php_codesniffer": "^3.7",
"symplify/monorepo-builder": "^10.3.3",
Expand Down Expand Up @@ -86,6 +86,8 @@
"mantle-framework/testkit": "self.version",
"mantle-framework/view": "self.version"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"files": [
"src/mantle/framework/helpers.php",
Expand All @@ -100,6 +102,11 @@
"src/mantle/testing/autoload.php"
]
},
"autoload-dev": {
"psr-4": {
"Mantle\\Tests\\": "tests/"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
Expand Down Expand Up @@ -137,7 +144,5 @@
"@phpstan",
"@phpunit"
]
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
2 changes: 1 addition & 1 deletion monorepo-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
],
ComposerJsonSection::REQUIRE_DEV => [
'alleyinteractive/alley-coding-standards' => '^1.0',
'phpunit/phpunit' => '^9.6.10',
'phpunit/phpunit' => '^9.3.3|^10.0.7',
],
],
);
Expand Down
23 changes: 11 additions & 12 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
>
<testsuites>
<testsuite name="mantle-framework">
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<testsuites>
<testsuite name="mantle-framework">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion src/mantle/assets/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"require": {
"php": "^8.0",
"alleyinteractive/composer-wordpress-autoloader": "^1.0",
"alleyinteractive/wp-asset-manager": "^1.3.3",
"alleyinteractive/wp-asset-manager": "^1.3.4",
"mantle-framework/contracts": "^0.12",
"mantle-framework/support": "^0.12"
},
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/database/model/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereTerm( array|\WP_Term|\Mantle\Database\Model\Term|int $term, ?string $taxonomy = null, string $operator = 'IN', string $field = 'term_id' )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> andWhereTerm( array|\WP_Term|\Mantle\Database\Model\Term|int $term, ?string $taxonomy = null, string $operator = 'IN', string $field = 'term_id' )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> orWhereTerm( array|\WP_Term|\Mantle\Database\Model\Term|int $term, ?string $taxonomy = null, string $operator = 'IN', string $field = 'term_id' )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereMeta( string $key, mixed $value, string $operator = '=' )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereMeta( string|\BackedEnum $key, mixed $value, string $operator = '=' )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereRaw( array|string $column, ?string $operator = null, mixed $value = null, string $boolean = 'AND' )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! I didn't know about BackedEnum

* @method static \Mantle\Database\Query\Post_Query_Builder<static> where_raw( array|string $column, ?string $operator = null, mixed $value = null, string $boolean = 'AND' )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> orWhereRaw( array|string $column, ?string $operator = null, mixed $value = null, string $boolean = 'AND' )
Expand Down
21 changes: 18 additions & 3 deletions src/mantle/database/model/meta/trait-model-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Mantle\Database\Model\Meta;

use BackedEnum;
use Mantle\Database\Model\Model_Exception;

/**
Expand Down Expand Up @@ -47,7 +48,7 @@ public function add_meta( string $meta_key, mixed $meta_value, mixed $prev_value
return;
}

\add_metadata( $this->get_meta_type(), $this->id(), $meta_key, $meta_value );
\add_metadata( $this->get_meta_type(), $this->id(), $meta_key, $this->serialize_value_for_storage( $meta_value ) );
}

/**
Expand All @@ -63,7 +64,7 @@ public function set_meta( string $meta_key, mixed $meta_value, mixed $prev_value
return;
}

\update_metadata( $this->get_meta_type(), $this->id(), $meta_key, $meta_value, $prev_value );
\update_metadata( $this->get_meta_type(), $this->id(), $meta_key, $this->serialize_value_for_storage( $meta_value ), $prev_value );
}

/**
Expand All @@ -73,7 +74,7 @@ public function set_meta( string $meta_key, mixed $meta_value, mixed $prev_value
* @param mixed $meta_value Previous meta value to delete.
*/
public function delete_meta( string $meta_key, mixed $meta_value = '' ): void {
\delete_metadata( $this->get_meta_type(), $this->id(), $meta_key, $meta_value );
\delete_metadata( $this->get_meta_type(), $this->id(), $meta_key, $this->serialize_value_for_storage( $meta_value ) );
}

/**
Expand Down Expand Up @@ -141,4 +142,18 @@ public function store_queued_meta(): void {

$this->queued_meta = [];
}

/**
* Serialize meta value for storage, converting all backed enums to their value.
*
* @param mixed $value Value to serialize.
* @return mixed
*/
protected function serialize_value_for_storage( mixed $value ): mixed {
if ( $value instanceof BackedEnum ) {
return $value->value;
}

return $value;
}
}
Loading
Loading