Skip to content

Commit

Permalink
Ensure attachments use attachment factory (#596)
Browse files Browse the repository at this point in the history
* Ensure that file exists before using with attachment factory

* Ensure that attachment models use the attachment factory

* CHANGELOG

* Use the attachment resolver
  • Loading branch information
srtfisher authored Oct 31, 2024
1 parent 3573a07 commit 1d4a671
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/mantle/database/factory/class-attachment-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
3 changes: 2 additions & 1 deletion src/mantle/database/factory/class-post-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\' );
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/database/model/class-attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Attachment Model
*
* @method static \Mantle\Database\Factory\Post_Factory<static, \WP_Post, static> factory( array|callable|null $state = null )
* @method static \Mantle\Database\Factory\Attachment_Factory<static, \WP_Post, static> factory( array|callable|null $state = null )
*/
class Attachment extends Post {
/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/Factory/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ],
Expand Down

0 comments on commit 1d4a671

Please sign in to comment.