Skip to content

Commit

Permalink
Adding dynamic factory creation (#584)
Browse files Browse the repository at this point in the history
* Adding dynamic factory creation

* Add macroable trait

* phpcs fix
  • Loading branch information
srtfisher authored Sep 17, 2024
1 parent f9d052c commit 6c68a83
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow the block factory to override text when generating blocks.
- Added new `defer()` helper.
- Added `Cache::flexible()` method to add SWR support to the cache.
- Added dynamic creation of post type/taxonomy factories.

### Changed

Expand Down
29 changes: 29 additions & 0 deletions src/mantle/database/factory/class-factory-container.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
namespace Mantle\Database\Factory;

use Faker\Generator;
use InvalidArgumentException;
use Mantle\Contracts\Container;
use Mantle\Faker\Faker_Provider;
use Mantle\Support\Traits\Macroable;

/**
* Collect all the Database Factories for IDE Support
Expand All @@ -20,6 +22,8 @@
* factories.
*/
class Factory_Container {
use Macroable;

/**
* Attachment Factory
*
Expand Down Expand Up @@ -135,4 +139,29 @@ function () {
},
);
}

/**
* Magic method to retrieve a custom post type/taxonomy factory.
*
* @throws InvalidArgumentException If the post type or taxonomy does not exist.
* @throws InvalidArgumentException If both post type and taxonomy exist.
*
* @param string $name Factory name.
* @return Post_Factory|Term_Factory Factory instance.
*/
public function __get( string $name ) {
if ( post_type_exists( $name ) && taxonomy_exists( $name ) ) {
throw new InvalidArgumentException( "Error creating dynamic factory for {$name}. Both post type and taxonomy exist." );
}

if ( post_type_exists( $name ) ) {
return $this->post->with_post_type( $name );
}

if ( taxonomy_exists( $name ) ) {
return $this->term->with_taxonomy( $name );
}

throw new InvalidArgumentException( "Error creating dynamic factory for {$name}. Post type or taxonomy does not exist." );
}
}
46 changes: 46 additions & 0 deletions tests/Database/Factory/UnitTestingFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,52 @@ public static function dataprovider_factory(): array {
'example' => [ static::factory()->post->create_and_get() ],
];
}

public function test_custom_post_type() {
register_post_type( 'custom-post' );

$post = static::factory()->post->for( 'custom-post' )->create_and_get();

$this->assertEquals( 'custom-post', get_post_type( $post ) );
}

public function test_custom_taxonomy() {
register_taxonomy( 'custom-taxonomy', 'post' );

$term = static::factory()->term->for( 'custom-taxonomy' )->create_and_get();

$this->assertEquals( 'custom-taxonomy', get_taxonomy( $term->taxonomy )->name );
}

public function test_dynamic_factory() {
register_post_type( 'events' );
register_taxonomy( 'event-category', 'events' );

$event = static::factory()->events->create_and_get();

$this->assertInstanceOf( \WP_Post::class, $event );
$this->assertEquals( 'events', get_post_type( $event ) );

$term = static::factory()->{'event-category'}->create_and_get();

$this->assertInstanceOf( \WP_Term::class, $term );
$this->assertEquals( 'event-category', get_taxonomy( $term->taxonomy )->name );
}

public function test_dynamic_factory_unknown() {
$this->expectException( \InvalidArgumentException::class );

static::factory()->unknown->create_and_get();
}

public function test_dynamic_factory_conflict() {
register_post_type( 'conflict' );
register_taxonomy( 'conflict', 'conflict' );

$this->expectException( \InvalidArgumentException::class );

static::factory()->conflict->create_and_get();
}
}

class Testable_Post_Tag extends Term {
Expand Down

0 comments on commit 6c68a83

Please sign in to comment.