public function setTags(Article $entity, array $tagIds) { foreach ($tagIds as $tagId) { $tag = $this->getEntityManager()->find(Tag::class, $tagId); if ($tag instanceof Tag) { $entity->addTag($tag); } } }
/** * @test * @dataProvider provideTagsCount * * @param $tagCount */ public function tag_workflow($tagCount) { $article = new Article(); for ($i = 0; $i < $tagCount; ++$i) { $tag = new Tag(uniqid('testTag_')); $this->assertFalse($article->hasTag($tag)); $article->addTag($tag); $this->assertTrue($article->hasTag($tag)); } $this->assertContainsOnlyInstancesOf(Tag::class, $article->getTags()); $this->assertCount($tagCount, $article->getTags()); if ($tagCount > 0) { $this->assertTrue($article->removeTag($tag)); $this->assertNotTrue($article->hasTag($tag)); $this->assertCount($tagCount - 1, $article->getTags()); } $anotherTag = new Tag('anotherTag'); $this->assertFalse($article->removeTag($anotherTag)); }