File tree 7 files changed +79
-3
lines changed
7 files changed +79
-3
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 6
6
/**
7
7
* A generator that uses all of the other generators randomly.
8
8
*/
9
- class All implements Generator
9
+ class All extends AbstractGenerator implements Generator
10
10
{
11
11
/** @type array The other generators to use. */
12
12
protected $ _generators ;
Original file line number Diff line number Diff line change 6
6
/**
7
7
* Defines an alliterative name generator.
8
8
*/
9
- class Alliteration implements Generator
9
+ class Alliteration extends AbstractGenerator implements Generator
10
10
{
11
11
/** @type array The definition of the potential adjectives. */
12
12
protected $ _adjectives ;
Original file line number Diff line number Diff line change 8
8
* https://github.com/nullpuppy/vgng which in turn is based off of
9
9
* http://videogamena.me/vgng.js.
10
10
*/
11
- class Vgng implements Generator
11
+ class Vgng extends AbstractGenerator implements Generator
12
12
{
13
13
/** @type array The definition of the potential names. */
14
14
protected $ _definitionSets ;
Original file line number Diff line number Diff line change @@ -49,4 +49,24 @@ public function getNameForced()
49
49
$ generator = new All ([new Alliteration ($ randomizer )]);
50
50
$ this ->assertSame ('Black Bear ' , $ generator ->getName ());
51
51
}
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
+ }
52
72
}
Original file line number Diff line number Diff line change @@ -45,4 +45,22 @@ public function getNameForced()
45
45
$ generator = new Alliteration ($ randomizer );
46
46
$ this ->assertSame ('Black Bear ' , $ generator ->getName ());
47
47
}
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
+ }
48
66
}
Original file line number Diff line number Diff line change @@ -45,4 +45,23 @@ public function getNameSimilarName()
45
45
46
46
$ this ->assertSame ('3D Aerobics Academy ' , $ vgng ->getName ());
47
47
}
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
+ }
48
67
}
You can’t perform that action at this time.
0 commit comments