PropertyAccessTraitTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. class TestClass
  4. {
  5. use \fphammerle\helpers\PropertyAccessTrait;
  6. private $_a;
  7. public function __construct($a)
  8. {
  9. $this->_a = $a;
  10. }
  11. public function getA()
  12. {
  13. return $this->_a;
  14. }
  15. public function setA($value)
  16. {
  17. $this->_a = $value;
  18. }
  19. public function getSquare()
  20. {
  21. return $this->_a * $this->_a;
  22. }
  23. public function setCubic($c)
  24. {
  25. $this->_a = pow($c, 1/3);
  26. }
  27. }
  28. class PropertyAccessTraitTest extends \PHPUnit_Framework_TestCase
  29. {
  30. public function testGetPublic()
  31. {
  32. $o = new TestClass(2);
  33. $this->assertEquals(2, $o->a);
  34. }
  35. public function testGetPublic2()
  36. {
  37. $o = new TestClass(3);
  38. $this->assertEquals(9, $o->square);
  39. }
  40. /**
  41. * @expectedException \Exception
  42. */
  43. public function testGetUnknown()
  44. {
  45. $o = new TestClass(2);
  46. $o->cubic;
  47. }
  48. public function testSetPublic()
  49. {
  50. $o = new TestClass(1);
  51. $o->a = 2;
  52. $this->assertEquals(2, $o->getA());
  53. }
  54. public function testSetPublic2()
  55. {
  56. $o = new TestClass(3);
  57. $o->cubic = 8;
  58. $this->assertEquals(2, $o->getA(), '', 0.1);
  59. }
  60. /**
  61. * @expectedException \Exception
  62. */
  63. public function testSetUnknown()
  64. {
  65. $o = new TestClass(1);
  66. $o->square = 4;
  67. }
  68. public function testIncrement()
  69. {
  70. $o = new TestClass(1);
  71. $o->a++;
  72. $this->assertEquals(2, $o->getA());
  73. }
  74. public function testAdd()
  75. {
  76. $o = new TestClass(1);
  77. $o->a += 3;
  78. $this->assertEquals(4, $o->getA());
  79. }
  80. public function testIssetTrue()
  81. {
  82. $o = new TestClass(2);
  83. $this->assertTrue(isset($o->a));
  84. }
  85. public function testIssetEmpty()
  86. {
  87. $o = new TestClass('');
  88. $this->assertEquals('', $o->a);
  89. $this->assertTrue(isset($o->a));
  90. }
  91. public function testIssetNull()
  92. {
  93. $o = new TestClass(null);
  94. $this->assertFalse(isset($o->a));
  95. }
  96. public function testIssetUndefined()
  97. {
  98. $o = new TestClass(null);
  99. $this->assertFalse(isset($o->b));
  100. }
  101. }