tests/GameOfLife/Inputs/BoardEditor/Options/SaveTemplateOptionTest.php
/**
     * Checks whether the currently edited board can be saved to a file.
     *
     * @covers \BoardEditor\Options\SaveTemplateOption::saveTemplate()
     */
Checks whether the currently edited board can be saved to a file.
Checks whether the currently edited board can be saved to a file .
public function testCanSaveCustomTemplate()
    {
        $board = new Board(2, 2, 1, true);
        $board->setField(1, 1, true);
        $this->assertEquals(1, $board->getAmountCellsAlive());
        $templateSaverMock = $this->getMockBuilder(TemplateSaver::class)
                                  ->setMethods(array("saveCustomTemplate"))
                                  ->disableOriginalConstructor()
                                  ->getMock();
        $boardEditorMock = $this->getMockBuilder(BoardEditor::class)
                                ->setMethods(array("readInput", "templateSaver", "board", "templateDirectory"))
                                ->disableOriginalConstructor()
                                ->getMock();
        if ($boardEditorMock instanceof BoardEditor)
        {
            $boardEditorMock->expects($this->exactly(1))
                            ->method("templateDirectory")
                            ->willReturn("hello");
            $option = new SaveTemplateOption($boardEditorMock);
            if ($templateSaverMock instanceof TemplateSaver) $option->setTemplateSaver($templateSaverMock);
            $expectedOutput = "Error: Invalid template name!\n";
            $this->expectOutputRegex("~.*" . $expectedOutput . ".*~");
            $result = $option->saveTemplate("");
            $this->assertFalse($result);
            $boardEditorMock->expects($this->exactly(4))
                ->method("board")
                ->willReturn($board);
            $templateSaverMock->expects($this->exactly(4))
                              ->method("saveCustomTemplate");
            $templateSaverMock->expects($this->at(0))
                              ->method("saveCustomTemplate")
                              ->with("testTemplate", $board)
                              ->willReturn(null);
            $templateSaverMock->expects($this->at(1))
                              ->method("saveCustomTemplate")
                              ->with("testTemplateAbort", $board)
                              ->willThrowException(new \Exception("Hello"));
            $templateSaverMock->expects($this->at(2))
                              ->method("saveCustomTemplate")
                              ->with("testTemplateReplace", $board)
                              ->willThrowException(new \Exception("Hello"));
            $templateSaverMock->expects($this->at(3))
                              ->method("saveCustomTemplate")
                              ->with("testTemplateReplace", $board)
                              ->willReturn(null);
            $expectedOutput = "Template successfully saved!\n\n"
                . 'You can set/unset more cells or start the simulation by typing "start"\n\n';
            $this->expectOutputRegex("~.*" . $expectedOutput . ".*~");
            $option->saveTemplate("testTemplate");
            $boardEditorMock->expects($this->exactly(2))
                ->method("readInput")
                ->withConsecutive(array("Overwrite the old file? (Yes|No): "), array("Overwrite the old file? (Yes|No): "))
                ->willReturn("n", "Y");
            $expectedOutput = "Warning: A template with that name already exists\.\n"
                            . "Saving aborted\.\n\n";
            $this->expectOutputRegex("~.*" . $expectedOutput . ".*~");
            $option->saveTemplate("testTemplateAbort");
            $expectedOutput = "Warning: A template with that name already exists\.\n"
                            . "Template successfully replaced!\n\n";
            $this->expectOutputRegex("~.*" . $expectedOutput . ".*~");
            $option->saveTemplate("testTemplateReplace");
        }
    }
public function testCanSaveCustomTemplate ( ) { $ board = new Board ( 2 , 2 , 1 , true ) ; $ board -> setField ( 1 , 1 , true ) ; $ this -> assertEquals ( 1 , $ board -> getAmountCellsAlive ( ) ) ; $ templateSaverMock = $ this -> getMockBuilder ( TemplateSaver :: class ) -> setMethods ( array ( " saveCustomTemplate " ) ) -> disableOriginalConstructor ( ) -> getMock ( ) ; $ boardEditorMock = $ this -> getMockBuilder ( BoardEditor :: class ) -> setMethods ( array ( " readInput " , " templateSaver " , " board " , " templateDirectory " ) ) -> disableOriginalConstructor ( ) -> getMock ( ) ; if ( $ boardEditorMock instanceof BoardEditor ) { $ boardEditorMock -> expects ( $ this -> exactly ( 1 ) ) -> method ( " templateDirectory " ) -> willReturn ( " hello " ) ; $ option = new SaveTemplateOption ( $ boardEditorMock ) ; if ( $ templateSaverMock instanceof TemplateSaver ) $ option -> setTemplateSaver ( $ templateSaverMock ) ; $ expectedOutput = " Error: Invalid template name! \n " ; $ this -> expectOutputRegex ( " ~.* " . $ expectedOutput . " .*~ " ) ; $ result = $ option -> saveTemplate ( " " ) ; $ this -> assertFalse ( $ result ) ; $ boardEditorMock -> expects ( $ this -> exactly ( 4 ) ) -> method ( " board " ) -> willReturn ( $ board ) ; $ templateSaverMock -> expects ( $ this -> exactly ( 4 ) ) -> method ( " saveCustomTemplate " ) ; $ templateSaverMock -> expects ( $ this -> at ( 0 ) ) -> method ( " saveCustomTemplate " ) -> with ( " testTemplate " , $ board ) -> willReturn ( null ) ; $ templateSaverMock -> expects ( $ this -> at ( 1 ) ) -> method ( " saveCustomTemplate " ) -> with ( " testTemplateAbort " , $ board ) -> willThrowException ( new \ Exception ( " Hello " ) ) ; $ templateSaverMock -> expects ( $ this -> at ( 2 ) ) -> method ( " saveCustomTemplate " ) -> with ( " testTemplateReplace " , $ board ) -> willThrowException ( new \ Exception ( " Hello " ) ) ; $ templateSaverMock -> expects ( $ this -> at ( 3 ) ) -> method ( " saveCustomTemplate " ) -> with ( " testTemplateReplace " , $ board ) -> willReturn ( null ) ; $ expectedOutput = " Template successfully saved! \n \n " . 'You can set/unset more cells or start the simulation by typing "start"\n\n' ; $ this -> expectOutputRegex ( " ~.* " . $ expectedOutput . " .*~ " ) ; $ option -> saveTemplate ( " testTemplate " ) ; $ boardEditorMock -> expects ( $ this -> exactly ( 2 ) ) -> method ( " readInput " ) -> withConsecutive ( array ( " Overwrite the old file? (Yes|No):  " ) , array ( " Overwrite the old file? (Yes|No):  " ) ) -> willReturn ( " n " , " Y " ) ; $ expectedOutput = " Warning: A template with that name already exists\. \n " . " Saving aborted\. \n \n " ; $ this -> expectOutputRegex ( " ~.* " . $ expectedOutput . " .*~ " ) ; $ option -> saveTemplate ( " testTemplateAbort " ) ; $ expectedOutput = " Warning: A template with that name already exists\. \n " . " Template successfully replaced! \n \n " ; $ this -> expectOutputRegex ( " ~.* " . $ expectedOutput . " .*~ " ) ; $ option -> saveTemplate ( " testTemplateReplace " ) ; } }
Checks whether the currently edited board can be saved to a file.
Checks whether the currently edited board can be saved to a file .
// No template name // Prepare the mocks // Template saved successfully // Template name already exists -> abort // Template name already exists -> overwrite