diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bee410e..6f850144 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Attachment factories now properly use the `Attachment_Factory` class. - Ensure that the `delete()` method of the HTTP Client doesn't set a body by default. - Ensure that `with_terms()` can support an array of term slugs when passed with a taxonomy index. diff --git a/src/mantle/database/factory/class-attachment-factory.php b/src/mantle/database/factory/class-attachment-factory.php index 28218504..7b87dfe3 100644 --- a/src/mantle/database/factory/class-attachment-factory.php +++ b/src/mantle/database/factory/class-attachment-factory.php @@ -75,6 +75,8 @@ public function with_image( string $file = null, int $parent = 0, int $width = 6 if ( ! $file ) { $file = $generated_images[ $hash ] = $this->generate_image( $width, $height ); } + } elseif ( ! file_exists( $file ) ) { + throw new RuntimeException( "File {$file} does not exist." ); } if ( empty( $file ) ) { diff --git a/src/mantle/database/factory/class-post-factory.php b/src/mantle/database/factory/class-post-factory.php index 6f1b41ad..ff9247ba 100644 --- a/src/mantle/database/factory/class-post-factory.php +++ b/src/mantle/database/factory/class-post-factory.php @@ -10,6 +10,7 @@ use Carbon\Carbon; use Closure; use Faker\Generator; +use Mantle\Database\Model\Attachment; use Mantle\Database\Model\Post; use WP_Post; @@ -145,7 +146,7 @@ function ( array $args, Closure $next ) use ( $file, $width, $height, $recycle ) update_post_meta( $post->ID, '_thumbnail_id', - ( new Attachment_Factory( $this->faker ) )->with_image( + Attachment::factory()->with_image( file: $file, width: $width, parent: $post->ID, diff --git a/src/mantle/database/factory/concerns/trait-resolves-factories.php b/src/mantle/database/factory/concerns/trait-resolves-factories.php index 3ccf0400..8cd93d0f 100644 --- a/src/mantle/database/factory/concerns/trait-resolves-factories.php +++ b/src/mantle/database/factory/concerns/trait-resolves-factories.php @@ -125,8 +125,10 @@ public static function default_factory_name( string $model_name ): string { } // Handle one-off models. - if ( Model\Site::class === $parent_class ) { + if ( in_array( Model\Site::class, [ $model_name, $parent_class ], true ) ) { return Factory\Blog_Factory::class; + } elseif ( in_array( Model\Attachment::class, [ $model_name, $parent_class ], true ) ) { + return Factory\Attachment_Factory::class; } $parent_class = Str::after_last( $parent_class, '\\' ); diff --git a/src/mantle/database/model/class-attachment.php b/src/mantle/database/model/class-attachment.php index 4dbeddbe..d2d4d06d 100644 --- a/src/mantle/database/model/class-attachment.php +++ b/src/mantle/database/model/class-attachment.php @@ -12,7 +12,7 @@ /** * Attachment Model * - * @method static \Mantle\Database\Factory\Post_Factory factory( array|callable|null $state = null ) + * @method static \Mantle\Database\Factory\Attachment_Factory factory( array|callable|null $state = null ) */ class Attachment extends Post { /** diff --git a/tests/Database/Factory/FactoryTest.php b/tests/Database/Factory/FactoryTest.php index 41772ed8..61e90aed 100644 --- a/tests/Database/Factory/FactoryTest.php +++ b/tests/Database/Factory/FactoryTest.php @@ -70,7 +70,7 @@ public function test_resolve_default( string $model, string $expected ) { public static function factory_resolve_default(): array { return [ - Model\Attachment::class => [ Model\Attachment::class, Factory\Post_Factory::class ], + Model\Attachment::class => [ Model\Attachment::class, Factory\Attachment_Factory::class ], Model\Post::class => [ Model\Post::class, Factory\Post_Factory::class ], Model\Term::class => [ Model\Term::class, Factory\Term_Factory::class ], Model\User::class => [ Model\User::class, Factory\User_Factory::class ],