NameFinderTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreStart
  7. namespace Magento\Framework\Reflection\Test\Unit;
  8. use Zend\Code\Reflection\ClassReflection;
  9. /**
  10. * NameFinder Unit Test
  11. */
  12. class NameFinderTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Framework\Reflection\NameFinder */
  15. protected $nameFinder;
  16. /**
  17. * Set up helper.
  18. */
  19. protected function setUp()
  20. {
  21. $this->nameFinder = new \Magento\Framework\Reflection\NameFinder();
  22. }
  23. public function testGetSetterMethodName()
  24. {
  25. $class = new ClassReflection(\Magento\Framework\Reflection\Test\Unit\DataObject::class);
  26. $setterName = $this->nameFinder->getSetterMethodName($class, 'AttrName');
  27. $this->assertEquals("setAttrName", $setterName);
  28. $booleanSetterName = $this->nameFinder->getSetterMethodName($class, 'Active');
  29. $this->assertEquals("setIsActive", $booleanSetterName);
  30. }
  31. /**
  32. * @expectedException \Exception
  33. * @codingStandardsIgnoreStart
  34. * @expectedExceptionMessage Property "InvalidAttribute" does not have accessor method "setInvalidAttribute" in class "Magento\Framework\Reflection\Test\Unit\DataObject"
  35. * @codingStandardsIgnoreEnd
  36. */
  37. public function testGetSetterMethodNameInvalidAttribute()
  38. {
  39. $class = new ClassReflection(\Magento\Framework\Reflection\Test\Unit\DataObject::class);
  40. $this->nameFinder->getSetterMethodName($class, 'InvalidAttribute');
  41. }
  42. /**
  43. * @expectedException \Exception
  44. * @codingStandardsIgnoreStart
  45. * @expectedExceptionMessage Property "ActivE" does not have accessor method "setActivE" in class "Magento\Framework\Reflection\Test\Unit\DataObject"
  46. * @codingStandardsIgnoreEnd
  47. */
  48. public function testGetSetterMethodNameWrongCamelCasedAttribute()
  49. {
  50. $class = new ClassReflection(\Magento\Framework\Reflection\Test\Unit\DataObject::class);
  51. $this->nameFinder->getSetterMethodName($class, 'ActivE');
  52. }
  53. /**
  54. * @expectedException \LogicException
  55. * @expectedExceptionMessage Property "Property" does not have accessor method "getProperty" in class "className".
  56. */
  57. public function testFindAccessorMethodName()
  58. {
  59. $reflectionClass = $this->createMock(\Zend\Code\Reflection\ClassReflection::class);
  60. $reflectionClass->expects($this->atLeastOnce())->method('hasMethod')->willReturn(false);
  61. $reflectionClass->expects($this->atLeastOnce())->method('getName')->willReturn('className');
  62. $this->nameFinder->findAccessorMethodName(
  63. $reflectionClass,
  64. 'Property',
  65. 'getProperty',
  66. 'isProperty'
  67. );
  68. }
  69. }