Skip to content

Commit 7004eb1

Browse files
authored
Merge pull request #34 from kabooodle/jaketoolson-patch
Added __toString() to all classes implementing Generator interface
2 parents acc5832 + 1e9e11b commit 7004eb1

7 files changed

+79
-3
lines changed

src/AbstractGenerator.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace Nubs\RandomNameGenerator;
3+
4+
abstract class AbstractGenerator implements Generator
5+
{
6+
/**
7+
* Alias for getName so that the generator can be directly stringified.
8+
*
9+
* Note that this will return a different name everytime it is cast to a
10+
* string.
11+
*
12+
* @api
13+
* @return string A random name.
14+
*/
15+
public function __toString()
16+
{
17+
return $this->getName();
18+
}
19+
}

src/All.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* A generator that uses all of the other generators randomly.
88
*/
9-
class All implements Generator
9+
class All extends AbstractGenerator implements Generator
1010
{
1111
/** @type array The other generators to use. */
1212
protected $_generators;

src/Alliteration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* Defines an alliterative name generator.
88
*/
9-
class Alliteration implements Generator
9+
class Alliteration extends AbstractGenerator implements Generator
1010
{
1111
/** @type array The definition of the potential adjectives. */
1212
protected $_adjectives;

src/Vgng.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* https://github.com/nullpuppy/vgng which in turn is based off of
99
* http://videogamena.me/vgng.js.
1010
*/
11-
class Vgng implements Generator
11+
class Vgng extends AbstractGenerator implements Generator
1212
{
1313
/** @type array The definition of the potential names. */
1414
protected $_definitionSets;

tests/AllTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,24 @@ public function getNameForced()
4949
$generator = new All([new Alliteration($randomizer)]);
5050
$this->assertSame('Black Bear', $generator->getName());
5151
}
52+
53+
/**
54+
* Verify basic behavior of __toString().
55+
*
56+
* @test
57+
* @covers ::__construct
58+
* @covers ::create
59+
* @covers ::__toString
60+
* @covers ::getName
61+
* @uses \Nubs\RandomNameGenerator\Alliteration
62+
* @uses \Nubs\RandomNameGenerator\Vgng
63+
*
64+
* @return void
65+
*/
66+
public function toStringBasic()
67+
{
68+
$generator = All::create();
69+
$name = (string)$generator;
70+
$this->assertRegexp('/.+/', $name);
71+
}
5272
}

tests/AlliterationTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,22 @@ public function getNameForced()
4545
$generator = new Alliteration($randomizer);
4646
$this->assertSame('Black Bear', $generator->getName());
4747
}
48+
49+
/**
50+
* Verify basic behavior of __toString().
51+
*
52+
* @test
53+
* @covers ::__construct
54+
* @covers ::__toString
55+
* @covers ::getName
56+
*
57+
* @return void
58+
*/
59+
public function toStringBasic()
60+
{
61+
$generator = new Alliteration();
62+
$parts = explode(' ', (string)$generator);
63+
$this->assertSame(2, count($parts));
64+
$this->assertSame($parts[0][0], $parts[1][0]);
65+
}
4866
}

tests/VgngTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,23 @@ public function getNameSimilarName()
4545

4646
$this->assertSame('3D Aerobics Academy', $vgng->getName());
4747
}
48+
49+
/**
50+
* Verify that toString returns the expected name.
51+
*
52+
* @test
53+
* @covers ::__construct
54+
* @covers ::__toString
55+
* @covers ::getName
56+
*/
57+
public function toStringBasic()
58+
{
59+
$numberGenerator = $this->createMock('\Cinam\Randomizer\NumberGenerator');
60+
$numberGenerator->expects($this->exactly(3))->method('getInt')->will($this->returnValue(1));
61+
$randomizer = new Randomizer($numberGenerator);
62+
63+
$vgng = new Vgng($randomizer);
64+
65+
$this->assertSame('8-Bit Acid - 3rd Strike', (string)$vgng);
66+
}
4867
}

0 commit comments

Comments
 (0)