From 5c37b70f86aa991bdad58302ee9df96d64e4972d Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 14 Nov 2023 10:35:47 -0500 Subject: [PATCH] Ensure factories can be used with data providers (#462) * Overhauling the generics of factories to return the proper type * Adding a global constant to make testing easier * Include some functionality from core * Ensure that post factories work in data providers * Switch to get_comment_delimited_block_content() to compile blocks * Ensure that a new line is added before/after content --- phpcs.xml | 1 + .../class-factory-service-provider.php | 2 +- .../factory/class-attachment-factory.php | 9 ++- .../database/factory/class-blog-factory.php | 8 ++- .../factory/class-comment-factory.php | 6 +- .../factory/class-factory-container.php | 65 +++++++++++++------ src/mantle/database/factory/class-factory.php | 20 +++--- .../database/factory/class-fluent-factory.php | 12 ++-- .../factory/class-network-factory.php | 8 ++- .../database/factory/class-post-factory.php | 11 +++- .../database/factory/class-term-factory.php | 8 ++- .../database/factory/class-user-factory.php | 8 ++- .../database/model/class-attachment.php | 3 +- src/mantle/database/model/class-comment.php | 2 + src/mantle/database/model/class-model.php | 3 + src/mantle/database/model/class-post.php | 3 + src/mantle/database/model/class-site.php | 2 + src/mantle/database/model/class-term.php | 1 + src/mantle/database/model/class-user.php | 2 + .../model/concerns/trait-has-factory.php | 8 ++- src/mantle/faker/class-faker-provider.php | 20 ++---- src/mantle/testing/wordpress-bootstrap.php | 8 +++ tests/database/factory/test-factory.php | 6 +- .../factory/test-unit-testing-factory.php | 32 +++++++-- 24 files changed, 172 insertions(+), 76 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index eb25c7720..42444f7c5 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -28,6 +28,7 @@ + diff --git a/src/mantle/database/class-factory-service-provider.php b/src/mantle/database/class-factory-service-provider.php index 23d948af3..7ec98f2f5 100644 --- a/src/mantle/database/class-factory-service-provider.php +++ b/src/mantle/database/class-factory-service-provider.php @@ -46,7 +46,7 @@ function ( $app, $parameters ) { $locale = config( 'app.faker_locale', Factory::DEFAULT_LOCALE ); if ( ! isset( static::$fakers[ $locale ] ) ) { - static::$fakers[ $locale ] = Factory::create(); + static::$fakers[ $locale ] = Factory::create( $locale ); static::$fakers[ $locale ]->addProvider( new Faker_Provider( static::$fakers[ $locale ] ) diff --git a/src/mantle/database/factory/class-attachment-factory.php b/src/mantle/database/factory/class-attachment-factory.php index 4363966bf..d50187463 100644 --- a/src/mantle/database/factory/class-attachment-factory.php +++ b/src/mantle/database/factory/class-attachment-factory.php @@ -8,7 +8,6 @@ namespace Mantle\Database\Factory; use Closure; -use Faker\Generator; use Mantle\Contracts\Database\Core_Object; use Mantle\Database\Model\Attachment; use RuntimeException; @@ -19,7 +18,11 @@ /** * Attachment Factory * - * @template TObject of \Mantle\Database\Model\Attachment + * @template TModel of \Mantle\Database\Model\Attachment + * @template TObject of \WP_Post + * @template TReturnValue + * + * @extends Factory */ class Attachment_Factory extends Post_Factory { use Concerns\Generates_Images; @@ -27,7 +30,7 @@ class Attachment_Factory extends Post_Factory { /** * Model to use when creating objects. * - * @var class-string + * @var class-string */ protected string $model = Attachment::class; diff --git a/src/mantle/database/factory/class-blog-factory.php b/src/mantle/database/factory/class-blog-factory.php index f2a76ff74..29ed305c7 100644 --- a/src/mantle/database/factory/class-blog-factory.php +++ b/src/mantle/database/factory/class-blog-factory.php @@ -14,13 +14,17 @@ /** * Blog Factory * - * @template TObject of \Mantle\Database\Model\Site + * @template TModel of \Mantle\Database\Model\Site + * @template TObject of \WP_Site + * @template TReturnValue + * + * @extends Factory */ class Blog_Factory extends Factory { /** * Model to use when creating objects. * - * @var class-string + * @var class-string */ protected string $model = Site::class; diff --git a/src/mantle/database/factory/class-comment-factory.php b/src/mantle/database/factory/class-comment-factory.php index 3f8abb31e..b71189db4 100644 --- a/src/mantle/database/factory/class-comment-factory.php +++ b/src/mantle/database/factory/class-comment-factory.php @@ -14,7 +14,11 @@ /** * Term Factory * - * @template TObject of \Mantle\Database\Model\Comment + * @template TModel of \Mantle\Database\Model\Comment + * @template TObject of \WP_Comment + * @template TReturnValue + * + * @extends Factory */ class Comment_Factory extends Factory { /** diff --git a/src/mantle/database/factory/class-factory-container.php b/src/mantle/database/factory/class-factory-container.php index 1e324cef4..ff2e89239 100644 --- a/src/mantle/database/factory/class-factory-container.php +++ b/src/mantle/database/factory/class-factory-container.php @@ -7,7 +7,9 @@ namespace Mantle\Database\Factory; +use Faker\Generator; use Mantle\Contracts\Container; +use Mantle\Faker\Faker_Provider; /** * Collect all the Database Factories for IDE Support @@ -21,72 +23,72 @@ class Factory_Container { /** * Attachment Factory * - * @var Attachment_Factory<\WP_Post|\Mantle\Database\Model\Attachment> + * @var Attachment_Factory<\Mantle\Database\Model\Attachment, \WP_Post, \WP_Post> */ - public $attachment; + public Attachment_Factory $attachment; /** * Blog Factory * - * @var Blog_Factory<\WP_Site|\Mantle\Database\Model\Site> + * @var Blog_Factory<\Mantle\Database\Model\Site, \WP_Site, \WP_Site> */ - public $blog; + public Blog_Factory $blog; /** * Category Factory * - * @var Term_Factory<\WP_Term|\Mantle\Database\Model\Term> + * @var Term_Factory<\Mantle\Database\Model\Term, \WP_Term, \WP_Term> */ - public $category; + public Term_Factory $category; /** * Comment Factory * - * @var Comment_Factory<\WP_Comment> + * @var Comment_Factory<\Mantle\Database\Model\Comment, \WP_Comment, \WP_Comment> */ - public $comment; + public Comment_Factory $comment; /** * Network Factory * - * @var Network_Factory<\WP_Network> + * @var Network_Factory */ - public $network; + public Network_Factory $network; /** * Page Factory * - * @var Post_Factory<\WP_Post|\Mantle\Database\Model\Post> + * @var Post_Factory<\Mantle\Database\Model\Post, \WP_Post, \WP_Post> */ public $page; /** * Post Factory * - * @var Post_Factory<\WP_Post|\Mantle\Database\Model\Post> + * @var Post_Factory<\Mantle\Database\Model\Post, \WP_Post, \WP_Post> */ - public $post; + public Post_Factory $post; /** * Tag Factory * - * @var Term_Factory<\WP_Term|\Mantle\Database\Model\Term> + * @var Term_Factory<\Mantle\Database\Model\Term, \WP_Term, \WP_Term> */ - public $tag; + public Term_Factory $tag; /** * Term Factory (alias for Tag Factory). * - * @var Term_Factory<\WP_Term|\Mantle\Database\Model\Term> + * @var Term_Factory<\Mantle\Database\Model\Term, \WP_Term, \WP_Term> */ - public $term; + public Term_Factory $term; /** * User Factory * - * @var User_Factory<\WP_User|\Mantle\Database\Model\User> + * @var User_Factory<\Mantle\Database\Model\User, \WP_User, \WP_User> */ - public $user; + public User_Factory $user; /** * Constructor. @@ -94,6 +96,8 @@ class Factory_Container { * @param Container $container Container instance. */ public function __construct( Container $container ) { + $this->setup_faker( $container ); + $this->attachment = $container->make( Attachment_Factory::class ); $this->category = $container->make( Term_Factory::class, [ 'taxonomy' => 'category' ] ); $this->comment = $container->make( Comment_Factory::class ); @@ -108,4 +112,27 @@ public function __construct( Container $container ) { $this->network = $container->make( Network_Factory::class ); } } + + /** + * Set up the Faker instance in the container. + * + * Primarily used when faker/factory is called from a data provider and the + * application hasn't been setup yet. + * + * @param Container $container Container instance. + */ + protected function setup_faker( Container $container ): void { + $container->singleton_if( + Generator::class, + function () { + $generator = \Faker\Factory::create(); + + $generator->unique(); + + $generator->addProvider( new Faker_Provider( $generator ) ); + + return $generator; + }, + ); + } } diff --git a/src/mantle/database/factory/class-factory.php b/src/mantle/database/factory/class-factory.php index c7471684e..767fd5905 100644 --- a/src/mantle/database/factory/class-factory.php +++ b/src/mantle/database/factory/class-factory.php @@ -24,9 +24,11 @@ /** * Base Factory * - * @template TObject of \Mantle\Database\Model\Model + * @template TModel of \Mantle\Database\Model\Model + * @template TObject + * @template TReturnValue * - * @method \Mantle\Database\Factory\Fluent_Factory count(int $count) + * @method \Mantle\Database\Factory\Fluent_Factory count(int $count) */ abstract class Factory { use Concerns\Resolves_Factories, @@ -74,7 +76,7 @@ abstract public function definition(): array; * Retrieves an object by ID. * * @param int $object_id The object ID. - * @return mixed + * @return TModel|TObject|null */ abstract public function get_object_by_id( int $object_id ); @@ -91,7 +93,7 @@ public function create( array $args = [] ): mixed { /** * Generate models from the factory. * - * @return static + * @return static */ public function as_models() { return tap( @@ -103,7 +105,7 @@ public function as_models() { /** * Generate core WordPress objects from the factory. * - * @return static + * @return static */ public function as_objects() { return tap( @@ -145,8 +147,10 @@ public function without_middleware() { * * @throws \InvalidArgumentException If the model does not extend from the base model class. * - * @param class-string $model The model to use. - * @return static + * @template TNewModel of \Mantle\Database\Model\Model + * + * @param class-string $model The model to use. + * @return static */ public function with_model( string $model ) { // Validate that model extends from the base model class. @@ -210,7 +214,7 @@ public function create_many( int $count, array $args = [] ) { * Creates an object and returns its object. * * @param array $args Optional. The arguments for the object to create. Default is empty array. - * @return TObject The created object. + * @return TReturnValue The created object. */ public function create_and_get( array $args = [] ) { return $this->get_object_by_id( $this->create( $args ) ); diff --git a/src/mantle/database/factory/class-fluent-factory.php b/src/mantle/database/factory/class-fluent-factory.php index 653306c1e..8b45d8783 100644 --- a/src/mantle/database/factory/class-fluent-factory.php +++ b/src/mantle/database/factory/class-fluent-factory.php @@ -18,7 +18,11 @@ * Extends upon the factory that is included with Mantle (one that is designed * to mirror WordPress) and builds upon it to provide a fluent interface. * - * @template TObject of \Mantle\Database\Model\Model + * @template TModel of \Mantle\Database\Model\Model + * @template TObject + * @template TReturnValue + * + * @extends Factory */ class Fluent_Factory extends Factory { /** @@ -57,7 +61,7 @@ public function count( int $count ): static { * Create one or multiple objects and return the IDs. * * @param array $args Arguments to pass to the factory. - * @return \Mantle\Support\Collection|mixed + * @return \Mantle\Support\Collection|mixed */ public function create( array $args = [] ): mixed { if ( 1 === $this->count ) { @@ -71,7 +75,7 @@ public function create( array $args = [] ): mixed { * Create one or multiple objects and return the objects. * * @param array $args Arguments to pass to the factory. - * @return \Mantle\Support\Collection|TObject + * @return \Mantle\Support\Collection|TReturnValue */ public function create_and_get( array $args = [] ): mixed { if ( 1 === $this->count ) { @@ -98,7 +102,7 @@ public function definition(): array { * Retrieves an object by ID. * * @param mixed $object_id The object ID. - * @return TObject + * @return TReturnValue */ public function get_object_by_id( mixed $object_id ): mixed { return $this->factory->get_object_by_id( $object_id ); diff --git a/src/mantle/database/factory/class-network-factory.php b/src/mantle/database/factory/class-network-factory.php index 65f5b8027..f708462f3 100644 --- a/src/mantle/database/factory/class-network-factory.php +++ b/src/mantle/database/factory/class-network-factory.php @@ -7,12 +7,14 @@ namespace Mantle\Database\Factory; -use Faker\Generator; - /** * Network Factory * - * @template TObject + * @template TModel + * @template TObject of \WP_Network + * @template TReturnValue + * + * @extends Factory */ class Network_Factory extends Factory { /** diff --git a/src/mantle/database/factory/class-post-factory.php b/src/mantle/database/factory/class-post-factory.php index c69f53c96..1ad971b94 100644 --- a/src/mantle/database/factory/class-post-factory.php +++ b/src/mantle/database/factory/class-post-factory.php @@ -19,7 +19,11 @@ /** * Post Factory * - * @template TObject of \Mantle\Database\Model\Post + * @template TModel of \Mantle\Database\Model\Post + * @template TObject + * @template TReturnValue + * + * @extends Factory */ class Post_Factory extends Factory { use Concerns\With_Meta; @@ -27,7 +31,7 @@ class Post_Factory extends Factory { /** * Model to use when creating objects. * - * @var class-string + * @var class-string */ protected string $model = Post::class; @@ -44,7 +48,7 @@ public function __construct( Generator $faker, public string $post_type = 'post' /** * Create a new factory instance to create posts with a set of terms. * - * @param array|\WP_Term|int|string ...$terms Terms to assign to the post. + * @param array>|\WP_Term|int|string ...$terms Terms to assign to the post. * @return static */ public function with_terms( ...$terms ): static { @@ -172,6 +176,7 @@ public function create_ordered_set( * * @param int $object_id The object ID. * @return Post|WP_Post|null + * @phpstan-return TModel|TObject|null */ public function get_object_by_id( int $object_id ) { return $this->as_models diff --git a/src/mantle/database/factory/class-term-factory.php b/src/mantle/database/factory/class-term-factory.php index 52db8f8c7..4d49ecabb 100644 --- a/src/mantle/database/factory/class-term-factory.php +++ b/src/mantle/database/factory/class-term-factory.php @@ -16,7 +16,11 @@ /** * Term Factory * - * @template TObject of \Mantle\Database\Model\Term + * @template TModel of \Mantle\Database\Model\Term + * @template TObject of \WP_Term + * @template TReturnValue + * + * @extends Factory */ class Term_Factory extends Factory { use Concerns\With_Meta; @@ -24,7 +28,7 @@ class Term_Factory extends Factory { /** * Model to use when creating objects. * - * @var class-string + * @var class-string */ protected string $model = Term::class; diff --git a/src/mantle/database/factory/class-user-factory.php b/src/mantle/database/factory/class-user-factory.php index ce41b3d42..927057ad1 100644 --- a/src/mantle/database/factory/class-user-factory.php +++ b/src/mantle/database/factory/class-user-factory.php @@ -14,7 +14,11 @@ /** * User Factory * - * @template TObject of \Mantle\Database\Model\User + * @template TModel of \Mantle\Database\Model\User + * @template TObject of \WP_User + * @template TReturnValue + * + * @extends Factory */ class User_Factory extends Factory { use Concerns\With_Meta; @@ -22,7 +26,7 @@ class User_Factory extends Factory { /** * Model to use when creating objects. * - * @var class-string + * @var class-string */ protected string $model = User::class; diff --git a/src/mantle/database/model/class-attachment.php b/src/mantle/database/model/class-attachment.php index d92ac3bec..5edf4be81 100644 --- a/src/mantle/database/model/class-attachment.php +++ b/src/mantle/database/model/class-attachment.php @@ -7,11 +7,12 @@ namespace Mantle\Database\Model; -use Mantle\Contracts; use Mantle\Facade\Storage; /** * Attachment Model + * + * @method static \Mantle\Database\Factory\Post_Factory factory( array|callable|null $state = null ) */ class Attachment extends Post { /** diff --git a/src/mantle/database/model/class-comment.php b/src/mantle/database/model/class-comment.php index 7df3c9921..60fe3c34a 100644 --- a/src/mantle/database/model/class-comment.php +++ b/src/mantle/database/model/class-comment.php @@ -12,6 +12,8 @@ /** * Comment Model + * + * @method static \Mantle\Database\Factory\Post_Factory factory( array|callable|null $state = null ) */ class Comment extends Model implements Contracts\Database\Core_Object, Contracts\Database\Model_Meta, Contracts\Database\Updatable { use Meta\Model_Meta, diff --git a/src/mantle/database/model/class-model.php b/src/mantle/database/model/class-model.php index b07ebccb3..470b714c6 100644 --- a/src/mantle/database/model/class-model.php +++ b/src/mantle/database/model/class-model.php @@ -25,6 +25,8 @@ /** * Database Model * + * @template TModelObject of object + * * @method static \Mantle\Support\Collection all() * @method static static first() * @method static static first_or_fail() @@ -41,6 +43,7 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab Concerns\Has_Aliases, Concerns\Has_Attributes, Concerns\Has_Events, + /** @use Concerns\Has_Factory */ Concerns\Has_Factory, Concerns\Has_Global_Scopes, Concerns\Has_Relationships; diff --git a/src/mantle/database/model/class-post.php b/src/mantle/database/model/class-post.php index fc3c70217..f6d72c87b 100644 --- a/src/mantle/database/model/class-post.php +++ b/src/mantle/database/model/class-post.php @@ -17,6 +17,8 @@ /** * Post Model * + * @extends Model<\WP_Post> + * * @property int $comment_count * @property int $ID * @property int $menu_order @@ -49,6 +51,7 @@ * @property string $status Alias to post_status. * @property string $title Alias to post_title. * + * @method static \Mantle\Database\Factory\Post_Factory factory( array|callable|null $state = null ) * @method static \Mantle\Database\Query\Post_Query_Builder anyStatus() * @method static \Mantle\Database\Query\Post_Query_Builder where( string|array $attribute, mixed $value ) * @method static \Mantle\Database\Query\Post_Query_Builder whereId( int $id ) diff --git a/src/mantle/database/model/class-site.php b/src/mantle/database/model/class-site.php index 0d328cfe2..bd75aa39c 100644 --- a/src/mantle/database/model/class-site.php +++ b/src/mantle/database/model/class-site.php @@ -12,6 +12,8 @@ /** * Site Model + * + * @method static \Mantle\Database\Factory\Post_Factory factory( array|callable|null $state = null ) */ class Site extends Model implements Contracts\Database\Core_Object, Contracts\Database\Updatable { /** diff --git a/src/mantle/database/model/class-term.php b/src/mantle/database/model/class-term.php index 94aa3eef9..19fa27710 100644 --- a/src/mantle/database/model/class-term.php +++ b/src/mantle/database/model/class-term.php @@ -22,6 +22,7 @@ * @property string $slug * @property string $taxonomy * + * @method static \Mantle\Database\Factory\Term_Factory factory( array|callable|null $state = null ) * @method static \Mantle\Database\Query\Term_Query_Builder whereId( int $id ) * @method static \Mantle\Database\Query\Term_Query_Builder whereName(string $name) * @method static \Mantle\Database\Query\Term_Query_Builder whereSlug(string $slug) diff --git a/src/mantle/database/model/class-user.php b/src/mantle/database/model/class-user.php index aceb6be7f..8a429921d 100644 --- a/src/mantle/database/model/class-user.php +++ b/src/mantle/database/model/class-user.php @@ -12,6 +12,8 @@ /** * User Model + * + * @method static \Mantle\Database\Factory\User_Factory factory( array|callable|null $state = null ) */ class User extends Model implements Contracts\Database\Core_Object, Contracts\Database\Model_Meta, Contracts\Database\Updatable { use Meta\Model_Meta, diff --git a/src/mantle/database/model/concerns/trait-has-factory.php b/src/mantle/database/model/concerns/trait-has-factory.php index 04147b463..1ba2a18b1 100644 --- a/src/mantle/database/model/concerns/trait-has-factory.php +++ b/src/mantle/database/model/concerns/trait-has-factory.php @@ -12,14 +12,16 @@ use Mantle\Database\Factory\Term_Factory; /** - * Model Database Factory + * Trait to add a factory to a model. + * + * @template TObject */ trait Has_Factory { /** * Create a builder for the model. * * @param array|callable $state Default state array or callable that will be invoked to set state. - * @return \Mantle\Database\Factory\Factory + * @return \Mantle\Database\Factory\Factory */ public static function factory( array|callable|null $state = null ): Factory { $factory = static::new_factory() ?: Factory::factory_for_model( static::class ); @@ -43,7 +45,7 @@ public static function factory( array|callable|null $state = null ): Factory { * * Optional: allows for the model factory to be overridden by application code. * - * @return \Mantle\Database\Factory\Factory|null + * @return \Mantle\Database\Factory\Factory|null */ protected static function new_factory(): ?Factory { return null; diff --git a/src/mantle/faker/class-faker-provider.php b/src/mantle/faker/class-faker-provider.php index 37bd97240..bf1f82d36 100644 --- a/src/mantle/faker/class-faker-provider.php +++ b/src/mantle/faker/class-faker-provider.php @@ -51,22 +51,12 @@ public function paragraph_blocks( int $count = 3, bool $as_text = true ) { * @param array $attributes Attributes for the block. * @return string */ - public static function block( string $block_name, string $content = '', array $attributes = [] ) { - $attributes = ! empty( $attributes ) ? \wp_json_encode( $attributes ) . ' ' : ''; - - if ( empty( $content ) ) { - return sprintf( - '', - $block_name, - $attributes - ); + public static function block( string $block_name, string $content = '', array $attributes = [] ): string { + // Add a newline before and after the content. + if ( ! empty( $content ) ) { + $content = "\n{$content}\n"; } - return sprintf( - '%3$s', - $block_name, - $attributes, - PHP_EOL . $content . PHP_EOL - ); + return get_comment_delimited_block_content( $block_name, $attributes, $content ); } } diff --git a/src/mantle/testing/wordpress-bootstrap.php b/src/mantle/testing/wordpress-bootstrap.php index d46f894d2..34932796f 100644 --- a/src/mantle/testing/wordpress-bootstrap.php +++ b/src/mantle/testing/wordpress-bootstrap.php @@ -11,6 +11,8 @@ use function Mantle\Testing\tests_add_filter; +defined( 'MANTLE_IS_TESTING' ) || define( 'MANTLE_IS_TESTING', true ); + require_once __DIR__ . '/class-utils.php'; require_once __DIR__ . '/class-wp-die.php'; @@ -174,6 +176,12 @@ // Use the Spy REST Server instead of default. tests_add_filter( 'wp_rest_server_class', [ Utils::class, 'wp_rest_server_class_filter' ], PHP_INT_MAX ); +// Prevent updating translations asynchronously. +tests_add_filter( 'async_update_translation', '__return_false' ); + +// Disable background updates. +tests_add_filter( 'automatic_updater_disabled', '__return_true' ); + // Load WordPress. require_once ABSPATH . '/wp-settings.php'; diff --git a/tests/database/factory/test-factory.php b/tests/database/factory/test-factory.php index 4d983b6bd..67cd6992d 100644 --- a/tests/database/factory/test-factory.php +++ b/tests/database/factory/test-factory.php @@ -6,11 +6,13 @@ use Mantle\Database\Model; use Mantle\Testing\Framework_Test_Case; +/** + * @group factory + */ class Test_Factory extends Framework_Test_Case { public function test_create_basic_model() { $factory = Testable_Post::factory(); - $this->assertInstanceOf( Factory\Factory::class, $factory ); $this->assertInstanceOf( Factory\Post_Factory::class, $factory ); $post = $factory->create_and_get(); @@ -154,7 +156,7 @@ class Testable_Post extends Model\Post { } /** - * @method static Testable_Post_Factory factory() + * @method static Testable_Post_Factory factory() */ class Testable_Post_With_Factory extends Model\Post { public static $object_name = 'post'; diff --git a/tests/database/factory/test-unit-testing-factory.php b/tests/database/factory/test-unit-testing-factory.php index 7c0c3c3ac..54cf3b810 100644 --- a/tests/database/factory/test-unit-testing-factory.php +++ b/tests/database/factory/test-unit-testing-factory.php @@ -14,6 +14,8 @@ * Test case with the focus of testing the unit testing factory that mirrors * WordPress core's factories. The factories here should be drop-in replacements * for core's factories with some sugar on top. + * + * @group factory */ class Test_Unit_Testing_Factory extends Framework_Test_Case { use With_Faker; @@ -21,7 +23,7 @@ class Test_Unit_Testing_Factory extends Framework_Test_Case { public function test_post_factory() { $this->assertInstanceOf( \WP_Post::class, static::factory()->post->create_and_get() ); - $posts = static::factory()->post->create_many( + $post_ids = static::factory()->post->create_many( 10, [ 'post_type' => 'post', @@ -29,12 +31,12 @@ public function test_post_factory() { ] ); - $this->assertCount( 10, $posts ); - foreach ( $posts as $post_id ) { + $this->assertCount( 10, $post_ids ); + foreach ( $post_ids as $post_id ) { $this->assertIsInt( $post_id ); } - $this->assertEquals( 'draft', get_post_status( array_shift( $posts ) ) ); + $this->assertEquals( 'draft', get_post_status( array_shift( $post_ids ) ) ); } public function test_post_create_with_thumbnail() { @@ -93,6 +95,7 @@ public function test_attachment_factory() { $this->shim_test( \WP_Post::class, 'attachment' ); $attachment = static::factory()->attachment->create_and_get(); + $this->assertEquals( 'attachment', get_post_type( $attachment ) ); } @@ -104,7 +107,6 @@ public function test_term_factory() { public function test_blog_factory() { if ( ! is_multisite() ) { $this->markTestSkipped( 'This test requires multisite.' ); - return; } $this->shim_test( \WP_Site::class, 'blog' ); @@ -113,7 +115,6 @@ public function test_blog_factory() { public function test_network_factory() { if ( ! is_multisite() ) { $this->markTestSkipped( 'This test requires multisite.' ); - return; } $this->shim_test( \WP_Network::class, 'network' ); @@ -148,7 +149,7 @@ public function test_comment_factory() { public function test_as_models() { $post = static::factory()->post->as_models()->create_and_get(); - $term = static::factory()->term->as_models()->with_model( Testable_Post_Tag::class )->create_and_get(); + $term = static::factory()->term->with_model( Testable_Post_Tag::class )->as_models()->create_and_get(); $this->assertInstanceOf( Post::class, $post ); $this->assertInstanceOf( Testable_Post_Tag::class, $term ); @@ -271,6 +272,23 @@ protected function shim_test( string $class_name, string $property ) { $this->assertCount( 10, $object_ids ); } + + /** + * @dataProvider dataprovider_factory + */ + public function test_dataprovider_factory( $post ) { + $this->assertInstanceOf( \WP_Post::class, $post ); + $this->assertStringContainsString( + '