Skip to content

Commit

Permalink
Merge branch '0.12.x' into flysystem
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher authored Aug 31, 2023
2 parents 7ba9bc9 + ba176d0 commit 8ee9fd9
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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.5 - 2023-09-01

### Fixed

- Improved the performance of the `with_image()` method on attachment factories.

## v0.12.4 - 2023-08-24

### Added
Expand Down
11 changes: 2 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"voku/portable-ascii": "^2.0.1"
},
"require-dev": {
"alleyinteractive/alley-coding-standards": "^1.0",
"alleyinteractive/alley-coding-standards": "^1.0.1",
"guzzlehttp/guzzle": "^7.7",
"league/flysystem-aws-s3-v3": "^3.15",
"mockery/mockery": "^1.6.6",
Expand All @@ -57,8 +57,7 @@
"predis/predis": "^2.2.0",
"squizlabs/php_codesniffer": "^3.7",
"symplify/monorepo-builder": "^10.3.3",
"szepeviktor/phpstan-wordpress": "^1.3",
"wp-coding-standards/wpcs": "dev-php-8-1 as 2.3.x-dev"
"szepeviktor/phpstan-wordpress": "^1.3"
},
"replace": {
"mantle-framework/assets": "self.version",
Expand Down Expand Up @@ -137,12 +136,6 @@
"@phpunit"
]
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/alleyinteractive/WordPress-Coding-Standards"
}
],
"minimum-stability": "dev",
"prefer-stable": true
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 28 additions & 2 deletions src/mantle/database/factory/class-attachment-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Faker\Generator;
use Mantle\Contracts\Database\Core_Object;
use Mantle\Database\Model\Attachment;
use RuntimeException;
use WP_Post;

use function Mantle\Support\Helpers\get_post_object;
Expand All @@ -21,6 +22,8 @@
* @template TObject of \Mantle\Database\Model\Attachment
*/
class Attachment_Factory extends Post_Factory {
use Concerns\Generates_Images;

/**
* Model to use when creating objects.
*
Expand All @@ -42,15 +45,38 @@ public function definition(): array {
/**
* Create an attachment object with an underlying image file.
*
* @throws RuntimeException If unable to generate image.
*
* @param string $file The file name to create attachment object from.
* @param int $parent The parent post ID.
* @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.
* @return static
*/
public function with_image( string $file = null, int $parent = 0, int $width = 640, int $height = 480 ): static {
public function with_image( string $file = null, int $parent = 0, int $width = 640, int $height = 480, bool $recycle = true ): static {
if ( ! $file ) {
$file = $this->faker->image( sys_get_temp_dir(), $width, $height );
static $generated_images = [
// Use the already generated default 600x480 image.
'6421c8050053a960a55c0e70f6006ca9' => __DIR__ . '/assets/factory/600x480.jpg',
];

$hash = md5( $width . $height );

// If we're recycling, and we've already generated an image of this size, use it.
if ( $recycle && isset( $generated_images[ $hash ] ) ) {
$file = $generated_images[ $hash ];
}

// If we're not recycling, or we haven't generated an image of this size,
// generate one and save it for later.
if ( ! $file ) {
$file = $generated_images[ $hash ] = $this->generate_image( $width, $height );
}
}

if ( empty( $file ) ) {
throw new RuntimeException( "Unable to generate {$width}x{$height} image." );
}

return $this->with_middleware(
Expand Down
102 changes: 102 additions & 0 deletions src/mantle/database/factory/concerns/trait-generates-images.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Generates_Images trait file
*
* @package Mantle
*/

namespace Mantle\Database\Factory\Concerns;

use Mantle\Http_Client\Factory;
use Mantle\Support\Str;
use RuntimeException;

/**
* Generate images for testing factories.
*
* Does not rely on FakerPHP since the image generation is being deprecated.
*/
trait Generates_Images {
/**
* Generate an image.
*
* @param int $width The width of the image.
* @param int $height The height of the image.
* @param string|null $directory The directory to store the image to, defaults to the system temp directory.
* @param string|null $filename The filename to save the image as.
* @return string
*/
public function generate_image( int $width, int $height, ?string $directory = null, ?string $filename = null ): string {
if ( ! $directory ) {
$directory = get_temp_dir();
}

$image = imagecreatetruecolor( $width, $height );

if ( ! is_gd_image( $image ) ) {
return $this->generate_remote_image( $width, $height, $directory, $filename );
}

imagefill( $image, 0, 0, imagecolorallocate( $image, 192, 192, 192 ) );

if ( ! $filename ) {
$filename = $this->generate_filename() . '.png';
}

// Ensure the filename ends with .png.
if ( ! Str::ends_with( $filename, '.png' ) ) {
$filename .= '.png';
}

imagepng( $image, "{$directory}/{$filename}" );

imagedestroy( $image );

return "{$directory}/{$filename}";
}

/**
* Generate an image by fetching the image from picsum.photos.
*
* @throws RuntimeException If the image cannot be fetched.
*
* @param int $width The width of the image.
* @param int $height The height of the image.
* @param string|null $directory The directory to save the image to.
* @param string|null $filename The filename to save the image as.
* @return string
*/
public function generate_remote_image( int $width, int $height, ?string $directory = null, ?string $filename = null ): string {
if ( ! $directory ) {
$directory = get_temp_dir();
}

if ( ! $filename ) {
$filename = $this->generate_filename() . '.jpg';
}

// Ensure the filename ends with .jpg.
if ( ! Str::ends_with( $filename, '.jpg' ) ) {
$filename .= '.jpg';
}

$request = ( new Factory() )
->stream( "{$directory}/{$filename}" )
->get( "https://picsum.photos/{$width}/{$height}" );

if ( ! $request->ok() ) {
throw new RuntimeException( 'Unable to fetch remote image from picsum.photos.' );
}

return "{$directory}/{$filename}";
}

/**
* Generate a random filename.
*
* @return string
*/
protected function generate_filename(): string {
return md5( wp_generate_password( 15, false ) );
}
}
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
1 change: 1 addition & 0 deletions tests/console/generators/test-printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ protected function lint_code( PhpFile $file, string $file_name ) {
$path,
'-vsn',
'--no-cache',
'--standard=' . dirname( MANTLE_PHPUNIT_INCLUDES_PATH, 2 ) . '/phpcs.xml',
];

// tip: turn off the output buffering for help debugging!
Expand Down

0 comments on commit 8ee9fd9

Please sign in to comment.