<commit_before><commit_msg>CRM-157: address type transformer unit test<commit_after><?php

namespace Oro\Bundle\AddressBundle\Tests\Unit\Form\DataTransformer;

use Doctrine\Common\Persistence\ObjectManager;
use Oro\Bundle\AddressBundle\Entity\AddressType;
use Oro\Bundle\AddressBundle\Form\DataTransformer\AddressTypeToTypeTransformer;

class AddressTypeToTypeTransformerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var ObjectManager
     */
    protected $om;

    /**
     * @var AddressTypeToTypeTransformer
     */
    protected $transformer;

    protected function setUp()
    {
        $this->om = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
            ->disableOriginalConstructor()
            ->getMock();
        $this->transformer = new AddressTypeToTypeTransformer($this->om);
    }

    /**
     * @dataProvider typesDataProvider
     * @param null|AddressType $type
     * @param string $expected
     */
    public function testTransform($type, $expected)
    {
        $this->assertEquals($expected, $this->transformer->transform($type));
    }

    /**
     * @return array
     */
    public function typesDataProvider()
    {
        return array(
            array(null, ''),
            array($this->getAddressTypeMock('test'), 'test')
        );
    }

    public function testReverseTransformEmpty()
    {
        $this->assertNull($this->transformer->reverseTransform(false));
    }

    /**
     * @expectedException Symfony\Component\Form\Exception\TransformationFailedException
     * @expectedExceptionMessage An address type with type "unknown" does not exist!
     */
    public function testReverseTransformException()
    {
        $type = 'unknown';
        $this->assertRepositoryCall($type, null);
        $this->transformer->reverseTransform($type);
    }

    public function testReverseTransform()
    {
        $type = 'test';
        $addressType = $addressType = $this->getMockBuilder('Oro\Bundle\AddressBundle\Entity\AddressType')
            ->disableOriginalConstructor()
            ->getMock();
        $this->assertRepositoryCall($type, $addressType);
        $this->assertSame($addressType, $this->transformer->reverseTransform($type));
    }

    protected function assertRepositoryCall($type, $addressType)
    {
        $repository = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectRepository')
            ->disableOriginalConstructor()
            ->getMock();
        $repository->expects($this->once())
            ->method('findOneBy')
            ->with(array('type' => $type))
            ->will($this->returnValue($addressType));

        $this->om->expects($this->once())
            ->method('getRepository')
            ->with('OroAddressBundle:AddressType')
            ->will($this->returnValue($repository));
    }

    protected function getAddressTypeMock($type)
    {
        $addressType = $this->getMockBuilder('Oro\Bundle\AddressBundle\Entity\AddressType')
            ->disableOriginalConstructor()
            ->getMock();
        $addressType->expects($this->once())
            ->method('getType')
            ->will($this->returnValue($type));
        return $addressType;
    }
}
{'fraction_non_alphanumeric': 0.1357254290171607, 'fraction_numerical': 0.00093603744149766, 'mean_word_length': 2.816666666666667, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '539358', 'n_tokens_mistral': 856, 'n_tokens_neox': 842, 'n_words': 127}