Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow testing framework factory to return models #276

Merged
merged 4 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/mantle/testing/factory/class-attachment-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public function create_upload_object( $file, $parent = 0 ) {
* @return \WP_Post|null
*/
public function get_object_by_id( int $object_id ): ?\WP_Post {
return get_post_object( $object_id );
return $this->as_models
? Post::find( $object_id )
: get_post_object( $object_id );
}
}
36 changes: 35 additions & 1 deletion src/mantle/testing/factory/class-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@

namespace Mantle\Testing\Factory;

use Mantle\Contracts\Database\Core_Object;

use function Mantle\Support\Helpers\collect;
use function Mantle\Support\Helpers\tap;

/**
* Base Factory
*/
abstract class Factory {
/**
* Flag to return the factory as a model.
*
* @var bool
*/
protected bool $as_models = false;

/**
* Creates an object.
*
Expand All @@ -29,6 +39,30 @@ abstract public function create( array $args = [] );
*/
abstract public function get_object_by_id( int $object_id );

/**
* Generate models from the factory.
*
* @return static
*/
public function as_models() {
return tap(
clone $this,
fn ( $factory ) => $factory->as_models = true,
);
}

/**
* Generate core WordPress objects from the factory.
*
* @return static
*/
public function as_objects() {
return tap(
clone $this,
fn ( $factory ) => $factory->as_models = false,
);
}

/**
* Creates multiple objects.
*
Expand All @@ -52,7 +86,7 @@ function() use ( $args ) {
* Creates an object and returns its object.
*
* @param array $args Optional. The arguments for the object to create. Default is empty array.
* @return mixed The created object.
* @return mixed|Core_Object The created object.
*/
public function create_and_get( $args = [] ) {
$object_id = $this->create( $args );
Expand Down
4 changes: 3 additions & 1 deletion src/mantle/testing/factory/class-post-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public function create_ordered_set(
* @return \WP_Post|null
*/
public function get_object_by_id( int $object_id ) {
return get_post_object( $object_id );
return $this->as_models
? Post::find( $object_id )
: get_post_object( $object_id );
}
}
8 changes: 7 additions & 1 deletion src/mantle/testing/factory/class-term-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public function create( array $args = [] ) {
* @return \WP_Term|null
*/
public function get_object_by_id( int $object_id ) {
return get_term_object( $object_id );
$term = get_term_object( $object_id );

if ( $term && $this->as_models ) {
return Term::new_from_existing( (array) $term );
}

return $term;
}
}
10 changes: 10 additions & 0 deletions tests/testing/test-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace Mantle\Testing;

use Carbon\Carbon;
use Mantle\Database\Model\Post;
use Mantle\Database\Model\Term;

use function Mantle\Support\Helpers\collect;

Expand Down Expand Up @@ -99,6 +101,14 @@ public function test_comment_factory() {
$this->shim_test( \WP_Comment::class, 'comment' );
}

public function test_as_models() {
$post = static::factory()->post->as_models()->create_and_get();
$term = static::factory()->term->as_models()->create_and_get();

$this->assertInstanceOf( Post::class, $post );
$this->assertInstanceOf( Term::class, $term );
}

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

Expand Down