Skip to content

Commit

Permalink
New tests, TagArrayToStringTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Grafikart committed Mar 13, 2017
1 parent ed7ce1a commit a8de079
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public function reverseTransform($string)
return [];
}

$names = explode(',', $string);
$names = array_unique(array_map(function($name) {
return trim($name);
}, explode(',', $string)));


// Get the current tags and find the new ones that should be created.
$tags = $this->manager->getRepository(Tag::class)->findBy([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Tests\AppBundle\Form\DataTransformer;

use AppBundle\Entity\Tag;
use AppBundle\Form\DataTransformer\TagArrayToStringTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityRepository;

class TagArrayToStringTransformerTest extends \PHPUnit\Framework\TestCase
{

/**
* Get a mocked instance of the TagArrayToStringTransformer
* @return TagArrayToStringTransformer
*/
public function getMockedTransformer($findByReturn = [])
{
$tagRepository = $this->getMockBuilder(EntityRepository::class)
->disableOriginalConstructor()
->getMock();
$tagRepository->expects($this->any())
->method('findBy')
->will($this->returnValue($findByReturn));

$entityManager = $this
->getMockBuilder(ObjectManager::class)
->disableOriginalConstructor()
->getMock();
$entityManager->expects($this->any())
->method('getRepository')
->will($this->returnValue($tagRepository));

return new TagArrayToStringTransformer($entityManager);
}

/**
* Creates a new TagEntity instance
* @param $name
* @return Tag
*/
public function createTag($name)
{
$tag = new Tag();
$tag->setName($name);
return $tag;
}

/**
* Ensures tags are created correctly
*/
public function testCreateTheRightAmountOfTags()
{
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Demo, How');
$this->assertCount(3, $tags);
$this->assertEquals('Hello', $tags[0]->getName());
}

/**
* Spaces at the end (and begining) of a world shouldn't matter
*/
public function testTrimNames()
{
$tags = $this->getMockedTransformer()->reverseTransform(' Hello ');
$this->assertEquals('Hello', $tags[0]->getName());
}

/**
* Duplicate tags shouldn't create new entities
*/
public function testDuplicateNames()
{
$tags = $this->getMockedTransformer()->reverseTransform('Hello, Hello, Hello');
$this->assertCount(1, $tags);
}

/**
* This test ensure that the transformer uses tag already persisted in the Database
*/
public function testUsesAlreadyDefinedTags()
{
$persisted_tags = [
$this->createTag('Hello'),
$this->createTag('World')
];
$tags = $this->getMockedTransformer($persisted_tags)->reverseTransform('Hello, World, How, Are, You');
$this->assertCount(5, $tags);
$this->assertEquals($persisted_tags[0], $tags[0]);
$this->assertEquals($persisted_tags[1], $tags[1]);
}

/**
* Tags should be transformed into a string
*/
public function testTransform () {
$persisted_tags = [
$this->createTag('Hello'),
$this->createTag('World')
];
$transformed = $this->getMockedTransformer()->transform($persisted_tags);
$this->assertEquals('Hello,World', $transformed);
}

}

0 comments on commit a8de079

Please sign in to comment.