Skip to content

Commit

Permalink
Merging in feature/fake-request-with-image
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Aug 6, 2024
2 parents b07dcc8 + d0b33a5 commit 3601a5f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed
### Added

- Fixed an issue with taxonomy registration not returning an array.
- Added `with_real_thumbnail()` method to post factory for creating posts with
real underlying thumbnail files.

### Changed

- Added support for faking specific HTTP requests by method.
- Added helper for fluently building HTTP sequence responses.

### Fixed

- Fixed an issue with taxonomy registration not returning an array.

## v1.1.2 - 2024-06-20

### Fixed
Expand Down
36 changes: 35 additions & 1 deletion src/mantle/database/factory/class-post-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public function with_terms( ...$terms ): static {
}

/**
* Create a new factory instance to create posts with a thumbnail.
* Attach a post thumbnail to the post.
*
* Note: the underlying attachment does not actually exist for performance.
* You can use `with_real_thumbnail()` to create a real underlying attachment
* for the post thumbnail.
*/
public function with_thumbnail(): static {
return $this->with_meta(
Expand All @@ -75,6 +79,36 @@ public function with_thumbnail(): static {
);
}

/**
* Attach a thumbnail to the post with an underlying file attachment.
*
* @param string $file The file name to create attachment object from.
* @param int $width The width of the image.
* @param int $height The height of the image.
* @param bool $recycle Whether to recycle the image file.
*/
public function with_real_thumbnail( string $file = null, int $width = 640, int $height = 480, bool $recycle = true ): static {
return $this->with_middleware(
function ( array $args, Closure $next ) use ( $file, $width, $height, $recycle ) {
$post = $next( $args );

update_post_meta(
$post->ID,
'_thumbnail_id',
( new Attachment_Factory( $this->faker ) )->with_image(
file: $file,
width: $width,
parent: $post->ID,
height: $height,
recycle: $recycle
)->create(),
);

return $post;
}
);
}

/**
* Create a new factory instance to create posts for a specific post type.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Database/Factory/UnitTestingFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ public function test_attachment_with_image() {
$this->assertNotEmpty( wp_get_attachment_image_url( $attachment ) );
}

public function test_post_with_real_image() {
$post = static::factory()->post->with_real_thumbnail()->create();

$this->assertNotEmpty( wp_get_attachment_image_url( get_post_thumbnail_id( $post ) ) );
}

protected function shim_test( string $class_name, string $property ) {
$this->assertInstanceOf( $class_name, static::factory()->$property->create_and_get() );

Expand Down

0 comments on commit 3601a5f

Please sign in to comment.