File tree Expand file tree Collapse file tree 3 files changed +30
-4
lines changed
Expand file tree Collapse file tree 3 files changed +30
-4
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 (
You can’t perform that action at this time.
0 commit comments