123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace fphammerle\helpers\tests;
- class TestClass
- {
- use \fphammerle\helpers\PropertyAccessTrait;
- private $_a;
- public function __construct($a)
- {
- $this->_a = $a;
- }
- public function getA()
- {
- return $this->_a;
- }
- public function setA($value)
- {
- $this->_a = $value;
- }
- public function getSquare()
- {
- return $this->_a * $this->_a;
- }
- public function setCubic($c)
- {
- $this->_a = pow($c, 1/3);
- }
- }
- class PropertyAccessTraitTest extends \PHPUnit_Framework_TestCase
- {
- public function testGetPublic()
- {
- $o = new TestClass(2);
- $this->assertEquals(2, $o->a);
- }
- public function testGetPublic2()
- {
- $o = new TestClass(3);
- $this->assertEquals(9, $o->square);
- }
- /**
- * @expectedException \Exception
- */
- public function testGetUnknown()
- {
- $o = new TestClass(2);
- $o->cubic;
- }
- public function testSetPublic()
- {
- $o = new TestClass(1);
- $o->a = 2;
- $this->assertEquals(2, $o->getA());
- }
- public function testSetPublic2()
- {
- $o = new TestClass(3);
- $o->cubic = 8;
- $this->assertEquals(2, $o->getA(), '', 0.1);
- }
- /**
- * @expectedException \Exception
- */
- public function testSetUnknown()
- {
- $o = new TestClass(1);
- $o->square = 4;
- }
- public function testIncrement()
- {
- $o = new TestClass(1);
- $o->a++;
- $this->assertEquals(2, $o->getA());
- }
- public function testAdd()
- {
- $o = new TestClass(1);
- $o->a += 3;
- $this->assertEquals(4, $o->getA());
- }
- public function testIssetTrue()
- {
- $o = new TestClass(2);
- $this->assertTrue(isset($o->a));
- }
- public function testIssetEmpty()
- {
- $o = new TestClass('');
- $this->assertEquals('', $o->a);
- $this->assertTrue(isset($o->a));
- }
- public function testIssetNull()
- {
- $o = new TestClass(null);
- $this->assertFalse(isset($o->a));
- }
- public function testIssetUndefined()
- {
- $o = new TestClass(null);
- $this->assertFalse(isset($o->b));
- }
- }
|