You are here

public function PhpConditionTest::testConditions in PHP 8

Tests conditions.

File

src/Tests/Condition/PhpConditionTest.php, line 40

Class

PhpConditionTest
Tests that the PHP Condition, provided by php module, is working properly.

Namespace

Drupal\php\Tests\Condition

Code

public function testConditions() {

  // Grab the PHP condition and configure it to check against a php snippet.
  $condition = $this->manager
    ->createInstance('php')
    ->setConfig('php', '<?php return TRUE; ?>');
  $this
    ->assertTrue((bool) $condition
    ->execute(), 'PHP condition passes as expected.');

  // Check for the proper summary.
  self::assertEquals($condition
    ->summary(), 'When the given PHP evaluates as TRUE.');

  // Set the PHP snippet to return FALSE.
  $condition
    ->setConfig('php', '<?php return FALSE; ?>');
  $this
    ->assertFalse((bool) $condition
    ->execute(), 'PHP condition fails as expected.');

  // Negate the condition.
  $condition
    ->setConfig('negate', TRUE);

  // Check for the proper summary.
  self::assertEquals($condition
    ->summary(), 'When the given PHP evaluates as FALSE.');

  // Reverse the negation.
  $condition
    ->setConfig('negate', FALSE);

  // Set and empty snippet.
  $condition
    ->setConfig('php', FALSE);

  // Check for the proper summary.
  self::assertEquals($condition
    ->summary(), 'No PHP code has been provided.');
}