tests/GameOfLife/Inputs/BoardEditor/OptionHandler/BoardEditorOptionParserTest.php
/**
     * Checks whether available options can be successfully called.
     *
     * @covers \BoardEditor\OptionHandler\BoardEditorOptionParser::callOption()
     * @covers \BoardEditor\OptionHandler\BoardEditorOptionParser::isOption()
     * @covers \BoardEditor\OptionHandler\BoardEditorOptionParser::splitOption()
     *
     * @throws \Exception
     */
Checks whether available options can be successfully called.

@throws \Exception
Checks whether available options can be successfully called . @throws \ Exception
public function testCanCallOption()
    {
        $optionHandlerMock = $this->getMockBuilder(BoardEditorOptionHandler::class)
                                  ->disableOriginalConstructor()
                                  ->getMock();
        if ($optionHandlerMock instanceof BoardEditorOptionHandler)
        {
            $optionParser = new BoardEditorOptionParser($optionHandlerMock);
            $startOptionMock = $this->getMockBuilder(StartOption::class)
                                    ->setMethods(array("callback", "arguments", "start", "getNumberOfArguments", "hasAlias", "name"))
                                    ->disableOriginalConstructor()
                                    ->getMock();
            $exitOptionMock = $this->getMockBuilder(ExitOption::class)
                                   ->setMethods(array("hasAlias", "name", "getNumberOfArguments"))
                                   ->disableOriginalConstructor()
                                   ->getMock();
            $toggleOptionMock = $this->getMockBuilder(ToggleFieldOption::class)
                                     ->setMethods(array("hasAlias", "name", "getNumberOfArguments"))
                                     ->disableOriginalConstructor()
                                     ->getMock();
            $optionList = array(
                "start" => $startOptionMock,
                "exit" => $exitOptionMock,
                "toggle" => $toggleOptionMock
            );
            $optionHandlerMock->expects($this->exactly(7))
                ->method("options")
                ->willReturn($optionList);
            $startOptionMock->expects($this->exactly(1))
                            ->method("getNumberOfArguments")
                            ->willReturn(0);
            $startOptionMock->expects($this->exactly(1))
                            ->method("callback")
                            ->willReturn("start");
            $startOptionMock->expects($this->exactly(1))
                            ->method("start")
                            ->willReturn(true);
            $startOptionMock->expects($this->exactly(3))
                            ->method("name")
                            ->willReturn("start");
            $startOptionMock->expects($this->exactly(1))
                            ->method("arguments")
                            ->willReturn(array());
            $result = $optionParser->callOption("start");
            $this->assertTrue($result);
            $startOptionMock->expects($this->exactly(2))
                            ->method("hasAlias")
                            ->withConsecutive(array("quit"), array("random"))
                            ->willReturn(false);
            $exitOptionMock->expects($this->exactly(2))
                           ->method("hasAlias")
                           ->withConsecutive(array("quit"), array("random"))
                           ->willReturn(true, false);
            $exitOptionMock->expects($this->exactly(2))
                           ->method("name")
                           ->willReturn("exit");
            $exitOptionMock->expects($this->exactly(2))
                           ->method("getNumberOfArguments")
                           ->willReturn(1);
            $exceptionOccurred = false;
            try
            {
                $optionParser->callOption("quit 4 4");
            }
            catch (\Exception $_exception)
            {
                $exceptionOccurred = true;
                $this->assertEquals("Invalid number of arguments (Expected 1, Got 2).", $_exception->getMessage());
            }
            $this->assertTrue($exceptionOccurred);
            $exceptionOccurred = false;
            try
            {
                $optionParser->callOption("random");
            }
            catch (\Exception $_exception)
            {
                $exceptionOccurred = true;
                $this->assertEquals("Invalid option \"random\".", $_exception->getMessage());
            }
            $this->assertTrue($exceptionOccurred);
            $toggleOptionMock->expects($this->exactly(2))
                             ->method("getNumberOfArguments")
                             ->willReturn(3);
            $exceptionOccurred = false;
            try
            {
                $optionParser->callOption("1,2,3,4");
            }
            catch (\Exception $_exception)
            {
                $exceptionOccurred = true;
                $this->assertEquals("Invalid number of arguments (Expected 3, Got 4).", $_exception->getMessage());
            }
            $this->assertTrue($exceptionOccurred);
        }
    }
public function testCanCallOption ( ) { $ optionHandlerMock = $ this -> getMockBuilder ( BoardEditorOptionHandler :: class ) -> disableOriginalConstructor ( ) -> getMock ( ) ; if ( $ optionHandlerMock instanceof BoardEditorOptionHandler ) { $ optionParser = new BoardEditorOptionParser ( $ optionHandlerMock ) ; $ startOptionMock = $ this -> getMockBuilder ( StartOption :: class ) -> setMethods ( array ( " callback " , " arguments " , " start " , " getNumberOfArguments " , " hasAlias " , " name " ) ) -> disableOriginalConstructor ( ) -> getMock ( ) ; $ exitOptionMock = $ this -> getMockBuilder ( ExitOption :: class ) -> setMethods ( array ( " hasAlias " , " name " , " getNumberOfArguments " ) ) -> disableOriginalConstructor ( ) -> getMock ( ) ; $ toggleOptionMock = $ this -> getMockBuilder ( ToggleFieldOption :: class ) -> setMethods ( array ( " hasAlias " , " name " , " getNumberOfArguments " ) ) -> disableOriginalConstructor ( ) -> getMock ( ) ; $ optionList = array ( " start " => $ startOptionMock , " exit " => $ exitOptionMock , " toggle " => $ toggleOptionMock ) ; $ optionHandlerMock -> expects ( $ this -> exactly ( 7 ) ) -> method ( " options " ) -> willReturn ( $ optionList ) ; $ startOptionMock -> expects ( $ this -> exactly ( 1 ) ) -> method ( " getNumberOfArguments " ) -> willReturn ( 0 ) ; $ startOptionMock -> expects ( $ this -> exactly ( 1 ) ) -> method ( " callback " ) -> willReturn ( " start " ) ; $ startOptionMock -> expects ( $ this -> exactly ( 1 ) ) -> method ( " start " ) -> willReturn ( true ) ; $ startOptionMock -> expects ( $ this -> exactly ( 3 ) ) -> method ( " name " ) -> willReturn ( " start " ) ; $ startOptionMock -> expects ( $ this -> exactly ( 1 ) ) -> method ( " arguments " ) -> willReturn ( array ( ) ) ; $ result = $ optionParser -> callOption ( " start " ) ; $ this -> assertTrue ( $ result ) ; $ startOptionMock -> expects ( $ this -> exactly ( 2 ) ) -> method ( " hasAlias " ) -> withConsecutive ( array ( " quit " ) , array ( " random " ) ) -> willReturn ( false ) ; $ exitOptionMock -> expects ( $ this -> exactly ( 2 ) ) -> method ( " hasAlias " ) -> withConsecutive ( array ( " quit " ) , array ( " random " ) ) -> willReturn ( true , false ) ; $ exitOptionMock -> expects ( $ this -> exactly ( 2 ) ) -> method ( " name " ) -> willReturn ( " exit " ) ; $ exitOptionMock -> expects ( $ this -> exactly ( 2 ) ) -> method ( " getNumberOfArguments " ) -> willReturn ( 1 ) ; $ exceptionOccurred = false ; try { $ optionParser -> callOption ( " quit 4 4 " ) ; } catch ( \ Exception $ _exception ) { $ exceptionOccurred = true ; $ this -> assertEquals ( " Invalid number of arguments (Expected 1, Got 2). " , $ _exception -> getMessage ( ) ) ; } $ this -> assertTrue ( $ exceptionOccurred ) ; $ exceptionOccurred = false ; try { $ optionParser -> callOption ( " random " ) ; } catch ( \ Exception $ _exception ) { $ exceptionOccurred = true ; $ this -> assertEquals ( " Invalid option  \" random \" . " , $ _exception -> getMessage ( ) ) ; } $ this -> assertTrue ( $ exceptionOccurred ) ; $ toggleOptionMock -> expects ( $ this -> exactly ( 2 ) ) -> method ( " getNumberOfArguments " ) -> willReturn ( 3 ) ; $ exceptionOccurred = false ; try { $ optionParser -> callOption ( " 1,2,3,4 " ) ; } catch ( \ Exception $ _exception ) { $ exceptionOccurred = true ; $ this -> assertEquals ( " Invalid number of arguments (Expected 3, Got 4). " , $ _exception -> getMessage ( ) ) ; } $ this -> assertTrue ( $ exceptionOccurred ) ; } }
Checks whether available options can be successfully called.
Checks whether available options can be successfully called .
// 2 x 2 calls, 1 x 1 call, 1 x 2 calls // Valid option with main name, valid amount of arguments // Options with alias look ups // Valid option with alias, invalid amount of arguments // Invalid option // Alias for toggle