(Fast)ArrayDecorator - Emit expected exception when using WP and strict PSR-16
authorTim Otten <totten@civicrm.org>
Wed, 13 Mar 2019 05:15:20 +0000 (22:15 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 13 Mar 2019 05:23:31 +0000 (22:23 -0700)
commit8a8cbada51b958bbbd15f59cdd3d8311606f3963
tree71eee74b63fd150555ecdee50532b627447f3d27
parent097cb75fe2ff7318de18f7d6e04375f69e8aaaf3
(Fast)ArrayDecorator - Emit expected exception when using WP and strict PSR-16

Suppose someone calls `ArrayDecorator::get()` or `FastArrayDecorator::get()`
with an invalid key (e.g.  passing `float` or an `array` instead of a
`string`).  This patch improves error-reporting for that scenario.

This is primarily about fixing multiple test failures in `E2E_Cache_ArrayDecoratorTest` reported on `wp-demo`. These
generally look like

```
1) E2E_Cache_ArrayDecoratorTest::testGetInvalidKeys with data set #1 (true)
array_key_exists(): The first argument should be either a string or an integer

/Users/totten/bknix/build/wpmaster/wp-content/plugins/civicrm/civicrm/CRM/Utils/Cache/ArrayDecorator.php:102
/Users/totten/bknix/build/wpmaster/wp-content/plugins/civicrm/civicrm/packages/Cache/IntegrationTests/LegacySimpleCacheTest.php:409
/Users/totten/bknix/civicrm-buildkit/extern/phpunit5/phpunit5.phar:598
```

Before
------

The ArrayDecorator first checks its front cache (`array_key_exists`) to see if the key is defined.  In the `wp-demo`
environment, this produces a warning and causes the test to fail.

The condition *is* erroneous, but PSR-16 specifies that the error should be reported as exception.

After
-----

The condition is reported as the expected exception. The test passes on `wp-demo`.

Comment
-------

This brings the code in `(Fast)ArrayDecorator.php` into alignment with most of the other `CRM/Utils/Cache/*.php`
drivers; in most drivers, it is typical to validate the key at the start of most functions.
CRM/Utils/Cache/ArrayDecorator.php
CRM/Utils/Cache/FastArrayDecorator.php