Skip to content

Commit 91e2bd4

Browse files
author
Alex Skrypnyk
committed
Fixed Watchdog trait not scanning for messages of type other than 'php'.
1 parent 83cd3aa commit 91e2bd4

File tree

7 files changed

+132
-12
lines changed

7 files changed

+132
-12
lines changed

src/D7/WatchdogTrait.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ trait WatchdogTrait {
2121
*/
2222
protected $watchdogScenarioStartTime;
2323

24+
/**
25+
* Array of watchdog message types.
26+
*
27+
* @var array
28+
*/
29+
protected $watchdogMessageTypes = [];
30+
2431
/**
2532
* Store current time.
2633
*
@@ -33,6 +40,34 @@ public function watchdogSetScenarioStartTime(BeforeScenarioScope $scope) {
3340
}
3441

3542
$this->watchdogScenarioStartTime = time();
43+
44+
$this->watchdogMessageTypes = $this->watchdogParseMessageTypes($scope->getScenario()->getTags());
45+
}
46+
47+
/**
48+
* Parse scenario tags into message types.
49+
*
50+
* @code
51+
* @watchdog:my_module_type @watchdog:my_other_module_type
52+
* @endcode
53+
*
54+
* @param array $tags
55+
* Array of scenario tags.
56+
* @param string $prefix
57+
* Optional tag prefix to filter by.
58+
*
59+
* @return array
60+
* Array of message types. 'php' is always added to the list.
61+
*/
62+
protected function watchdogParseMessageTypes(array $tags = [], $prefix = 'watchdog:') {
63+
$types = [];
64+
foreach ($tags as $tag) {
65+
if (strpos($tag, $prefix) === 0 && strlen($tag) > strlen($prefix)) {
66+
$types[] = substr($tag, strlen($prefix));
67+
}
68+
}
69+
70+
return array_unique(array_merge($types, ['php']));
3671
}
3772

3873
/**
@@ -64,7 +99,7 @@ public function watchdogAssertErrors(AfterScenarioScope $scope) {
6499
// of the scenario.
65100
$entries = db_select('watchdog', 'w')
66101
->fields('w')
67-
->condition('w.type', 'php', '=')
102+
->condition('w.type', $this->watchdogMessageTypes, 'IN')
68103
->condition('w.timestamp', $this->watchdogScenarioStartTime, '>=')
69104
->execute()
70105
->fetchAll();

src/WatchdogTrait.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ trait WatchdogTrait {
2222
*/
2323
protected $watchdogScenarioStartTime;
2424

25+
/**
26+
* Array of watchdog message types.
27+
*
28+
* @var array
29+
*/
30+
protected $watchdogMessageTypes = [];
31+
2532
/**
2633
* Store current time.
2734
*
@@ -34,6 +41,34 @@ public function watchdogSetScenarioStartTime(BeforeScenarioScope $scope) {
3441
}
3542

3643
$this->watchdogScenarioStartTime = time();
44+
45+
$this->watchdogMessageTypes = $this->watchdogParseMessageTypes($scope->getScenario()->getTags());
46+
}
47+
48+
/**
49+
* Parse scenario tags into message types.
50+
*
51+
* @code
52+
* @watchdog:my_module_type @watchdog:my_other_module_type
53+
* @endcode
54+
*
55+
* @param array $tags
56+
* Array of scenario tags.
57+
* @param string $prefix
58+
* Optional tag prefix to filter by.
59+
*
60+
* @return array
61+
* Array of message types. 'php' is always added to the list.
62+
*/
63+
protected function watchdogParseMessageTypes(array $tags = [], $prefix = 'watchdog:') {
64+
$types = [];
65+
foreach ($tags as $tag) {
66+
if (strpos($tag, $prefix) === 0 && strlen($tag) > strlen($prefix)) {
67+
$types[] = substr($tag, strlen($prefix));
68+
}
69+
}
70+
71+
return array_unique(array_merge($types, ['php']));
3772
}
3873

3974
/**
@@ -65,7 +100,7 @@ public function watchdogAssertErrors(AfterScenarioScope $scope) {
65100
// of the scenario.
66101
$entries = $database->select('watchdog', 'w')
67102
->fields('w')
68-
->condition('w.type', 'php', '=')
103+
->condition('w.type', $this->watchdogMessageTypes, 'IN')
69104
->condition('w.timestamp', $this->watchdogScenarioStartTime, '>=')
70105
->execute()
71106
->fetchAll();

tests/behat/bootstrap/BehatCliTrait.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,19 @@ public function throwTestException($message) {
8888
8989
/**
9090
* @Given set Drupal7 watchdog error level :level
91+
* @Given set Drupal7 watchdog error level :level of type :type
9192
*/
92-
public function setWatchdogErrorDrupal7($level) {
93-
watchdog('php', 'test', [], $level);
94-
}
93+
public function setWatchdogErrorDrupal7($level, $type = 'php') {
94+
watchdog($type, 'test', [], $level);
95+
}
9596
9697
/**
9798
* @Given set watchdog error level :level
99+
* @Given set watchdog error level :level of type :type
98100
*/
99-
public function setWatchdogErrorDrupal9($level) {
100-
\Drupal::logger('php')->log($level, 'test');
101-
}
101+
public function setWatchdogErrorDrupal9($level, $type = 'php') {
102+
\Drupal::logger($type)->log($level, 'test');
103+
}
102104
103105
}
104106
EOL;

tests/behat/bootstrap/FeatureContextD7Trait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ public function fileObjectExist($file_name) {
7878

7979
/**
8080
* @Given set Drupal7 watchdog error level :level
81+
* @Given set Drupal7 watchdog error level :level of type :type
8182
*/
82-
public function setWatchdogErrorDrupal7($level) {
83-
watchdog('php', 'test', [], $level);
83+
public function setWatchdogErrorDrupal7($level, $type = 'php') {
84+
watchdog($type, 'test', [], $level);
8485
}
8586

8687
/**

tests/behat/bootstrap/FeatureContextTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ public function userDoesNotExist($name) {
5858

5959
/**
6060
* @Given set watchdog error level :level
61+
* @Given set watchdog error level :level of type :type
6162
*/
62-
public function setWatchdogErrorDrupal9($level) {
63-
\Drupal::logger('php')->log($level, 'test');
63+
public function setWatchdogErrorDrupal9($level, $type = 'php') {
64+
\Drupal::logger($type)->log($level, 'test');
6465
}
6566

6667
/**

tests/behat/features/d7.watchdog.feature

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ Feature: Check that WatchdogTrait works for D7
1414
PHP errors were logged to watchdog during scenario "Stub scenario title" (line 3):
1515
"""
1616

17+
@trait:D7\WatchdogTrait
18+
Scenario: Assert that watchdog does not fail when a custom message type is triggered
19+
Given some behat configuration
20+
And scenario steps:
21+
"""
22+
Given set Drupal7 watchdog error level 4 of type "custom_type"
23+
"""
24+
When I run "behat --no-colors"
25+
Then it should pass
26+
27+
@trait:D7\WatchdogTrait
28+
Scenario: Assert that watchdog fails when a custom message type is triggered
29+
Given some behat configuration
30+
And scenario steps tagged with "@api @watchdog:custom_type":
31+
"""
32+
Given set Drupal7 watchdog error level 4 of type "custom_type"
33+
"""
34+
When I run "behat --no-colors"
35+
Then it should fail with an error:
36+
"""
37+
PHP errors were logged to watchdog during scenario "Stub scenario title" (line 3):
38+
"""
39+
1740
@api
1841
Scenario: Assert that watchdog does not track errors with level below threshold
1942
Given set Drupal7 watchdog error level 5

tests/behat/features/watchdog.feature

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ Feature: Check that WatchdogTrait works for or D9
1414
PHP errors were logged to watchdog during scenario "Stub scenario title" (line 3):
1515
"""
1616

17+
@trait:WatchdogTrait
18+
Scenario: Assert that watchdog does not fail when a custom message type is triggered
19+
Given some behat configuration
20+
And scenario steps:
21+
"""
22+
Given set watchdog error level "warning" of type "custom_type"
23+
"""
24+
When I run "behat --no-colors"
25+
Then it should pass
26+
27+
@trait:WatchdogTrait
28+
Scenario: Assert that watchdog fails when a custom message type is triggered
29+
Given some behat configuration
30+
And scenario steps tagged with "@api @watchdog:custom_type":
31+
"""
32+
Given set watchdog error level "warning" of type "custom_type"
33+
"""
34+
When I run "behat --no-colors"
35+
Then it should fail with an error:
36+
"""
37+
PHP errors were logged to watchdog during scenario "Stub scenario title" (line 3):
38+
"""
39+
1740
@api
1841
Scenario: Assert that watchdog does not track errors with level below threshold
1942
Given set watchdog error level "notice"

0 commit comments

Comments
 (0)