Skip to content

Commit

Permalink
Merge branch '1.x' into flysystem
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher authored Sep 13, 2023
2 parents 8ee9fd9 + 33d3edc commit 94f81cc
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 30 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ 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.6 - 2023-09-06

### Fixed

- Fix issue with custom post types/taxonomies and factories not resuming the
correct post type/taxonomy after creation.

## v0.12.5 - 2023-09-01

### Fixed
Expand Down
11 changes: 4 additions & 7 deletions src/mantle/database/factory/class-post-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Post_Factory extends Factory {
* @param Generator $faker Faker generator.
* @param string $post_type Post type to use.
*/
public function __construct( Generator $faker, protected string $post_type = 'post' ) {
public function __construct( Generator $faker, public string $post_type = 'post' ) {
parent::__construct( $faker );
}

Expand Down Expand Up @@ -80,12 +80,9 @@ public function with_thumbnail(): static {
* @return static
*/
public function with_post_type( string $post_type ): static {
return $this->with_middleware(
function ( array $args, Closure $next ) use ( $post_type ) {
$args['post_type'] = $post_type;

return $next( $args );
}
return tap(
clone $this,
fn ( Post_Factory $factory ) => $factory->post_type = $post_type,
);
}

Expand Down
31 changes: 25 additions & 6 deletions src/mantle/database/factory/class-term-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Term_Factory extends Factory {
* @param Generator $faker Faker generator.
* @param string $taxonomy Taxonomy name.
*/
public function __construct( Generator $faker, protected string $taxonomy ) {
public function __construct( Generator $faker, protected string $taxonomy = 'post_tag' ) {
parent::__construct( $faker );
}

Expand Down Expand Up @@ -91,12 +91,31 @@ function ( array $args, Closure $next ) use ( $posts ) {
* @return \WP_Term|Term|null
*/
public function get_object_by_id( int $object_id ) {
$term = get_term_object( $object_id );
return $this->as_models
? $this->model::find( $object_id )
: get_term_object( $object_id );
}

if ( $term && $this->as_models ) {
return Term::new_from_existing( (array) $term );
}
/**
* Create a new factory instance to create posts for a specific taxonomy.
*
* @param string $taxonomy Post type to use.
* @return static
*/
public function with_taxonomy( string $taxonomy ): static {
return tap(
clone $this,
fn ( Term_Factory $factory ) => $factory->taxonomy = $taxonomy,
);
}

return $term;
/**
* Alias for {@see Term_Factory::with_taxonomy()}.
*
* @param string $taxonomy Taxonomy to use.
* @return static
*/
public function for( string $taxonomy ): static {
return $this->with_taxonomy( $taxonomy );
}
}
7 changes: 4 additions & 3 deletions src/mantle/database/model/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
* @property string $title Alias to post_title.
*
* @method static \Mantle\Database\Query\Post_Query_Builder<static> anyStatus()
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereId( int $id )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> where( string|array $attribute, mixed $value )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereId( int $id )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereNotIn(string $key, array $values)
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereIn(string $key, array $values)
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereName( string $name )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereSlug( string $slug )
* @method static \Mantle\Database\Query\Post_Query_Builder<static> whereStatus( string $status )
Expand Down Expand Up @@ -142,8 +144,7 @@ public static function find( $object ) {
}

// Verify the object type matches the model type.
$object_name = static::get_object_name();
if ( $post->post_type !== $object_name ) {
if ( static::get_object_name() !== $post->post_type ) {
return null;
}

Expand Down
12 changes: 11 additions & 1 deletion src/mantle/database/model/concerns/trait-has-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace Mantle\Database\Model\Concerns;

use Mantle\Database\Factory\Factory;
use Mantle\Database\Factory\Post_Factory;
use Mantle\Database\Factory\Term_Factory;

/**
* Model Database Factory
Expand All @@ -19,12 +21,20 @@ trait Has_Factory {
* @param array|callable $state Default state array or callable that will be invoked to set state.
* @return \Mantle\Database\Factory\Factory<static>
*/
public static function factory( array|callable|null $state = null ) {
public static function factory( array|callable|null $state = null ): Factory {
$factory = static::new_factory() ?: Factory::factory_for_model( static::class );

return $factory
->as_models()
->with_model( static::class )
->when(
$factory instanceof Post_Factory,
fn ( Post_Factory $factory ) => $factory->with_post_type( static::get_object_name() ), // @phpstan-ignore-line expects
)
->when(
$factory instanceof Term_Factory,
fn ( Term_Factory $factory ) => $factory->with_taxonomy( static::get_object_name() ), // @phpstan-ignore-line expects
)
->when( is_array( $state ) || is_callable( $state ), fn ( Factory $factory ) => $factory->state( $state ) );
}

Expand Down
34 changes: 34 additions & 0 deletions src/mantle/database/query/class-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ public function __construct( $model ) {
*/
abstract public function get(): Collection;

/**
* Get the count of the query results.
*
* @return int
*/
abstract public function count(): int;

/**
* Get the query arguments.
*
Expand Down Expand Up @@ -967,4 +974,31 @@ public function dd(): void {
$this->dump();
die;
}

/**
* Check if any models are found for the current query.
*
* @return bool
*/
public function exists(): bool {
return $this->count() > 0;
}

/**
* Check if no models are found for the current query.
*
* @return bool
*/
public function doesntExist(): bool {
return ! $this->exists();
}

/**
* Alias for `doesntExists()`.
*
* @return bool
*/
public function doesnt_exist(): bool {
return $this->doesntExist();
}
}
22 changes: 21 additions & 1 deletion src/mantle/database/query/class-post-query-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ public function get(): Collection {
return $this->eager_load_relations( $models );
}

/**
* Get the count of the query results.
*
* @return int
*/
public function count(): int {
$this->take( -1 );

$query = new \WP_Query();

// Store the query hash for reference by side-effects.
$this->query_hash = spl_object_hash( $query );

$this->with_clauses(
fn () => $query->query( $this->get_query_args() ),
);

return $query->found_posts;
}

/**
* Retrieve hydrated models for the post IDs.
*
Expand Down Expand Up @@ -288,7 +308,7 @@ function ( string $sql, \WP_Query $query ) use ( $die ) {
return $sql;
},
10,
2
2
);

return $this;
Expand Down
26 changes: 25 additions & 1 deletion src/mantle/database/query/class-term-query-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ public function get(): Collection {
);
}

/**
* Get the count of the query results.
*
* @return int
*/
public function count(): int {
$this->take( -1 );

$query = new \WP_Term_Query();

$this->query_hash = spl_object_hash( $query );

return $this->with_clauses(
fn (): int => (int) $query->query(
array_merge(
$this->get_query_args(),
[
'fields' => 'count',
],
),
),
);
}

/**
* Dump the SQL query being executed.
*
Expand All @@ -149,7 +173,7 @@ function ( mixed $terms, \WP_Term_Query $query ) use ( $die ) {
return $terms;
},
10,
2
2
);

return $this;
Expand Down
1 change: 1 addition & 0 deletions src/mantle/database/query/concerns/trait-query-clauses.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected function unregister_clauses(): void {
* Execute a callback with the clauses applied only for the closure.
*
* @template TReturnValue
*
* @param callable(): TReturnValue $callback The callback to execute.
* @return TReturnValue The return value of the callback.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/mantle/testing/class-installation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ public function with_active_plugins( array $plugins ): static {
public function install() {
require_once __DIR__ . '/core-polyfill.php';

if ( Utils::is_debug_mode() ) {
Utils::info( '🚨 Debug mode is enabled.' );
}

if ( $this->rsync_to ) {
$this->perform_rsync_testsuite();
return $this;
Expand Down
55 changes: 47 additions & 8 deletions src/mantle/testing/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Mantle\Testing;

use Mantle\Support\Collection;
use Mantle\Support\Str;
use Mantle\Testing\Doubles\Spy_REST_Server;

Expand Down Expand Up @@ -267,7 +268,7 @@ public static function shell_safe( string|bool $string ): string {
return $string ? 'true' : 'false';
}

return empty( trim( $string ) ) ? "''" : $string;
return empty( trim( $string ) ) ? "''" : "\"{$string}\"";
}

/**
Expand All @@ -289,12 +290,33 @@ public static function install_wordpress(
): void {
$branch = static::env( 'MANTLE_CI_BRANCH', 'HEAD' );

// Compile the variables to pass to the shell script.
$variables = collect(
[
[ 'WP_CORE_DIR', $directory ],
[ 'WP_MULTISITE', static::env( 'WP_MULTISITE', '0' ) ],
]
)
->when(
$use_sqlite_db,
fn ( Collection $collection ) => $collection->push( [ 'WP_USE_SQLITE', 'true' ] )
)
->when(
static::is_debug_mode(),
fn ( Collection $collection ) => $collection->push( [ 'INSTALL_WP_TEST_DEBUG', 'true' ] )
)
->when(
! empty( static::env( 'CACHEDIR', '' ) ),
fn ( Collection $collection ) => $collection->push( [ 'CACHEDIR', static::env( 'CACHEDIR', '' ) ] )
)
->map(
fn ( array $item ) => sprintf( 'export %s=%s', $item[0], static::shell_safe( $item[1] ) )
)
->implode( ' && ' );

$command = sprintf(
'export WP_CORE_DIR=%s WP_MULTISITE=%s WP_USE_SQLITE=%s INSTALL_WP_TEST_DEBUG=%s && curl -s %s | bash -s %s',
$directory,
static::shell_safe( static::env( 'WP_MULTISITE', '0' ) ),
static::shell_safe( $use_sqlite_db ),
static::shell_safe( static::is_debug_mode() ),
'%s && curl -s %s | bash -s %s',
$variables,
"https://raw.githubusercontent.com/alleyinteractive/mantle-ci/{$branch}/install-wp-tests.sh",
collect(
[
Expand Down Expand Up @@ -330,9 +352,24 @@ public static function install_wordpress(
public static function install_plugin( string $directory, string $plugin, string $version_or_url = 'latest' ): void {
$branch = static::env( 'MANTLE_CI_BRANCH', 'HEAD' );

// Compile the variables to pass to the shell script.
$variables = collect(
[
[ 'WP_CORE_DIR', $directory ],
]
)
->when(
! empty( static::env( 'CACHEDIR', '' ) ),
fn ( Collection $collection ) => $collection->push( [ 'CACHEDIR', static::env( 'CACHEDIR', '' ) ] )
)
->map(
fn ( array $item ) => sprintf( 'export %s=%s', $item[0], static::shell_safe( $item[1] ) )
)
->implode( ' && ' );

$command = sprintf(
'export WP_CORE_DIR=%s && curl -s %s | bash -s %s',
$directory,
'%s && curl -s %s | bash -s %s',
$variables,
"https://raw.githubusercontent.com/alleyinteractive/mantle-ci/{$branch}/install-plugin.sh",
collect(
[
Expand Down Expand Up @@ -400,6 +437,8 @@ public static function command( $command, &$exit_code = null ) {
$command = implode( ' ', $command );
}

$output = null;

exec( $command, $output, $exit_code ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_exec

// Display the command runtime if in debug mode.
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
define( 'MANTLE_PHPUNIT_TEMPLATE_PATH', __DIR__ . '/template-parts' );

// Enable debugging flag for local development on the testing framework.
define( 'MANTLE_TESTING_DEBUG', true );
// define( 'MANTLE_TESTING_DEBUG', true );

\Mantle\Testing\manager()
->maybe_rsync_plugin()
Expand Down
Loading

0 comments on commit 94f81cc

Please sign in to comment.