1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use Budabot\Core\AutoInject;
6: use Budabot\Core\CommandReply;
7: use Budabot\Core\Registry;
8: use stdClass;
9:
10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71:
72: class TestController extends AutoInject {
73:
74: 75: 76: 77:
78: public $moduleName;
79:
80: 81: 82:
83: public function setup() {
84: $this->path = getcwd() . "/modules/" . $this->moduleName . "/tests/";
85:
86: $this->settingManager->add($this->moduleName, "show_test_commands", "Show test commands as they are executed", "edit", "options", "0", "true;false", "1;0");
87: $this->settingManager->add($this->moduleName, "show_test_results", "Show test results from test commands", "edit", "options", "0", "true;false", "1;0");
88: }
89:
90: 91: 92: 93:
94: public function testListCommand($message, $channel, $sender, $sendto, $args) {
95: $files = $this->util->getFilesInDirectory($this->path);
96: $count = count($files);
97: sort($files);
98: $blob = $this->text->makeChatcmd("All Tests", "/tell <myname> test all") . "\n";
99: forEach ($files as $file) {
100: $name = str_replace(".txt", "", $file);
101: $blob .= $this->text->makeChatcmd($name, "/tell <myname> test $name") . "\n";
102: }
103: $msg = $this->text->makeBlob("Tests Available ($count)", $blob);
104: $sendto->reply($msg);
105: }
106:
107: 108: 109: 110:
111: public function testAllCommand($message, $channel, $sender, $sendto, $args) {
112: $type = "msg";
113: if ($this->setting->show_test_results == 1) {
114: $mockSendto = $sendto;
115: } else {
116: $mockSendto = new MockCommandReply();
117: }
118:
119: $files = $this->util->getFilesInDirectory($this->path);
120: $starttime = time();
121: $sendto->reply("Starting tests...");
122: forEach ($files as $file) {
123: $lines = file($this->path . $file, FILE_IGNORE_NEW_LINES);
124: $this->runTests($lines, $sender, $type, $mockSendto);
125: }
126: $time = $this->util->unixtimeToReadable(time() - $starttime);
127: $sendto->reply("Finished tests. Time: $time");
128: }
129:
130: 131: 132: 133:
134: public function testModuleCommand($message, $channel, $sender, $sendto, $args) {
135: $file = $args[1] . ".txt";
136:
137: $type = "msg";
138: if ($this->setting->show_test_results == 1) {
139: $mockSendto = $sendto;
140: } else {
141: $mockSendto = new MockCommandReply();
142: }
143:
144: $lines = file($this->path . $file, FILE_IGNORE_NEW_LINES);
145: if ($lines === false) {
146: $sendto->reply("Could not find test <highlight>$file<end> to run.");
147: } else {
148: $starttime = time();
149: $sendto->reply("Starting test $file...");
150: $this->runTests($lines, $sender, $type, $mockSendto);
151: $time = $this->util->unixtimeToReadable(time() - $starttime);
152: $sendto->reply("Finished test $file. Time: $time");
153: }
154: }
155:
156: public function runTests($commands, $sender, $type, $sendto) {
157: forEach ($commands as $line) {
158: if ($line[0] == "!") {
159: if ($this->setting->show_test_commands == 1) {
160: $this->chatBot->sendTell($line, $sender);
161: }
162: $line = substr($line, 1);
163: $this->commandManager->process($type, $line, $sender, $sendto);
164: }
165: }
166: }
167:
168: 169: 170: 171:
172: public function testorgjoinCommand($message, $channel, $sender, $sendto, $args) {
173: $packet = new stdClass;
174: $packet->type = AOCP_GROUP_MESSAGE;
175: $packet->args = array();
176: $packet->args[0] = $this->chatBot->get_gid('org msg');
177: $packet->args[1] = (int)0xFFFFFFFF;
178: $packet->args[2] = "$sender invited $args[1] to your organization.";
179:
180: $this->chatBot->process_packet($packet);
181: }
182:
183: 184: 185: 186:
187: public function testtowerattackCommand($message, $channel, $sender, $sendto, $args) {
188: $eventObj = new stdClass;
189: $eventObj->sender = -1;
190: $eventObj->channel = "All Towers";
191: $eventObj->message = "The $args[1] organization $args[2] just entered a state of war! $args[3] attacked the $args[4] organization $args[5]'s tower in $args[6] at location ($args[7],$args[8]).";
192: $eventObj->type = 'towers';
193: $this->eventManager->fireEvent($eventObj);
194: }
195:
196: 197: 198: 199:
200: public function testtowervictoryCommand($message, $channel, $sender, $sendto, $args) {
201: $packet = new stdClass;
202: $packet->type = AOCP_GROUP_MESSAGE;
203: $packet->args = array();
204: $packet->args[0] = $this->chatBot->get_gid('tower battle outcome');
205: $packet->args[1] = (int)0xFFFFFFFF;
206: $packet->args[2] = "The $args[1] organization $args[2] attacked the $args[3] $args[4] at their base in $args[5]. The attackers won!!";
207:
208: $this->chatBot->process_packet($packet);
209: }
210:
211: 212: 213: 214:
215: public function testosCommand($message, $channel, $sender, $sendto, $args) {
216: $launcher = ucfirst(strtolower($args[1]));
217:
218: $packet = new stdClass;
219: $packet->type = AOCP_GROUP_MESSAGE;
220: $packet->args = array();
221: $packet->args[0] = $this->chatBot->get_gid('org msg');
222: $packet->args[1] = (int)0xFFFFFFFF;
223: $packet->args[2] = "Blammo! $launcher has launched an orbital attack!";
224:
225: $this->chatBot->process_packet($packet);
226: }
227:
228: 229: 230: 231:
232: public function testeventCommand($message, $channel, $sender, $sendto, $args) {
233: $event = $args[1];
234:
235: list($instanceName, $methodName) = explode(".", $event);
236: $instance = Registry::getInstance($instanceName);
237: if ($instance == null) {
238: $sendto->reply("Instance <highlight>$instanceName<end> does not exist.");
239: } else if (!method_exists($instance, $methodName)) {
240: $sendto->reply("Method <highlight>$methodName<end> does not exist on instance <highlight>$instanceName<end>.");
241: } else {
242: $this->eventManager->callEventHandler(null, $event);
243: $sendto->reply("Event has been fired.");
244: }
245: }
246:
247: 248: 249: 250:
251: public function testcloaklowerCommand($message, $channel, $sender, $sendto, $args) {
252: $packet = new stdClass;
253: $packet->type = AOCP_GROUP_MESSAGE;
254: $packet->args = array();
255: $packet->args[0] = $this->chatBot->get_gid($this->chatBot->vars['my_guild']);
256: $packet->args[1] = (int)0xFFFFFFFF;
257: $packet->args[2] = "$sender turned the cloaking device in your city off.";
258:
259: $this->chatBot->process_packet($packet);
260: }
261:
262: 263: 264: 265:
266: public function testcloakraiseCommand($message, $channel, $sender, $sendto, $args) {
267: $packet = new stdClass;
268: $packet->type = AOCP_GROUP_MESSAGE;
269: $packet->args = array();
270: $packet->args[0] = $this->chatBot->get_gid($this->chatBot->vars['my_guild']);
271: $packet->args[1] = (int)0xFFFFFFFF;
272: $packet->args[2] = "$sender turned the cloaking device in your city on.";
273:
274: $this->chatBot->process_packet($packet);
275: }
276:
277: 278: 279: 280:
281: public function msginfoCommand($message, $channel, $sender, $sendto, $args) {
282: $cmd = $args[1];
283:
284: $mockSendto = new MessageInfoCommandReply($sendto);
285: $this->commandManager->process($channel, $cmd, $sender, $mockSendto);
286: }
287: }
288:
289: class MockCommandReply implements CommandReply {
290: public function reply($msg) {
291:
292:
293: }
294: }
295:
296: class MessageInfoCommandReply implements CommandReply {
297: private $sendto;
298: private $startTime;
299:
300: public function __construct($sendto) {
301: $this->sendto = $sendto;
302: $this->startTime = microtime(true);
303: }
304:
305: public function reply($msg) {
306: $endTime = microtime(true);
307: if (!is_array($msg)) {
308: $msg = array($msg);
309: }
310:
311: forEach ($msg as $page) {
312: $elapsed = round($endTime - $this->startTime, 4);
313: $this->sendto->reply($page);
314: $this->sendto->reply("Size: " . strlen($page));
315: $this->sendto->reply("Time: $elapsed seconds");
316: }
317: }
318: }