Completed
Push — master ( 97e339...a3d86d )
by Jonathan
01:30
created

BatchContext::thereIsAnItemInTheSystemQueue()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 6
eloc 12
nc 4
nop 1
1
<?php
2
3
namespace Drupal\DrupalExtension\Context;
4
5
use Behat\MinkExtension\Context\RawMinkContext;
6
7
/**
8
 * Extensions to the Mink Extension.
9
 */
10
class BatchContext extends RawMinkContext {
11
12
  /**
13
   * Wait for the Batch API to finish.
14
   *
15
   * Wait until the id="updateprogress" element is gone,
16
   * or timeout after 3 minutes (180,000 ms).
17
   *
18
   * @Given /^I wait for the batch job to finish$/
19
   */
20
  public function iWaitForTheBatchJobToFinish() {
21
    $this->getSession()->wait(180000, 'jQuery("#updateprogress").length === 0');
22
  }
23
24
  /**
25
   * Creates a queue item. Defaults inputs if none are available.
26
   *
27
   * Expects the `data` to be a json encoded string.
28
   *
29
   * @Given there is an item in the system queue:
30
   */
31
  public function thereIsAnItemInTheSystemQueue(TableNode $table) {
32
    // Gather the data.
33
    $fields = $table->getRowsHash();
34
35
    // Default data field separately since this is longish.
36
    if (empty($fields['data'])) {
37
      $fields['data'] = json_encode([]);
38
    }
39
40
    // @see SystemQueue::createItem().
41
    $query = db_insert('queue')
42
      ->fields(array(
43
        'name' => $fields['name'] ?: user_password(),
44
        'data' => serialize(json_decode($fields['data'])),
45
        'created' => $fields['created'] ?: REQUEST_TIME,
46
        'expire' => $fields['expire'] ?: 0,
47
      ));
48
    if (!$query->execute()) {
49
      throw new Exception('Unable to create the queue item.');
50
    }
51
  }
52
53
}
54