john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

phpunit setup teardown dependencies phpstorm skip temp file expectedexception

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<?php
class Utility {

  function isOdd( $number )
  {
    if( $number % 2 == 0 ){
      return False;
    } else {
      return True;
    }
  }
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<?php
require_once "Utility.php";

class UtilityTest extends PHPUnit_Framework_TestCase
{
  protected $utility;

  function setUp() {
    $this->utility = new Utility();
  }

  function tearDown() {
    unset( $this->utility );
  }

  public function testIsOddFalse() {
    $this->assertFalse( $this->utility->isOdd( 2 ) );
  }


  // dependencies can produce output for later tests
  public function testProducerFirst()
  {
    $this->assertTrue(true);
    return 'first';
  }

  public function testProducerSecond()
  {
    $this->assertTrue(true);
    return 'second';
  }

  /**
   * @depends testProducerFirst
   * @depends testProducerSecond
   */
  public function testConsumer()
  {
    $this->assertEquals( array('first', 'second'), func_get_args() );
  }


  public function testIncompleteExample()
  {
    $this->assertEquals( array('first', 'second'), func_get_args() );
    $this->markTestIncomplete('This test has not been implemented yet.');

    if (!extension_loaded('mysqli')) {
      $this->markTestSkipped('The MySQLi extension is not available.');
    }
  }

  /**
   * @expectedException Exception
   */
  public function test_cli_unavailable_exception() {
    $cli_mock = $this->getMock('ServerCLI');
    $cli_mock->expects($this->once())
      ->method('is_available')
      ->will($this->returnValue(false));
    $this->license = new License($cli_mock);
  }
  /*
  public function __construct($cli=null) {
    ...
    if (!$this->cli->is_available()) {
      throw new Exception('CLI is unavailable');
    }
  }
  */

}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
phpunit UtilityTest.php

PHPUnit 3.7.27 by Sebastian Bergmann.

....

Time: 42 ms, Memory: 2.50Mb

OK, but incomplete or skipped tests! Tests: 5, Assertions: 5, Incomplete: 1.

. = Success, F = Fail, E = Error, S = Skipped, I = Incomplete


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
phpunit --colors --verbose --debug UtilityTest.php

# will display each test name before the result, the final summary result will be green or red


phpunit --filter test_numbers_from_config ConfigTest.php

# run any tests that match the string filter (in this case a single test)


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
INCOMPLETE

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin

Right click on an open project name -> Composer -> init
Choose the path to composer (/usr/local/bin/composer.phar)


File -> Settings (project settings) -> PHP -> PHPUnit -> Path to phpunit.phar
/usr/local/bin/phpunit




- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
REFERENCES:
http://phpunit.de/manual/3.8/en/writing-tests-for-phpunit.html
http://www.jetbrains.com/phpstorm/webhelp/enabling-phpunit-support.html#d79599e1236


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$location = tempnam('/tmp','temppem');  # $location = '/dev/null/doesnotexist'  // is useful too
$example = "smtp.example.com:465";
file_put_contents($location, "relayhost =" . $example);
$cli = new ServerCLI('/bin/ls');   // /bin/echo is useful too
$this->assertEquals($example, $cli->get_email_smartrelay_host($location));
unlink($location);

  • « arrays associative and strings foreach
  • Sftp scp ftp over ssh how to »

Published

May 7, 2014

Category

php

~259 words

Tags

  • dependencies 3
  • expectedexception 1
  • file 92
  • php 82
  • phpstorm 1
  • phpunit 3
  • setup 8
  • skip 5
  • teardown 1
  • temp 3