Skip to content

Commit 4d35578

Browse files
ludicruzbobthecow
authored andcommitted
add more tests; add countable interface
1 parent 12b2775 commit 4d35578

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

src/Ruler/Set.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Justin Hileman <justin@justinhileman.info>
2121
*/
22-
class Set extends Value
22+
class Set extends Value implements \Countable
2323
{
2424
/**
2525
* Set constructor.
@@ -43,14 +43,18 @@ public function __construct($set)
4343
if (is_array($value)) {
4444
$value = new Set($value);
4545
} elseif (is_object($value)) {
46-
$reflect = new \ReflectionObject($value);
47-
if (!$reflect->hasMethod('__toString')) {
46+
if (!method_exists($value, '__toString')) {
4847
$value = new Value($value);
4948
}
5049
}
5150
}
5251

5352
$this->value = array_unique($this->value);
53+
foreach ($this->value as &$value) {
54+
if ($value instanceof Value && !$value instanceof Set) {
55+
$value = $value->getValue();
56+
}
57+
}
5458
}
5559

5660
/**
@@ -243,4 +247,12 @@ protected function isValidNumericSet()
243247
{
244248
return count($this->value) == array_sum(array_map('is_numeric', $this->value));
245249
}
250+
251+
/**
252+
* @return integer
253+
*/
254+
public function count()
255+
{
256+
return count($this->value);
257+
}
246258
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Ruler\Test\Fixtures;
4+
5+
class toStringable
6+
{
7+
private $thingy = fail;
8+
9+
public function __construct($foo = null)
10+
{
11+
$this->thingy = $foo;
12+
}
13+
14+
public function __toString()
15+
{
16+
return (string) $this->thingy;
17+
}
18+
}

tests/Ruler/Test/SetTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Ruler\Test;
44

55
use Ruler\Set;
6+
use Ruler\Value;
7+
use Ruler\Test\Fixtures\toStringable;
68

79
class SetTest extends \PHPUnit_Framework_TestCase
810
{
@@ -13,6 +15,39 @@ public function testNonStringableObject()
1315
new \stdClass()
1416
);
1517
$set = new Set($setExpected);
16-
$this->assertEquals(1, count($set));
18+
$this->assertEquals(2, count($set));
19+
}
20+
21+
public function testObjectUniqueness()
22+
{
23+
$objectA = new \stdClass();
24+
$objectA->something = 'else';
25+
$objectB = new \stdClass();
26+
$objectB->foo = 'bar';
27+
28+
$set = new Set(array(
29+
$objectA,
30+
$objectB
31+
));
32+
33+
$this->assertEquals(2, count($set));
34+
$this->assertTrue($set->setContains(new Value($objectA)));
35+
$this->assertTrue($set->setContains(new Value($objectB)));
36+
$this->assertFalse($set->setContains(new Value(false)));
37+
}
38+
39+
public function testStringable()
40+
{
41+
$set = new Set(array(
42+
$one = new toStringable(1),
43+
$two = new toStringable(2),
44+
$too = new toStringable(2)
45+
));
46+
47+
$this->assertEquals(2, count($set));
48+
$this->assertTrue($set->setContains(new Value($one)));
49+
$this->assertTrue($set->setContains(new Value($two)));
50+
$this->assertTrue($set->setContains(new Value($too)));
51+
$this->assertFalse($set->setContains(new Value(2)));
1752
}
1853
}

0 commit comments

Comments
 (0)