public function QueueCase::testRequeueException

master queueTest.php public QueueCase::testRequeueException()

Tests the RequeueException.

File

tests/queueTest.php, line 91

Namespace

Unish

Class

QueueCase
@group commands

Code

public function testRequeueException() {
  if (UNISH_DRUPAL_MAJOR_VERSION < 8) {
    $this->markTestSkipped("RequeueException only available in Drupal 8.");
  }

  $sites = $this->setUpDrupal(1, TRUE);
  $options = array(
    'yes' => NULL,
    'root' => $this->webroot(),
    'uri' => key($sites),
  );

  // Copy the 'woot' module over to the Drupal site we just set up.
  $this->setupModulesForTests($this->webroot());

  // Enable woot module, which contains a queue worker that throws a
  // RequeueException.
  $this->drush('pm-enable', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS);

  // Add an item to the queue.
  $this->drush('php-script', array('requeue_script'), $options + array('script-path' => dirname(__FILE__) . '/resources'));

  // Check that the queue exists and it has one item in it.
  $expected = 'woot_requeue_exception,%items,Drupal\Core\Queue\DatabaseQueue';
  $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
  $output = trim($this->getOutput());
  $this->assertEquals(str_replace('%items', 1, $expected), $output, 'Item was successfully added to the queue.');

  // Process the queue.
  $this->drush('queue-run', array('woot_requeue_exception'), $options);

  // Check that the item was processed after being requeued once.
  // Here is the detailed workflow of what the above command did.
  // 1. Drush calls drush queue-run woot_requeue_exception.
  // 2. Drush claims the item. The worker sets a state variable (see below)
  // and throws the RequeueException.
  // 3. Drush catches the exception and puts it back in the queue.
  // 4. Drush claims the next item, which is the one that we just requeued.
  // 5. The worker finds the state variable, so it does not throw the
  // RequeueException this time (see below).
  // 6. Drush removes the item from the queue.
  // 7. Command finishes. The queue is empty.
  $this->drush('queue-list', array(), $options + array('pipe' => TRUE));
  $output = trim($this->getOutput());
  $this->assertEquals(str_replace('%items', 0, $expected), $output, 'Queue item processed after being requeued.');
}