Skip to content

Commit

Permalink
Add support for an array of term slugs with the factory
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Oct 30, 2024
1 parent a0b17f9 commit f85a079
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/mantle/database/factory/class-post-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct( Generator $faker, public string $post_type = 'post'
*/
public function with_terms( ...$terms ): static {
// Handle an array in the first argument.
if ( 1 === count( $terms ) && is_array( $terms[0] ) ) {
if ( 1 === count( $terms ) && isset( $terms[0] ) && is_array( $terms[0] ) ) {
$terms = $terms[0];
}

Expand Down
8 changes: 7 additions & 1 deletion src/mantle/database/model/term/trait-model-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function set_terms( $terms, ?string $taxonomy = null, bool $append = fals
// If taxonomy is not specified, chunk the terms into taxonomy groups.
if ( ! $taxonomy ) {
$terms = $terms->reduce(
function ( array $carry, $term ): array {
function ( array $carry, $term, $index ): array {
if ( $term instanceof WP_Term ) {
$carry[ $term->taxonomy ][] = $term;

Expand Down Expand Up @@ -165,6 +165,12 @@ function ( array $carry, $term ): array {
continue;
}

// Use the parent array key as the taxonomy if the parent array
// key is a string and the current array index is not.
if ( ! is_string( $taxonomy ) && is_string( $index ) ) {
$taxonomy = $index;
}

// Attempt to infer if the key is a taxonomy slug and this is a
// taxonomy => term slug pair.
if ( ! is_string( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) {
Expand Down
90 changes: 80 additions & 10 deletions tests/Database/Factory/UnitTestingFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public function test_factory_middleware() {
$this->assertEquals( '_test_meta_value', get_post_meta( $post->ID, '_test_meta_key', true ) );
}

#[Group( 'with_terms' )]
public function test_posts_with_terms() {
$post = static::factory()->post->with_terms(
[
Expand All @@ -183,6 +184,7 @@ public function test_posts_with_terms() {
$this->assertTrue( has_term( $category->term_id, 'category', $post ) );
}

#[Group( 'with_terms' )]
public function test_posts_with_term_ids() {
$post = static::factory()->post->with_terms(
[
Expand All @@ -196,15 +198,7 @@ public function test_posts_with_term_ids() {
$this->assertTrue( has_term( $category->term_id, 'category', $post ) );
}

public function test_terms_with_posts() {
$post_ids = static::factory()->post->create_many( 2 );

$category = static::factory()->category->with_posts( $post_ids )->create_and_get();

$this->assertTrue( has_term( $category->term_id, 'category', $post_ids[0] ) );
$this->assertTrue( has_term( $category->term_id, 'category', $post_ids[1] ) );
}

#[Group( 'with_terms' )]
public function test_posts_with_terms_multiple_taxonomies() {
$post = static::factory()->post->with_terms(
$category = static::factory()->category->create_and_get(),
Expand All @@ -223,7 +217,8 @@ public function test_posts_with_terms_multiple_taxonomies() {
$this->assertTrue( has_term( $tag->term_id, 'post_tag', $post ) );
}

public function test_posts_with_terms_multiple_taxonomies_and_term_slug() {
#[Group( 'with_terms' )]
public function test_posts_with_multiple_taxonomies_with_mixed_objects() {
$tag = static::factory()->tag->create_and_get();

// Test with the arguments passed as individual parameters.
Expand All @@ -249,6 +244,74 @@ public function test_posts_with_terms_multiple_taxonomies_and_term_slug() {
$this->assertTrue( has_term( $tag->term_id, 'post_tag', $post ) );
}

#[Group( 'with_terms' )]
public function test_posts_with_multiple_terms_single_array_argument() {
$tags = collect( static::factory()->tag->create_many( 5 ) )
->map( fn ( $term_id ) => get_term( $term_id ) )
->pluck( 'term_id' )
->all();

$post = static::factory()->post->with_terms( $tags )->create_and_get();

$post_tags = get_the_terms( $post, 'post_tag' );

$this->assertCount( 5, $post_tags );
$this->assertEquals(
collect( $tags )->sort()->values()->all(),
collect( $post_tags )->pluck( 'term_id' )->sort()->values()->all(),
);
}

#[Group( 'with_terms' )]
public function test_posts_with_multiple_terms_spread_array_argument() {
$tags = collect( static::factory()->tag->create_many( 5 ) )
->map( fn ( $term_id ) => get_term( $term_id ) )
->pluck( 'term_id' )
->all();

$post = static::factory()->post->with_terms( ...$tags )->create_and_get();

$post_tags = get_the_terms( $post, 'post_tag' );

$this->assertCount( 5, $post_tags );
$this->assertEquals(
collect( $tags )->sort()->values()->all(),
collect( $post_tags )->pluck( 'term_id' )->sort()->values()->all(),
);
}

#[Group( 'with_terms' )]
#[DataProvider( 'slug_id_dataprovider' )]
public function test_posts_with_multiple_terms_single_array( string $field ) {
$tags = collect( static::factory()->tag->create_many( 5 ) )
->map( fn ( $term_id ) => get_term( $term_id ) )
->pluck( $field )
->all();

$post = static::factory()->post->with_terms( [ 'post_tag' => $tags ] )->create_and_get();
$post_tags = get_the_terms( $post, 'post_tag' );

$this->assertCount( 5, $post_tags );
$this->assertEquals(
collect( $tags )->sort()->values()->all(),
collect( $post_tags )->pluck( $field )->sort()->values()->all(),
);
}

#[Group( 'with_terms' )]
public function test_posts_with_terms_create_unknown_term() {
$this->markTestIncomplete( 'This test is incomplete.' );
}

public function test_terms_with_posts() {
$post_ids = static::factory()->post->create_many( 2 );

$category = static::factory()->category->with_posts( $post_ids )->create_and_get();

$this->assertTrue( has_term( $category->term_id, 'category', $post_ids[0] ) );
$this->assertTrue( has_term( $category->term_id, 'category', $post_ids[1] ) );
}

public function test_post_with_meta() {
$post = static::factory()->post->with_meta(
[
Expand Down Expand Up @@ -358,6 +421,13 @@ public function test_dynamic_factory_conflict() {

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

public static function slug_id_dataprovider(): array {
return [
'term_id' => [ 'term_id' ],
'slug' => [ 'slug' ],
];
}
}

class Testable_Post_Tag extends Term {
Expand Down

0 comments on commit f85a079

Please sign in to comment.