You are here

class UnitTest in Acquia Cloud Site Factory Connector 8.2

Same name and namespace in other branches
  1. 8 tests/AcsfEventsTest.php \UnitTest

Hierarchy

  • class \UnitTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of UnitTest

File

tests/AcsfEventsTest.php, line 28
Provides PHPUnit tests for the Acsf Events system.

View source
class UnitTest extends TestCase {

  // phpcs:enable

  /**
   * Setup.
   */
  public function setUp() {

    // The files in this directory can't be autoloaded as long as they don't
    // match their classes' namespaces.
    $files = [
      __DIR__ . '/UnitTestDummyHandler1.php',
      __DIR__ . '/UnitTestDummyHandler2.php',
      __DIR__ . '/UnitTestDummyHandler3.php',
      __DIR__ . '/UnitTestDummyHandlerInterrupt.php',
      __DIR__ . '/UnitTestDummyHandlerIncompatible.php',
    ];
    foreach ($files as $file) {

      // Acquia rules disallow 'include/require' with dynamic arguments.
      // phpcs:disable
      require_once $file;

      // phpcs:enable
    }
  }

  /**
   * Tests that the handlers are initially empty.
   */
  public function testAcsfEventHandlersEmpty() {
    $event = new AcsfEvent(new AcsfEventDispatcher(), new AcsfLog(), 'unit_test', [], []);
    $this
      ->assertEmpty($event
      ->debug());
  }

  /**
   * Tests that the push and pop methods work as expected.
   */
  public function testAcsfEventPushPop() {
    $classes = [
      'UnitTestDummyHandler1',
      'UnitTestDummyHandler2',
      'UnitTestDummyHandler3',
    ];
    $event = new AcsfEvent(new AcsfEventDispatcher(), new AcsfLog(), 'unit_test', [], []);
    foreach ($classes as $class) {
      $event
        ->pushHandler(new $class($event));
    }
    $debug = $event
      ->debug();
    $this
      ->assertCount(3, $debug['handlers']['incomplete']);
    $handlers = [];
    while ($handler = $event
      ->popHandler()) {
      $handlers[] = $handler;
    }
    $this
      ->assertCount(3, $handlers);
    $this
      ->assertEmpty($event
      ->debug());
  }

  /**
   * Tests that events get run as expected.
   */
  public function testAcsfEventExecute() {
    $registry = acsf_get_registry();
    $event = new AcsfEvent(new AcsfEventDispatcher(), new AcsfLog(), 'unit_test', $registry, []);
    $event
      ->run();
    $debug = $event
      ->debug();
    $this
      ->assertCount(3, $debug['handlers']['complete']);
  }

  /**
   * Tests that the events system handles interrupts correctly.
   */
  public function testAcsfEventInterrupt() {
    $registry = acsf_get_registry(TRUE);
    $event = new AcsfEvent(new AcsfEventDispatcher(), new AcsfLog(), 'unit_test', $registry, []);
    $event
      ->run();
    $debug = $event
      ->debug();
    $this
      ->assertCount(1, $debug['handlers']['incomplete']);
    $this
      ->assertCount(3, $debug['handlers']['complete']);
  }

  /**
   * Tests the create method.
   */
  public function testAcsfEventCreate() {
    $event = AcsfEvent::create('unit_test', []);
    $event
      ->run();
    $debug = $event
      ->debug();
    $this
      ->assertCount(3, $debug['handlers']['complete']);
  }

  /**
   * Tests that incompatible handler types may not be used.
   */
  public function testAcsfEventHandlerIncompatibleType() {
    $registry = acsf_get_registry(FALSE, 'UnitTestDummyHandler1');
    $event = new AcsfEvent(new AcsfEventDispatcher(), new AcsfLog(), 'unit_test', $registry, []);

    // Pass in a bogus handler type to trigger an exception.
    $this
      ->expectException(AcsfEventHandlerIncompatibleException::class);
    $event
      ->popHandler('bogus_type');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UnitTest::setUp public function Setup.
UnitTest::testAcsfEventCreate public function Tests the create method.
UnitTest::testAcsfEventExecute public function Tests that events get run as expected.
UnitTest::testAcsfEventHandlerIncompatibleType public function Tests that incompatible handler types may not be used.
UnitTest::testAcsfEventHandlersEmpty public function Tests that the handlers are initially empty.
UnitTest::testAcsfEventInterrupt public function Tests that the events system handles interrupts correctly.
UnitTest::testAcsfEventPushPop public function Tests that the push and pop methods work as expected.