Autoformat /tests directory with php short array syntax
[civicrm-core.git] / tests / phpunit / CRM / Extension / Container / CollectionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Extension_Container_CollectionTest
30 * @group headless
31 */
32 class CRM_Extension_Container_CollectionTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 }
37
38 public function tearDown() {
39 parent::tearDown();
40 }
41
42 public function testGetKeysEmpty() {
43 $c = new CRM_Extension_Container_Collection([]);
44 $this->assertEquals($c->getKeys(), []);
45 }
46
47 public function testGetKeys() {
48 $c = $this->_createContainer();
49 $this->assertEquals([
50 'test.conflict',
51 'test.whiz',
52 'test.whizbang',
53 'test.foo',
54 'test.foo.bar',
55 ], $c->getKeys());
56 }
57
58 public function testGetPath() {
59 $c = $this->_createContainer();
60 try {
61 $c->getPath('un.kno.wn');
62 }
63 catch (CRM_Extension_Exception $e) {
64 $exc = $e;
65 }
66 $this->assertTrue(is_object($exc), 'Expected exception');
67
68 $this->assertEquals("/path/to/foo", $c->getPath('test.foo'));
69 $this->assertEquals("/path/to/bar", $c->getPath('test.foo.bar'));
70 $this->assertEquals("/path/to/whiz", $c->getPath('test.whiz'));
71 $this->assertEquals("/path/to/whizbang", $c->getPath('test.whizbang'));
72 $this->assertEquals("/path/to/conflict-b", $c->getPath('test.conflict'));
73 }
74
75 public function testGetResUrl() {
76 $c = $this->_createContainer();
77 try {
78 $c->getResUrl('un.kno.wn');
79 }
80 catch (CRM_Extension_Exception $e) {
81 $exc = $e;
82 }
83 $this->assertTrue(is_object($exc), 'Expected exception');
84
85 $this->assertEquals('http://foo', $c->getResUrl('test.foo'));
86 $this->assertEquals('http://foobar', $c->getResUrl('test.foo.bar'));
87 $this->assertEquals('http://whiz', $c->getResUrl('test.whiz'));
88 $this->assertEquals('http://whizbang', $c->getResUrl('test.whizbang'));
89 $this->assertEquals('http://conflict-b', $c->getResUrl('test.conflict'));
90 }
91
92 public function testCaching() {
93 $cache = new CRM_Utils_Cache_Arraycache([]);
94 $this->assertTrue(!is_array($cache->get('ext-collection')));
95 $c = $this->_createContainer($cache, 'ext-collection');
96 $this->assertEquals('http://foo', $c->getResUrl('test.foo'));
97 $this->assertTrue(is_array($cache->get('ext-collection')));
98
99 $cacheData = $cache->get('ext-collection');
100 // 'test.foo' was defined in the 'a' container
101 $this->assertEquals('a', $cacheData['test.foo']);
102 // 'test.whiz' was defined in the 'b' container
103 $this->assertEquals('b', $cacheData['test.whiz']);
104 }
105
106 /**
107 * @param CRM_Utils_Cache_Interface $cache
108 * @param null $cacheKey
109 *
110 * @return CRM_Extension_Container_Collection
111 */
112 public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
113 $containers = [];
114 $containers['a'] = new CRM_Extension_Container_Static([
115 'test.foo' => [
116 'path' => '/path/to/foo',
117 'resUrl' => 'http://foo',
118 ],
119 'test.foo.bar' => [
120 'path' => '/path/to/bar',
121 'resUrl' => 'http://foobar',
122 ],
123 ]);
124 $containers['b'] = new CRM_Extension_Container_Static([
125 'test.whiz' => [
126 'path' => '/path/to/whiz',
127 'resUrl' => 'http://whiz',
128 ],
129 'test.whizbang' => [
130 'path' => '/path/to/whizbang',
131 'resUrl' => 'http://whizbang',
132 ],
133 'test.conflict' => [
134 'path' => '/path/to/conflict-b',
135 'resUrl' => 'http://conflict-b',
136 ],
137 ]);
138 $containers['c'] = new CRM_Extension_Container_Static([
139 'test.conflict' => [
140 'path' => '/path/to/conflict-c',
141 'resUrl' => 'http://conflict-c',
142 ],
143 ]);
144 $c = new CRM_Extension_Container_Collection($containers, $cache, $cacheKey);
145 return $c;
146 }
147
148 }