1
+ <?php
2
+
3
+ namespace BoggleSolver ;
4
+
5
+ class BoggleSolverTest extends \PHPUnit_Framework_TestCase
6
+ {
7
+ public $ lessWords = array (
8
+ "ABCD " , "ABC " , "ABBA " , "ABABA " , "ABQU " , "XYZ " , "ABBBBBBBBBBBBBBBB " , "AB "
9
+ );
10
+
11
+ public function testLoadDictBuildsDictionary ()
12
+ {
13
+ $ solverMock = $ this ->getMockBuilder ('\BoggleSolver\BoggleSolver ' )
14
+ ->setMethods (array ('getWords ' ))
15
+ ->getMock ();
16
+
17
+ $ solverMock ->expects ($ this ->once ())
18
+ ->method ('getWords ' )
19
+ ->will ($ this ->returnValue ($ this ->lessWords ));
20
+
21
+ $ solverMock ->boardLookup = array ('A ' => 1 );
22
+ $ solverMock ->size = 3 ;
23
+
24
+ $ solverMock ->loadDict ();
25
+
26
+ $ expectedDict = array (
27
+ 'A ' => array (
28
+ 'B ' => array (
29
+ 'C ' => array (
30
+ "ABC " ,
31
+ 'D ' => array (
32
+ "ABCD " ,
33
+ ),
34
+ ),
35
+ 'B ' => array (
36
+ 'A ' => array (
37
+ "ABBA " ,
38
+ ),
39
+ ),
40
+ 'A ' => array (
41
+ 'B ' => array (
42
+ 'A ' => array (
43
+ "ABABA " ,
44
+ ),
45
+ ),
46
+ ),
47
+ 'Qu ' => array (
48
+ "ABQU "
49
+ ),
50
+ ),
51
+ ),
52
+ );
53
+
54
+ $ this ->assertEquals ($ expectedDict , $ solverMock ->dict );
55
+ }
56
+
57
+ public function testLoadBoardLoadsBoard ()
58
+ {
59
+ $ exampleBoard = "A B C \nX Y Z \nQu R S " ;
60
+
61
+ $ solverMock = $ this ->getMockBuilder ('\BoggleSolver\BoggleSolver ' )
62
+ ->setMethods (array ('loadDict ' ))
63
+ ->getMock ();
64
+
65
+ $ solverMock ->expects ($ this ->once ())
66
+ ->method ('loadDict ' );
67
+
68
+ $ solverMock ->loadBoard ($ exampleBoard );
69
+
70
+ $ expectedLookup = array (
71
+ 'A ' , 'B ' , 'C ' , 'X ' , 'Y ' , 'Z ' , 'Q ' , 'R ' , 'S '
72
+ );
73
+
74
+ $ this ->assertEquals (3 , $ solverMock ->size );
75
+
76
+ $ this ->assertEquals ($ expectedLookup , array_keys ($ solverMock ->boardLookup ));
77
+ }
78
+
79
+ public function testLoadBoardLoadsBoardOfFourByFour ()
80
+ {
81
+ $ exampleBoard = "A B C D \nX Y Z A \nQu R S T \nO K A Y " ;
82
+
83
+ $ solverMock = $ this ->getMockBuilder ('\BoggleSolver\BoggleSolver ' )
84
+ ->setMethods (array ('loadDict ' ))
85
+ ->getMock ();
86
+
87
+ $ solverMock ->expects ($ this ->once ())
88
+ ->method ('loadDict ' );
89
+
90
+ $ solverMock ->loadBoard ($ exampleBoard );
91
+
92
+ $ this ->assertEquals (4 , $ solverMock ->size );
93
+
94
+ }
95
+
96
+ public function testLoadBoardThrowsExceptionOnUnknownBoardSize ()
97
+ {
98
+ $ exampleBoard = "A " ;
99
+
100
+ $ this ->setExpectedException ('\BoggleSolver\BoggleException ' );
101
+
102
+ $ solverMock = $ this ->getMockBuilder ('\BoggleSolver\BoggleSolver ' )
103
+ ->setMethods (array ('loadDict ' ))
104
+ ->getMock ();
105
+
106
+ $ solverMock ->expects ($ this ->never ())
107
+ ->method ('loadDict ' );
108
+
109
+ $ solverMock ->loadBoard ($ exampleBoard );
110
+ }
111
+
112
+ public function testDisplayBoardReturnsBoard ()
113
+ {
114
+ $ solverMock = $ this ->getMockBuilder ('\BoggleSolver\BoggleSolver ' )
115
+ ->setMethods (array ('loadDict ' ))
116
+ ->getMock ();
117
+
118
+ $ solverMock ->expects ($ this ->once ())
119
+ ->method ('loadDict ' );
120
+
121
+ $ solverMock ->loadBoard ("ABCDEFGHI " );
122
+
123
+ $ result = $ solverMock ->displayBoard ();
124
+
125
+ $ this ->assertEquals ("ABC \nDEF \nGHI \n" , $ result );
126
+ }
127
+
128
+ public function testFindWords ()
129
+ {
130
+ $ solverMock = $ this ->getMockBuilder ('\BoggleSolver\BoggleSolver ' )
131
+ ->setMethods (array ('getWords ' ))
132
+ ->getMock ();
133
+
134
+ $ solverMock ->expects ($ this ->once ())
135
+ ->method ('getWords ' )
136
+ ->will ($ this ->returnValue ($ this ->lessWords ));
137
+
138
+ $ solverMock ->boardLookup = array ('A ' => 1 );
139
+ $ solverMock ->size = 3 ;
140
+
141
+ $ solverMock ->loadBoard ("ABCABDABA " );
142
+
143
+ $ result = $ solverMock ->findWords ();
144
+
145
+ $ expected = array (
146
+ "ABC " , "ABCD " , "ABBA " , "ABABA " ,
147
+ );
148
+
149
+ $ this ->assertEquals ($ expected , $ result );
150
+ }
151
+ }
0 commit comments