PageRenderTime 25ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/unit/Functional/PluckTest.php

http://github.com/lstrojny/functional-php
PHP | 199 lines | 152 code | 26 blank | 21 comment | 3 complexity | 28fd08259d3347cc4dd014b9d1d56f58 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Copyright (C) 2011 - 2012 by Lars Strojny <lstrojny@php.net>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. namespace Functional;
  24. use ArrayIterator;
  25. use ArrayObject;
  26. use SplFixedArray;
  27. class MagicGetThrowException
  28. {
  29. public function __get($propertyName)
  30. {
  31. throw new \Exception($propertyName);
  32. }
  33. }
  34. class MagicGet
  35. {
  36. protected $properties;
  37. public function __construct(array $properties)
  38. {
  39. $this->properties = $properties;
  40. }
  41. public function __isset($propertyName)
  42. {
  43. return isset($this->properties[$propertyName]);
  44. }
  45. public function __get($propertyName)
  46. {
  47. return $this->properties[$propertyName];
  48. }
  49. }
  50. class MagicGetException
  51. {
  52. protected $throwExceptionInIsset = false;
  53. protected $throwExceptionInGet = false;
  54. public function __construct($throwExceptionInIsset, $throwExceptionInGet)
  55. {
  56. $this->throwExceptionInIsset = $throwExceptionInIsset;
  57. $this->throwExceptionInGet = $throwExceptionInGet;
  58. }
  59. public function __isset($propertyName)
  60. {
  61. if ($this->throwExceptionInIsset) {
  62. throw new \DomainException('__isset exception: ' . $propertyName);
  63. }
  64. return true;
  65. }
  66. public function __get($propertyName)
  67. {
  68. if ($this->throwExceptionInGet) {
  69. throw new \DomainException('__get exception: ' . $propertyName);
  70. }
  71. return "value";
  72. }
  73. }
  74. class PluckCaller
  75. {
  76. protected $property;
  77. public function call($collection, $property)
  78. {
  79. $this->property = 'value';
  80. $plucked = pluck($collection, $property);
  81. if (!isset($this->property)) {
  82. throw new \Exception('Property is no longer accessable');
  83. }
  84. return $plucked;
  85. }
  86. }
  87. class PluckTest extends AbstractTestCase
  88. {
  89. function setUp()
  90. {
  91. parent::setUp();
  92. $this->propertyExistsEverywhereArray = array((object)array('property' => 1), (object)array('property' => 2));
  93. $this->propertyExistsEverywhereIterator = new ArrayIterator($this->propertyExistsEverywhereArray);
  94. $this->propertyExistsSomewhere = array((object)array('property' => 1), (object)array('otherProperty' => 2));
  95. $this->propertyMagicGet = array(new MagicGet(array('property' => 1)), new MagicGet(array('property' => 2)), array('property' => '3'), new ArrayObject(array('property' => 4)));
  96. $this->mixedCollection = array((object)array('property' => 1), array('key' => 'value'), array('property' => 2));
  97. $this->keyedCollection = array('test' => (object)array('property' => 1), 'test2' => (object)array('property' => 2));
  98. $fixedArray = new SplFixedArray(1);
  99. $fixedArray[0] = 3;
  100. $this->numericArrayCollection = array('one' => array(1), 'two' => array(1 => 2), 'three' => array('idx' => 2), 'four' => new ArrayObject(array(2)), 'five' => $fixedArray);
  101. $this->issetExceptionArray = array((object)array('property' => 1), new MagicGetException(true, false));
  102. $this->issetExceptionIterator = new ArrayIterator($this->issetExceptionArray);
  103. $this->getExceptionArray = array((object)array('property' => 1), new MagicGetException(false, true));
  104. $this->getExceptionIterator = new ArrayIterator($this->getExceptionArray);
  105. }
  106. function testPluckPropertyThatExistsEverywhere()
  107. {
  108. $this->assertSame(array(1, 2, '3', 4), pluck($this->propertyMagicGet, 'property'));
  109. $this->assertSame(array(1, 2), pluck($this->propertyExistsEverywhereArray, 'property'));
  110. $this->assertSame(array(1, 2), pluck($this->propertyExistsEverywhereIterator, 'property'));
  111. }
  112. function testPluckPropertyThatExistsSomewhere()
  113. {
  114. $this->assertSame(array(1, null), pluck($this->propertyExistsSomewhere, 'property'));
  115. $this->assertSame(array(null, 2), pluck($this->propertyExistsSomewhere, 'otherProperty'));
  116. }
  117. function testPluckPropertyFromMixedCollection()
  118. {
  119. $this->assertSame(array(1, null, 2), pluck($this->mixedCollection, 'property'));
  120. }
  121. function testPluckProtectedProperty()
  122. {
  123. $this->assertSame(array(null, null), pluck(array($this, 'foo'), 'preserveGlobalState'));
  124. }
  125. function testPluckPropertyInKeyedCollection()
  126. {
  127. $this->assertSame(array('test' => 1, 'test2' => 2), pluck($this->keyedCollection, 'property'));
  128. }
  129. function testPluckNumericArrayIndex()
  130. {
  131. $this->assertSame(array('one' => 1, 'two' => null, 'three' => null, 'four' => 2, 'five' => 3), pluck($this->numericArrayCollection, 0));
  132. $this->assertSame(array('one' => 1, 'two' => null, 'three' => null, 'four' => 2, 'five' => 3), pluck($this->numericArrayCollection, 0));
  133. $this->assertSame(array('one' => 1, 'two' => null, 'three' => null, 'four' => 2, 'five' => 3), pluck(new ArrayIterator($this->numericArrayCollection), 0));
  134. $this->assertSame(array(1, null, null, 2, 3), pluck(array_values($this->numericArrayCollection), 0));
  135. $this->assertSame(array(1, null, null, 2, 3), pluck(new ArrayIterator(array_values($this->numericArrayCollection)), 0));
  136. $this->assertSame(array('one' => 1, 'two' => null, 'three' => null, 'four' => 2, 'five' => 3), pluck($this->numericArrayCollection, '0'));
  137. }
  138. function testPassNoCollection()
  139. {
  140. $this->expectArgumentError('Functional\pluck() expects parameter 1 to be array or instance of Traversable');
  141. pluck('invalidCollection', 'property');
  142. }
  143. function testPassNoPropertyName()
  144. {
  145. $this->expectArgumentError('Functional\pluck() expects parameter 2 to be a valid property name or array index, object given');
  146. pluck($this->propertyExistsSomewhere, new \stdClass());
  147. }
  148. function testExceptionThrownInMagicIssetWhileIteratingArray()
  149. {
  150. $this->setExpectedException('DomainException', '__isset exception: foobar');
  151. pluck($this->issetExceptionArray, 'foobar');
  152. }
  153. function testExceptionThrownInMagicIssetWhileIteratingIterator()
  154. {
  155. $this->setExpectedException('DomainException', '__isset exception: foobar');
  156. pluck($this->issetExceptionIterator, 'foobar');
  157. }
  158. function testExceptionThrownInMagicGetWhileIteratingArray()
  159. {
  160. $this->setExpectedException('DomainException', '__get exception: foobar');
  161. pluck($this->getExceptionArray, 'foobar');
  162. }
  163. function testExceptionThrownInMagicGetWhileIteratingIterator()
  164. {
  165. $this->setExpectedException('DomainException', '__get exception: foobar');
  166. pluck($this->getExceptionIterator, 'foobar');
  167. }
  168. function testClassCallsPluck()
  169. {
  170. $caller = new PluckCaller();
  171. $this->assertSame(array('test' => 1, 'test2' => 2), $caller->call($this->keyedCollection, 'property'));
  172. }
  173. }