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