PHP code example of cornermonkey / phpunit-conditional-assertions

1. Go to this page and download the library: Download cornermonkey/phpunit-conditional-assertions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cornermonkey / phpunit-conditional-assertions example snippets



use CornerMonkey\ConditionalAssertion\ConditionalAssertionTrait;
use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase
{
  use ConditionalAssertionTrait;

  public function testConditionIsValid()
  {
    
    $this->when(true)->assertThat(true, true);
    $this->when(false)->assertThat(true, true);    // This assertion will not be called
    $this->unless(true)->assertThat(true, true);   // This assertion will not be called
    $this->unless(false)->assertThat(true, true);
    
  }
}


use CornerMonkey\ConditionalAssertion\ConditionalAssertionTrait;
use PHPUnit\Framework\TestCase;

class MyTestCase extends TestCase
{
  use ConditionalAssertionTrait;
   
  public function dataProvider()
  {
    return [
      [true],
      [false]
    ];
  }
  /** 
    * @dataProvider dataProvider
    */
  public function testIfExceptionShouldBeThrown($shouldThrow)
  {
    $this->when($shouldThrow, function(TestCase $testCase, $value) {
      $testCase->expectException(Exception::class);
    });
    
    if ($shouldThrow) {
      throw new Exception();
    }
  }
}