NameFinderTest.php 2.71 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
// @codingStandardsIgnoreStart
namespace Magento\Framework\Reflection\Test\Unit;

use Zend\Code\Reflection\ClassReflection;

/**
 * NameFinder Unit Test
 */
class NameFinderTest extends \PHPUnit\Framework\TestCase
{
    /** @var \Magento\Framework\Reflection\NameFinder */
    protected $nameFinder;

    /**
     * Set up helper.
     */
    protected function setUp()
    {
        $this->nameFinder = new \Magento\Framework\Reflection\NameFinder();
    }

    public function testGetSetterMethodName()
    {
        $class = new ClassReflection(\Magento\Framework\Reflection\Test\Unit\DataObject::class);
        $setterName = $this->nameFinder->getSetterMethodName($class, 'AttrName');
        $this->assertEquals("setAttrName", $setterName);

        $booleanSetterName = $this->nameFinder->getSetterMethodName($class, 'Active');
        $this->assertEquals("setIsActive", $booleanSetterName);
    }

    /**
     * @expectedException \Exception
     * @codingStandardsIgnoreStart
     * @expectedExceptionMessage Property "InvalidAttribute" does not have accessor method "setInvalidAttribute" in class "Magento\Framework\Reflection\Test\Unit\DataObject"
     * @codingStandardsIgnoreEnd
     */
    public function testGetSetterMethodNameInvalidAttribute()
    {
        $class = new ClassReflection(\Magento\Framework\Reflection\Test\Unit\DataObject::class);
        $this->nameFinder->getSetterMethodName($class, 'InvalidAttribute');
    }

    /**
     * @expectedException \Exception
     * @codingStandardsIgnoreStart
     * @expectedExceptionMessage Property "ActivE" does not have accessor method "setActivE" in class "Magento\Framework\Reflection\Test\Unit\DataObject"
     * @codingStandardsIgnoreEnd
     */
    public function testGetSetterMethodNameWrongCamelCasedAttribute()
    {
        $class = new ClassReflection(\Magento\Framework\Reflection\Test\Unit\DataObject::class);
        $this->nameFinder->getSetterMethodName($class, 'ActivE');
    }

    /**
     * @expectedException \LogicException
     * @expectedExceptionMessage Property "Property" does not have accessor method "getProperty" in class "className".
     */
    public function testFindAccessorMethodName()
    {
        $reflectionClass = $this->createMock(\Zend\Code\Reflection\ClassReflection::class);
        $reflectionClass->expects($this->atLeastOnce())->method('hasMethod')->willReturn(false);
        $reflectionClass->expects($this->atLeastOnce())->method('getName')->willReturn('className');

        $this->nameFinder->findAccessorMethodName(
            $reflectionClass,
            'Property',
            'getProperty',
            'isProperty'
        );
    }
}