PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/pimple/pimple/tests/Pimple/Tests/PimpleTest.php

https://bitbucket.org/laborautonomo/laborautonomo-site
PHP | 270 lines | 176 code | 47 blank | 47 comment | 0 complexity | 2fe7490ad5bb18a3da1f8e8d609b24b3 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Pimple.
  4. *
  5. * Copyright (c) 2009 Fabien Potencier
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is furnished
  12. * to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. namespace Pimple\Tests;
  26. use Pimple;
  27. /**
  28. * Pimple Test
  29. *
  30. * @package pimple
  31. * @author Igor Wiedler <igor@wiedler.ch>
  32. */
  33. class PimpleTest extends \PHPUnit_Framework_TestCase
  34. {
  35. public function testWithString()
  36. {
  37. $pimple = new Pimple();
  38. $pimple['param'] = 'value';
  39. $this->assertEquals('value', $pimple['param']);
  40. }
  41. public function testWithClosure()
  42. {
  43. $pimple = new Pimple();
  44. $pimple['service'] = function () {
  45. return new Service();
  46. };
  47. $this->assertInstanceOf('Pimple\Tests\Service', $pimple['service']);
  48. }
  49. public function testServicesShouldBeDifferent()
  50. {
  51. $pimple = new Pimple();
  52. $pimple['service'] = function () {
  53. return new Service();
  54. };
  55. $serviceOne = $pimple['service'];
  56. $this->assertInstanceOf('Pimple\Tests\Service', $serviceOne);
  57. $serviceTwo = $pimple['service'];
  58. $this->assertInstanceOf('Pimple\Tests\Service', $serviceTwo);
  59. $this->assertNotSame($serviceOne, $serviceTwo);
  60. }
  61. public function testShouldPassContainerAsParameter()
  62. {
  63. $pimple = new Pimple();
  64. $pimple['service'] = function () {
  65. return new Service();
  66. };
  67. $pimple['container'] = function ($container) {
  68. return $container;
  69. };
  70. $this->assertNotSame($pimple, $pimple['service']);
  71. $this->assertSame($pimple, $pimple['container']);
  72. }
  73. public function testIsset()
  74. {
  75. $pimple = new Pimple();
  76. $pimple['param'] = 'value';
  77. $pimple['service'] = function () {
  78. return new Service();
  79. };
  80. $pimple['null'] = null;
  81. $this->assertTrue(isset($pimple['param']));
  82. $this->assertTrue(isset($pimple['service']));
  83. $this->assertTrue(isset($pimple['null']));
  84. $this->assertFalse(isset($pimple['non_existent']));
  85. }
  86. public function testConstructorInjection ()
  87. {
  88. $params = array("param" => "value");
  89. $pimple = new Pimple($params);
  90. $this->assertSame($params['param'], $pimple['param']);
  91. }
  92. /**
  93. * @expectedException InvalidArgumentException
  94. * @expectedExceptionMessage Identifier "foo" is not defined.
  95. */
  96. public function testOffsetGetValidatesKeyIsPresent()
  97. {
  98. $pimple = new Pimple();
  99. echo $pimple['foo'];
  100. }
  101. public function testOffsetGetHonorsNullValues()
  102. {
  103. $pimple = new Pimple();
  104. $pimple['foo'] = null;
  105. $this->assertNull($pimple['foo']);
  106. }
  107. public function testUnset()
  108. {
  109. $pimple = new Pimple();
  110. $pimple['param'] = 'value';
  111. $pimple['service'] = function () {
  112. return new Service();
  113. };
  114. unset($pimple['param'], $pimple['service']);
  115. $this->assertFalse(isset($pimple['param']));
  116. $this->assertFalse(isset($pimple['service']));
  117. }
  118. public function testShare()
  119. {
  120. $pimple = new Pimple();
  121. $pimple['shared_service'] = $pimple->share(function () {
  122. return new Service();
  123. });
  124. $serviceOne = $pimple['shared_service'];
  125. $this->assertInstanceOf('Pimple\Tests\Service', $serviceOne);
  126. $serviceTwo = $pimple['shared_service'];
  127. $this->assertInstanceOf('Pimple\Tests\Service', $serviceTwo);
  128. $this->assertSame($serviceOne, $serviceTwo);
  129. }
  130. public function testProtect()
  131. {
  132. $pimple = new Pimple();
  133. $callback = function () { return 'foo'; };
  134. $pimple['protected'] = $pimple->protect($callback);
  135. $this->assertSame($callback, $pimple['protected']);
  136. }
  137. public function testGlobalFunctionNameAsParameterValue()
  138. {
  139. $pimple = new Pimple();
  140. $pimple['global_function'] = 'strlen';
  141. $this->assertSame('strlen', $pimple['global_function']);
  142. }
  143. public function testRaw()
  144. {
  145. $pimple = new Pimple();
  146. $pimple['service'] = $definition = function () { return 'foo'; };
  147. $this->assertSame($definition, $pimple->raw('service'));
  148. }
  149. public function testRawHonorsNullValues()
  150. {
  151. $pimple = new Pimple();
  152. $pimple['foo'] = null;
  153. $this->assertNull($pimple->raw('foo'));
  154. }
  155. /**
  156. * @expectedException InvalidArgumentException
  157. * @expectedExceptionMessage Identifier "foo" is not defined.
  158. */
  159. public function testRawValidatesKeyIsPresent()
  160. {
  161. $pimple = new Pimple();
  162. $pimple->raw('foo');
  163. }
  164. public function testExtend()
  165. {
  166. $pimple = new Pimple();
  167. $pimple['shared_service'] = $pimple->share(function () {
  168. return new Service();
  169. });
  170. $value = 12345;
  171. $pimple->extend('shared_service', function($sharedService) use ($value) {
  172. $sharedService->value = $value;
  173. return $sharedService;
  174. });
  175. $serviceOne = $pimple['shared_service'];
  176. $this->assertInstanceOf('Pimple\Tests\Service', $serviceOne);
  177. $this->assertEquals($value, $serviceOne->value);
  178. $serviceTwo = $pimple['shared_service'];
  179. $this->assertInstanceOf('Pimple\Tests\Service', $serviceTwo);
  180. $this->assertEquals($value, $serviceTwo->value);
  181. $this->assertSame($serviceOne, $serviceTwo);
  182. }
  183. /**
  184. * @expectedException InvalidArgumentException
  185. * @expectedExceptionMessage Identifier "foo" is not defined.
  186. */
  187. public function testExtendValidatesKeyIsPresent()
  188. {
  189. $pimple = new Pimple();
  190. $pimple->extend('foo', function () {});
  191. }
  192. /**
  193. * @expectedException InvalidArgumentException
  194. * @expectedExceptionMessage Identifier "foo" does not contain an object definition.
  195. */
  196. public function testExtendValidatesKeyYieldsObjectDefinition()
  197. {
  198. $pimple = new Pimple();
  199. $pimple['foo'] = 123;
  200. $pimple->extend('foo', function () {});
  201. }
  202. public function testKeys()
  203. {
  204. $pimple = new Pimple();
  205. $pimple['foo'] = 123;
  206. $pimple['bar'] = 123;
  207. $this->assertEquals(array('foo', 'bar'), $pimple->keys());
  208. }
  209. /** @test */
  210. public function settingAnInvokableObjectShouldTreatItAsFactory()
  211. {
  212. $pimple = new Pimple();
  213. $pimple['invokable'] = new Invokable();
  214. $this->assertEquals('I was invoked', $pimple['invokable']);
  215. }
  216. /** @test */
  217. public function settingNonInvokableObjectShouldTreatItAsParameter()
  218. {
  219. $pimple = new Pimple();
  220. $pimple['non_invokable'] = new NonInvokable();
  221. $this->assertInstanceOf('Pimple\Tests\NonInvokable', $pimple['non_invokable']);
  222. }
  223. }