Skip to content

Commit 92ae9d5

Browse files
committed
Initial commit.
0 parents  commit 92ae9d5

File tree

8 files changed

+252
-0
lines changed

8 files changed

+252
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor/
2+
/web/
3+
/composer.lock
4+
.env
5+
.idea

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
env:
2+
- DOCKER_COMPOSE_VERSION=1.4.2
3+
4+
before_script:
5+
- docker-compose up -d
6+
7+
script:
8+
- docker-compose exec -T drupal composer install -n --prefer-dist
9+
- docker-compose exec -T drupal composer lint
10+
- docker-compose exec -T drupal composer cs
11+
- docker-compose exec -T drupal mkdir web/sites/simpletest
12+
- docker-compose exec -T drupal composer si
13+
- docker-compose exec -T drupal chown -R www-data:www-data .
14+
- docker-compose exec -T drupal composer unit

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM massgov/drupal-container:1.0.11-dev
2+
3+
ENV PATH /var/www/code/vendor/bin:$PATH

composer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "drupaltest/queue-runner-trait",
3+
"description": "A Drupal queue runner trait for use with Drupal Test Traits.",
4+
"type": "drupal-dtt",
5+
"require": {
6+
"weitzman/drupal-test-traits": "^1.2"
7+
},
8+
"require-dev": {
9+
"drupal/core": "^8.7",
10+
"jakub-onderka/php-parallel-lint": "^1.0",
11+
"squizlabs/php_codesniffer": "^3.4",
12+
"drush/drush": "^9.7",
13+
"composer/installers": "^1.7",
14+
"zaporylie/composer-drupal-optimizations": "^1.1",
15+
"drupal-composer/drupal-scaffold": "^2.6"
16+
},
17+
"license": "GPL-2.0-or-later",
18+
"authors": [
19+
{
20+
"name": "Jonathan Hedstrom",
21+
"email": "[email protected]"
22+
}
23+
],
24+
"autoload": {
25+
"psr-4": {
26+
"DrupalTest\\QueueRunnerTrait\\": "src"
27+
}
28+
},
29+
"extra": {
30+
"installer-paths": {
31+
"web/core": ["type:drupal-core"]
32+
}
33+
},
34+
"scripts": {
35+
"si": [
36+
"drush si -yv --db-url=mysql://circle:circle@db/circle --account-name=admin --account-pass=password standard"
37+
],
38+
"cs": "phpcs -p -n --report-full",
39+
"cbf": "phpcbf --standard=PSR2 -n src tests",
40+
"lint": "parallel-lint src tests",
41+
"unit": "phpunit --colors --bootstrap=vendor/weitzman/drupal-test-traits/src/bootstrap-fast.php --printer '\\Drupal\\Tests\\Listeners\\HtmlOutputPrinter' --log-junit web/sites/simpletest/browser_output/junit.xml tests",
42+
"test": [
43+
"@lint",
44+
"@cs",
45+
"@unit"
46+
]
47+
}
48+
}

docker-compose.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
version: '3.1'
2+
3+
# This file may be used for local development of DTT. It is also used by Gitlab CI.
4+
services:
5+
drupal:
6+
build: .
7+
volumes:
8+
# Non OSX users may want to set VOLUME_FLAGS via the .env file.
9+
- .:/var/www/code:${VOLUME_FLAGS-cached}
10+
environment:
11+
- APACHE_DOCROOT=/var/www/code/web
12+
- DTT_BASE_URL=http://drupal
13+
- DTT_API_URL=http://chrome:9222
14+
- COMPOSER_ALLOW_SUPERUSER=1
15+
# @todo use firefox once https://github.com/minkphp/MinkSelenium2Driver/pull/289 is fixed.
16+
- DTT_MINK_DRIVER_ARGS=["chrome", null, "http://selenium:4444/wd/hub"]
17+
- BROWSERTEST_OUTPUT_DIRECTORY=/tmp
18+
- XDEBUG_ENABLE
19+
- XDEBUG_CONFIG
20+
# Suggested by https://confluence.jetbrains.com/display/PhpStorm/Debugging+PHP+CLI+scripts+with+PhpStorm#DebuggingPHPCLIscriptswithPhpStorm-2.StarttheScriptwithDebuggerOptions
21+
# Create a PHP Server in PHPStorm called 'dtt'. Then add path mappings during first debug session.
22+
- PHP_IDE_CONFIG=serverName=dtt
23+
working_dir: /var/www/code
24+
ports:
25+
#Expose port 81 by default since that collides less frequently.
26+
#Define WEB_PORT in a .env file to change to a different port.
27+
- '${WEB_PORT-81}:80'
28+
29+
db:
30+
image: mariadb
31+
restart: always
32+
environment:
33+
MYSQL_USER: circle
34+
MYSQL_PASSWORD: circle
35+
MYSQL_DATABASE: circle
36+
MYSQL_RANDOM_ROOT_PASSWORD: 1
37+
command: --max_allowed_packet=32M --innodb_flush_method=O_DIRECT --tmp_table_size=16M --query_cache_size=16M --innodb-flush-log-at-trx-commit=2 --innodb_buffer_pool_size=500M --innodb_log_buffer_size=64M --skip-innodb_doublewrite --innodb_log_file_size=64M
38+
ports:
39+
- '3106:3306'
40+
volumes:
41+
- dtt-datavolume:/var/lib/mysql
42+
43+
#data volumes https://docs.docker.com/storage/volumes/
44+
volumes:
45+
dtt-datavolume:

phpcs.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset name="drupaltest/queue-runner-trait">
3+
<description>PHP CodeSniffer configuration for Drupal Queue Runner Trait.</description>
4+
<file>./src</file>
5+
<file>./tests</file>
6+
<rule ref="PSR2"></rule>
7+
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
8+
<!-- This file by definition has side effects and declares symbols. -->
9+
<exclude-pattern>./src/bootstrap.php</exclude-pattern>
10+
</rule>
11+
</ruleset>

src/QueueRunnerTrait.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace DrupalTest\QueueRunnerTrait;
4+
5+
/**
6+
* Methods to process queues during test runs.
7+
*
8+
* @property \Drupal\Core\DependencyInjection\Container $container
9+
* @used-by \weitzman\DrupalTestTraits\ExistingSiteBase
10+
*/
11+
trait QueueRunnerTrait
12+
{
13+
14+
/**
15+
* Empties a given queue.
16+
*
17+
* This should be run in setUp to ensure the test only processes items
18+
* that are part of the test, not left-over items from the database.
19+
*
20+
* @param string $queue_name
21+
* The queue to empty.
22+
*/
23+
protected function clearQueue($queue_name)
24+
{
25+
$this->container->get('database')
26+
->delete('queue')
27+
->condition('name', $queue_name)
28+
->execute();
29+
}
30+
31+
/**
32+
* Runs a given queue until all items are processed.
33+
*
34+
* @param string $queue_name
35+
* The queue to process.
36+
*
37+
* @see \Drush\Drupal\Commands\core\QueueCommands::run
38+
*/
39+
protected function runQueue($queue_name)
40+
{
41+
$queue = $this->container->get('queue')->get($queue_name);
42+
$worker = $this->container->get('plugin.manager.queue_worker')->createInstance($queue_name);
43+
while ($item = $queue->claimItem()) {
44+
$worker->processItem($item->data);
45+
$queue->deleteItem($item);
46+
}
47+
}
48+
}

tests/QueueRunnerTraitTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace weitzman\DrupalTestTraits\Tests;
4+
5+
use weitzman\DrupalTestTraits\ExistingSiteBase;
6+
use DrupalTest\QueueRunnerTrait\QueueRunnerTrait;
7+
8+
/**
9+
* @coversDefaultClass \weitzman\DrupalTestTraits\QueueRunnerTrait
10+
*/
11+
class QueueRunnerTraitTest extends ExistingSiteBase
12+
{
13+
14+
use QueueRunnerTrait;
15+
16+
/**
17+
* The queue to test with.
18+
*
19+
* @var \Drupal\Core\Queue\QueueInterface
20+
*/
21+
protected $queue;
22+
23+
/**
24+
* The queue to test with.
25+
*
26+
* @var string
27+
*/
28+
protected $queueName = 'media_entity_thumbnail';
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function setUp()
34+
{
35+
parent::setUp();
36+
37+
// The core media module has a queue to test with.
38+
$this->container->get('module_installer')->install(['media']);
39+
40+
/** @var \Drupal\Core\Queue\QueueInterface $queue */
41+
$this->queue = $this->container->get('queue')->get($this->queueName);
42+
}
43+
44+
/**
45+
* @covers ::clearQueue
46+
*/
47+
public function testClearQueue()
48+
{
49+
$this->addQueueItem();
50+
$this->clearQueue($this->queueName);
51+
$this->assertEquals(0, $this->queue->numberOfItems());
52+
}
53+
54+
/**
55+
* @covers ::runQueue
56+
*/
57+
public function testRunQueue()
58+
{
59+
$this->addQueueItem();
60+
$this->runQueue($this->queueName);
61+
$this->assertEquals(0, $this->queue->numberOfItems());
62+
}
63+
64+
/**
65+
* Adds an item to the queue.
66+
*/
67+
protected function addQueueItem()
68+
{
69+
// Add an item to a queue.
70+
$item = [
71+
'id' => 'dummy_id',
72+
];
73+
$this->queue->createItem($item);
74+
75+
// Verify the item is in the queue.
76+
$this->assertEquals(1, $this->queue->numberOfItems());
77+
}
78+
}

0 commit comments

Comments
 (0)