Skip to content

Commit 4aa2f05

Browse files
authored
Merge pull request #26 from ingenerator/1.5-logging-cli-device-id
Init DeviceIdentifier to fixed value in CLI without setting cookies
2 parents 3eba786 + c67c6b5 commit 4aa2f05

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
### Unreleased
22

3+
### v1.5.0-beta3 (2020-10-09)
4+
5+
* Init the DeviceIdentifier to a fixed value in the CLI environment without setting any cookies, to prevent
6+
errors if the process has already sent output.
37
* Add MutexWrapper with Mock and Db (mysql) backed implementations for preventing concurrent executions
48
of code.
59

src/Logging/DeviceIdentifier.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,19 @@ public static function forceGlobalTestValue(string $id): void
4545
* before sending any output.
4646
*
4747
* @param bool $ssl_available
48+
* @param bool $is_cli
4849
*/
49-
public static function initAndEnsureCookieSet(bool $ssl_available): void
50+
public static function initAndEnsureCookieSet(bool $ssl_available, bool $is_cli = (PHP_SAPI === 'cli')): void
5051
{
51-
static::$instance = new DeviceIdentifier(new CookieWrapper($ssl_available));
52+
if ($is_cli) {
53+
// Stub the class without setting cookies for use in a CLI environment.
54+
// Note that as it's not really appropriate to inject the `is_cli` arg to `::get()`, and we can't stub
55+
// PHP_SAPI for testing, that method will still return `-unset-` if it is called before
56+
// ::initAndEnsureCookieSet()
57+
static::forceGlobalTestValue(static::CLI_ID);
58+
} else {
59+
static::$instance = new DeviceIdentifier(new CookieWrapper($ssl_available));
60+
}
5261
static::$instance->init();
5362
}
5463

@@ -68,8 +77,9 @@ public static function get(): string
6877
return $i->getValue();
6978
}
7079

80+
const CLI_ID = 'cli-------------------';
7181
const COOKIE_LIFETIME = 'P5Y';
72-
const VALID_REGEX = '/^[a-zA-Z0-9\-_=]{22}$/';
82+
const VALID_REGEX = '/^[a-zA-Z0-9\-_=]{22}$/';
7383

7484
/**
7585
* @var string|null

test/unit/Logging/DeviceIdentifierTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ function () { DeviceIdentifier::$instance = NULL; }
154154
$old_cookie = $_COOKIE;
155155
try {
156156
$_COOKIE['did'] = '1234567890123456789012';
157-
DeviceIdentifier::initAndEnsureCookieSet(TRUE);
157+
DeviceIdentifier::initAndEnsureCookieSet(TRUE, FALSE);
158158
$this->assertSame('1234567890123456789012', DeviceIdentifier::get());
159159

160160
// Note that here the init method has assigned a global instance so changes to $_COOKIE
@@ -168,6 +168,18 @@ function () { DeviceIdentifier::$instance = NULL; }
168168
$this->assertSame('1234567890123456789012', DeviceIdentifier::get());
169169
}
170170

171+
public function test_its_static_init_creates_instance_and_initialises_with_fixed_id_in_cli()
172+
{
173+
ScopeChangingCaller::call(
174+
DeviceIdentifier::class,
175+
function () { DeviceIdentifier::$instance = NULL; }
176+
);
177+
178+
DeviceIdentifier::initAndEnsureCookieSet(TRUE); // Fall back to default arg
179+
$this->assertSame(DeviceIdentifier::CLI_ID, DeviceIdentifier::get());
180+
$this->assertRegExp(DeviceIdentifier::VALID_REGEX, DeviceIdentifier::get());
181+
}
182+
171183
public function test_its_static_get_can_read_from_cookies_even_if_not_initialised()
172184
{
173185
ScopeChangingCaller::call(

0 commit comments

Comments
 (0)