| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class CRM_Utils_AutoCleanTest |
| 5 | * @group headless |
| 6 | */ |
| 7 | class CRM_Utils_AutoCleanTest extends CiviUnitTestCase { |
| 8 | |
| 9 | public $foo; |
| 10 | |
| 11 | protected function setUp() { |
| 12 | $this->useTransaction(); |
| 13 | parent::setUp(); |
| 14 | } |
| 15 | |
| 16 | public function testAutoclean() { |
| 17 | $this->foo = 'orig'; |
| 18 | $this->assertEquals('orig', $this->foo); |
| 19 | $this->nestedWithArrayCb(); |
| 20 | $this->assertEquals('orig', $this->foo); |
| 21 | $this->nestedWithFuncCb(); |
| 22 | $this->assertEquals('orig', $this->foo); |
| 23 | $this->nestedSwap(); |
| 24 | $this->assertEquals('orig', $this->foo); |
| 25 | } |
| 26 | |
| 27 | public function nestedWithArrayCb() { |
| 28 | $this->foo = 'arraycb'; |
| 29 | $ac = CRM_Utils_AutoClean::with([$this, 'setFoo'], 'orig'); |
| 30 | $this->assertEquals('arraycb', $this->foo); |
| 31 | } |
| 32 | |
| 33 | public function nestedWithFuncCb() { |
| 34 | $this->foo = 'funccb'; |
| 35 | |
| 36 | $self = $this; /* php 5.3 */ |
| 37 | $ac = CRM_Utils_AutoClean::with(function () use ($self /* php 5.3 */) { |
| 38 | $self->foo = 'orig'; |
| 39 | }); |
| 40 | |
| 41 | $this->assertEquals('funccb', $this->foo); |
| 42 | } |
| 43 | |
| 44 | public function nestedSwap() { |
| 45 | $ac = CRM_Utils_AutoClean::swap([$this, 'getFoo'], [$this, 'setFoo'], 'tmp'); |
| 46 | $this->assertEquals('tmp', $this->foo); |
| 47 | } |
| 48 | |
| 49 | public function getFoo() { |
| 50 | return $this->foo; |
| 51 | } |
| 52 | |
| 53 | public function setFoo($value) { |
| 54 | $this->foo = $value; |
| 55 | } |
| 56 | |
| 57 | } |