Thursday 10 December 2015

PHPUnit Test : Write a Test Method Very Quickly For Your Actual Method!


I have written a single method 'testPhpUnit' inside Utility.php which will run 2 times through the data provider method 'providerTestPhpUnit'.

One will be succeeded and another will fail.

/**
 * @file
 * Unit tests (PHPUnit) to test your methods.
 */

/**
 * Class UnitTestUtilityValidate.
 */
class UnitTestUtilityValidate extends PHPUnit_Framework_TestCase {
/**
   * Test method.
   *
   * @param string $test_string
   *   The string to be tested.
   * @param string $expected_string
   *   The string we are expecting.
   *
   * @dataProvider providerTestPhpUnit
   */
  public function testPhpUnit($test_string, $expected_string) {
    $this->assertEquals($test_string, $expected_string);
  }
  /**
   * Data provider to testPhpUnit().
   *
   * @see testPhpUnit
   */
  public function providerTestPhpUnit() {
    
    $arr_csv_data[0][0] = xyz::actualMethodToBeTest('Hello');
    $arr_csv_data[0][1] = 'Hello';
    $arr_csv_data[1][0] = xyz::actualMethodToBeTest('Hello1');
    $arr_csv_data[1][1] = 'Hello2';
    return $arr_csv_data;
  }
}

/**
 * Class xyz.
 * 
 * Actual class where your actual method will be implemented.
 */
Class xyz {
  /**
   * Actual method to be tested.
   *
   * @param string $val
   *   The string we are testing.
   *
   * return string
   *   The output value that we need to test against the expected value.
   */
  public static function actualMethodToBeTest(string $val) {
    return $val;
  }
}


$ phpunit -c phpunit.xml --filter 'UnitTestUtilityValidate::testPhpUnit' UnitTestUtilityValidate /www/demodrupal/sites/all/modules/custom/utility/unit_tests/UnitTestUtilityValidate.php


1 comment :