|
14 | 14 | use Williarin\WordpressInterop\Criteria\SelectColumns; |
15 | 15 | use Williarin\WordpressInterop\Test\TestCase; |
16 | 16 |
|
| 17 | +use function Williarin\WordpressInterop\Util\String\field_to_property; |
| 18 | + |
17 | 19 | class TermRepositoryTest extends TestCase |
18 | 20 | { |
19 | 21 | /** @var TermRepository */ |
@@ -99,6 +101,27 @@ public function testFindByPostRelationshipCondition(): void |
99 | 101 | ], array_combine(array_column($terms, 'taxonomy'), array_column($terms, 'name'))); |
100 | 102 | } |
101 | 103 |
|
| 104 | + public function testCreateNewTermForTaxonomy(): void |
| 105 | + { |
| 106 | + $term = $this->repository->createTermForTaxonomy('Jewelry', 'product_cat'); |
| 107 | + |
| 108 | + $this->validateTerm($term, [ |
| 109 | + 'name' => 'Jewelry', |
| 110 | + 'slug' => 'jewelry', |
| 111 | + 'taxonomy' => 'product_cat', |
| 112 | + 'count' => 0, |
| 113 | + ]); |
| 114 | + } |
| 115 | + |
| 116 | + public function testCreateTermForTaxonomyNoDuplicate(): void |
| 117 | + { |
| 118 | + $term1 = $this->repository->createTermForTaxonomy('Jewelry', 'product_cat'); |
| 119 | + $term2 = $this->repository->createTermForTaxonomy('Jewelry', 'product_cat'); |
| 120 | + |
| 121 | + self::assertEquals($term1, $term2); |
| 122 | + self::assertNotSame($term1, $term2); |
| 123 | + } |
| 124 | + |
102 | 125 | public function testAddTermsToEntity(): void |
103 | 126 | { |
104 | 127 | $hoodieTerms = $this->repository->findBy([ |
@@ -295,4 +318,14 @@ static function (Term $term) { |
295 | 318 |
|
296 | 319 | self::assertEquals(['simple', 'Hoodies', 'MegaBrand'], array_column($terms, 'name')); |
297 | 320 | } |
| 321 | + |
| 322 | + private function validateTerm(Term $term, array $values): void |
| 323 | + { |
| 324 | + foreach ($values as $key => $value) { |
| 325 | + self::assertEquals($value, $term->{field_to_property($key)}); |
| 326 | + } |
| 327 | + |
| 328 | + self::assertIsInt($term->termId); |
| 329 | + self::assertIsInt($term->termTaxonomyId); |
| 330 | + } |
298 | 331 | } |
0 commit comments