Unittests
[ class tree: Unittests ] [ index: Unittests ] [ all elements ]

Source for file AbstractTest.php

Documentation is available at AbstractTest.php

  1. <?php
  2. /**
  3.  * Console Library
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revisio$
  15.  *
  16.  * @category    Console
  17.  * @package     Unittests
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. // Call Console_ApplicationTest::main() if this source file is executed directly.
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23.     define('PHPUnit_MAIN_METHOD''Console_Application_AbstractTest::main');
  24. }
  25.  
  26. /**
  27.  * Test helper
  28.  */
  29. require_once(dirname(__FILE__'/../../TestHelper.php');
  30.  
  31. /**
  32.  * Console_Application_Abstract
  33.  */
  34. require_once('Console/Application/Abstract.php');
  35.  
  36.     private $callCounter 0;
  37.  
  38.     public $setUpCalled = false;
  39.     public $findConsoleParametersCalled = false;
  40.     public $runCalled = false;
  41.     public $tearDownCalled = false;
  42.     public $onErrorCalled = false;
  43.  
  44.     public $retValFindParams = true;
  45.     public $thowsExceptionInRun = false;
  46.  
  47.     public $sigTermCalled = false;
  48.     public $sigIntCalled = false;
  49.     public $sigHangUpCalled = false;
  50.     
  51.     public static $disablePcntl false;
  52.    
  53.     protected function getCallCounter({
  54.         $this->callCounter++;
  55.         return $this->callCounter;
  56.     }
  57.  
  58.     protected function run({
  59.         $this->runCalled = $this->getCallCounter();
  60.  
  61.         if ($this->thowsExceptionInRun{
  62.             throw new Console_Exception('');
  63.         }
  64.     }
  65.  
  66.     protected function setUp($maxExecutionTime 10$memoryLimit '32M'{
  67.         parent::setUp($maxExecutionTime$memoryLimit);
  68.         $this->setUpCalled = $this->getCallCounter();
  69.     }
  70.  
  71.     protected function findConsoleParameters({
  72.         parent::findConsoleParameters();
  73.         $this->findConsoleParametersCalled = $this->getCallCounter();
  74.         return $this->retValFindParams;
  75.     }
  76.  
  77.     protected function tearDown({
  78.         parent::tearDown();
  79.         $this->tearDownCalled = $this->getCallCounter();
  80.     }
  81.  
  82.     protected function onError($errorData{
  83.         parent::onError($errorData);
  84.         $this->onErrorCalled = $this->getCallCounter();
  85.     }
  86.  
  87.     protected function isPcntlEnabled({
  88.         if (self::$disablePcntl{
  89.             return false;
  90.         }
  91.  
  92.         return parent::isPcntlEnabled();
  93.     }
  94.  
  95.     public function signalTerm($signal{
  96.         $this->sigTermCalled = true;
  97.  
  98.     }
  99.  
  100.     public function signalInt($signal{
  101.         $this->sigIntCalled = true;
  102.     }
  103.  
  104.     public function signalHangUp($signal{
  105.         $this->sigHangUpCalled = true;
  106.     }
  107. }
  108.  
  109. /**
  110.  * @category   Console
  111.  * @package    Unittests
  112.  * @subpackage Console_Application
  113.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  114.  */
  115. class Console_Application_AbstractTest extends PHPUnit_Framework_TestCase {
  116.     private $applicationFixture;
  117.  
  118.     /**
  119.      * Runs the test methods of this class.
  120.      *
  121.      * @return void 
  122.      */
  123.     public static function main({
  124.         require_once('PHPUnit/TextUI/TestRunner.php');
  125.         $suite  new PHPUnit_Framework_TestSuite("Console_Application_AbstractTest");
  126.         $result PHPUnit_TextUI_TestRunner::run($suite);
  127.     }
  128.  
  129.     public function setUp({
  130.         Console_Application_Concrete::$disablePcntl false;
  131.         $this->applicationFixture new Console_Application_Concrete('Concrete');
  132.     }
  133.  
  134.     public function tearDown({
  135.         $this->applicationFixture null;
  136.     }
  137.  
  138.     /**
  139.      *
  140.      */
  141.     public function testExecutes({
  142.         $this->applicationFixture->execute();
  143.         $this->assertTrue($this->applicationFixture->isExecuted());
  144.     }
  145.  
  146.     public function testCallsSetUpHook({
  147.         $this->applicationFixture->execute();
  148.         // We assume that the template method execute() calls
  149.         // setUp() first.
  150.         $this->assertEquals(2$this->applicationFixture->setUpCalled);
  151.         $this->assertTrue($this->applicationFixture->isExecuted());
  152.     }
  153.  
  154.     public function testCallsFindConsoleParmetersHook({
  155.         $this->applicationFixture->execute();
  156.         // We assume that the template method execute() calls
  157.         // findConsoleParameters() second.
  158.         $this->assertEquals(1$this->applicationFixture->findConsoleParametersCalled);
  159.         $this->assertTrue($this->applicationFixture->isExecuted());
  160.     }
  161.  
  162.     public function testExitsIfFindConsoleParametersReturnsFalse({
  163.         $this->applicationFixture->retValFindParams false;
  164.         $this->applicationFixture->execute();
  165.         $this->assertEquals(1$this->applicationFixture->findConsoleParametersCalled);
  166.         $this->assertFalse($this->applicationFixture->setUpCalled);
  167.         $this->assertFalse($this->applicationFixture->runCalled);
  168.         $this->assertFalse($this->applicationFixture->tearDownCalled);
  169.         $this->assertFalse($this->applicationFixture->isExecuted());
  170.     }
  171.  
  172.     public function testCallsRunHook({
  173.         $this->applicationFixture->execute();
  174.         // We assume that the template method execute() calls
  175.         // run() third.
  176.         $this->assertEquals(3$this->applicationFixture->runCalled);
  177.         $this->assertTrue($this->applicationFixture->isExecuted());
  178.     }
  179.  
  180.     public function testCallsTearDownHook({
  181.         $this->applicationFixture->execute();
  182.         // We assume that the template method execute() calls
  183.         // tearDown() fourth.
  184.         $this->assertEquals(4$this->applicationFixture->tearDownCalled);
  185.         $this->assertTrue($this->applicationFixture->isExecuted());
  186.     }
  187.  
  188.     public function testCallsOnErrorHookByExceptions({
  189.         $this->applicationFixture->thowsExceptionInRun true;
  190.         $this->applicationFixture->execute();
  191.         $this->assertEquals(4$this->applicationFixture->onErrorCalled);
  192.         $this->assertFalse($this->applicationFixture->tearDownCalled);
  193.         $this->assertTrue($this->applicationFixture->isExecuted());
  194.     }
  195.  
  196.     public function testIsVerbose({
  197.         $this->markTestIncomplete();
  198.     }
  199.  
  200.     public function testGetHelpMessage({
  201.         $expectedString  "\tconcrete.sh -v : Sets the script in verbose mode." PHP_EOL;
  202.         $expectedString .= "\tconcrete.sh -h : prints this help message."  PHP_EOL PHP_EOL;
  203.         $expectedString .= 'For bug reporting or issues contact: foo@bar.de'  PHP_EOL;
  204.         $usageString $this->applicationFixture->getHelpMessage();
  205.         $this->assertEquals($expectedString$usageString);
  206.  
  207.         $expectedString  "\tconcrete.sh -v : Sets the script in verbose mode." PHP_EOL;
  208.         $expectedString .= "\tconcrete.sh -h : prints this help message."  PHP_EOL PHP_EOL;
  209.         $expectedString .= 'Written by Sven Strittmatter.' PHP_EOL;
  210.         $expectedString .= 'For bug reporting or issues contact: ich@weltraumschaf.de'  PHP_EOL;
  211.         $usageString $this->applicationFixture->getHelpMessage('Sven Strittmatter''ich@weltraumschaf.de');
  212.         $this->assertEquals($expectedString$usageString);
  213.     }
  214.  
  215.     public function testGetUsage({
  216.         $this->assertEquals('' PHP_EOL$this->applicationFixture->echoMsg());
  217.         $this->assertEquals('test message' PHP_EOL$this->applicationFixture->echoMsg('test message'));
  218.     }
  219.  
  220.     public function testEchoMessage({
  221.         /**
  222.          * @todo should be renamed to displayMessage()
  223.          */
  224.         $this->markTestIncomplete();
  225.     }
  226.  
  227.     public function testThrowExcpetionOnCantRegisterSignalHandler({
  228.         $this->setExpectedException('RuntimeException');
  229.         Console_Application_Concrete::$disablePcntl true;
  230.         $app new Console_Application_Concrete('Concrete');
  231.     }
  232.  
  233.     public function testHandleSignalTerm({
  234.         $this->markTestIncomplete('On Mac OS the function pcntl_signal_dispatch() does not exists!');
  235.         posix_kill(posix_getpid()SIGTERM);
  236.         pcntl_signal_dispatch();
  237.         $this->assertTrue($this->applicationFixture->sigTermCalled);
  238.     }
  239.  
  240.     public function testHandleSignalInt({
  241.         $this->markTestIncomplete('On Mac OS the function pcntl_signal_dispatch() does not exists!');
  242.         posix_kill(posix_getpid()SIGINT);
  243.         pcntl_signal_dispatch();
  244.         $this->assertTrue($this->applicationFixture->sigIntCalled);
  245.     }
  246.  
  247.     public function testHandleSignalHup({
  248.         $this->markTestIncomplete('On Mac OS the function pcntl_signal_dispatch() does not exists!');
  249.         posix_kill(posix_getpid()SIGHUP);
  250.         pcntl_signal_dispatch();
  251.         $this->assertTrue($this->applicationFixture->sigHangUpCalled);
  252.     }
  253.  
  254.     public function testSetPhpEnviroment({
  255.         $this->markTestIncomplete();
  256.     }
  257.  
  258.     public function testRestorePhpEnviroment({
  259.         $this->markTestIncomplete();
  260.     }
  261. }
  262.  
  263. // Call Console_ApplicationTest::main() if this source file is executed directly.
  264. if (PHPUnit_MAIN_METHOD === 'Console_Application_AbstractTest::main'{
  265. }

Documentation generated on Mon, 17 Aug 2009 15:52:21 +0200 by phpDocumentor 1.4.2