diff --git a/app/classes/ReleaseInsights/Bugzilla.php b/app/classes/ReleaseInsights/Bugzilla.php index fd8f53d..bba174a 100644 --- a/app/classes/ReleaseInsights/Bugzilla.php +++ b/app/classes/ReleaseInsights/Bugzilla.php @@ -4,8 +4,6 @@ namespace ReleaseInsights; -use ReleaseInsights\URL; - class Bugzilla { /** @@ -51,7 +49,7 @@ public static function linkify(string $text): ?string */ public static function getBugsFromHgWeb(string $query, bool $detect_backouts = false, int $cache_ttl = 0): array { - $results = Utils::getJson($query, $cache_ttl)['pushes'] ?? null; + $results = Json::load($query, $cache_ttl)['pushes'] ?? null; // Handle the lack of data from HG if (empty($results)) { diff --git a/app/classes/ReleaseInsights/Data.php b/app/classes/ReleaseInsights/Data.php index 1e0f17d..2b2d74e 100644 --- a/app/classes/ReleaseInsights/Data.php +++ b/app/classes/ReleaseInsights/Data.php @@ -4,8 +4,6 @@ namespace ReleaseInsights; -use ReleaseInsights\URL; - class Data { /** @var array $future_releases */ @@ -42,7 +40,7 @@ public function getFutureReleases(): array public function getESRReleases(): array { // Historical data from Product Details, cache a week - $esr_releases = Utils::getJson($this->pd_url . 'firefox.json', $this->cache_duration)['releases']; + $esr_releases = Json::load($this->pd_url . 'firefox.json', $this->cache_duration)['releases']; // Reduce to only ESR releases $esr_releases = array_filter( @@ -81,8 +79,8 @@ public function getLatestMajorRelease(): array public function getPastReleases(bool $dot_releases = true): array { // Historical data from Product Details, cache a week - $major_releases = Utils::getJson($this->pd_url . 'firefox_history_major_releases.json', $this->cache_duration); - $minor_releases = $dot_releases == true ? Utils::getJson($this->pd_url . 'firefox_history_stability_releases.json', $this->cache_duration) : []; + $major_releases = Json::load($this->pd_url . 'firefox_history_major_releases.json', $this->cache_duration); + $minor_releases = $dot_releases == true ? Json::load($this->pd_url . 'firefox_history_stability_releases.json', $this->cache_duration) : []; $all_releases = [...$major_releases, ...$minor_releases]; // Sort releases by release date @@ -119,7 +117,7 @@ public function getPastReleases(bool $dot_releases = true): array */ public function getPastBetas(): array { - return Utils::getJson($this->pd_url . 'firefox_history_development_releases.json', $this->cache_duration); + return Json::load($this->pd_url . 'firefox_history_development_releases.json', $this->cache_duration); } /** @@ -129,7 +127,7 @@ public function getPastBetas(): array */ public function getMajorPastReleases(): array { - return Utils::getJson($this->pd_url . 'firefox_history_major_releases.json', $this->cache_duration); + return Json::load($this->pd_url . 'firefox_history_major_releases.json', $this->cache_duration); } /** @@ -149,7 +147,7 @@ public function getMajorReleases(): array public function getFirefoxVersions(): array { // Cache Product Details versions, 15mn cache - return Utils::getJson($this->pd_url . 'firefox_versions.json', $this->cache_duration); + return Json::load($this->pd_url . 'firefox_versions.json', $this->cache_duration); } /** diff --git a/app/classes/ReleaseInsights/ESR.php b/app/classes/ReleaseInsights/ESR.php index dcaa901..94bbce2 100644 --- a/app/classes/ReleaseInsights/ESR.php +++ b/app/classes/ReleaseInsights/ESR.php @@ -4,8 +4,6 @@ namespace ReleaseInsights; -use ReleaseInsights\Version; - class ESR { /** diff --git a/app/classes/ReleaseInsights/Json.php b/app/classes/ReleaseInsights/Json.php new file mode 100644 index 0000000..58259c3 --- /dev/null +++ b/app/classes/ReleaseInsights/Json.php @@ -0,0 +1,123 @@ + $data + */ + public function __construct( + public array $data = [], + public mixed $jsonp = false, + public bool $pretty_print = false, + ) + { + } + + /** + * Return a JSON/JSONP representation of data with the right HTTP headers + */ + public function output(): string + { + $json = $this->pretty_print ? json_encode($this->data, JSON_PRETTY_PRINT) : json_encode($this->data); + $mime = 'application/json'; + + if (is_string($this->jsonp)) { + $mime = 'application/javascript'; + $json = $this->jsonp . '(' . $json . ')'; + } + + ob_start(); + header("access-control-allow-origin: *"); + header("Content-type: {$mime}; charset=UTF-8"); + header("Content-Length: " . strlen($json)); + echo $json; + $json = ob_get_contents(); + ob_end_clean(); + + return $json; + } + + /** + * Return HTTP code 400 and an error message if an API call is incorrect + */ + public function outputError(): string + { + $this->pretty_print = true; + http_response_code(400); + + return $this->output(); + } + + /** + * Output Json data + */ + public function render(): void + { + if (array_key_exists('error', $this->data)) { + print_r($this->outputError()); + } else { + $this->jsonp = $_GET['callback'] ?? false; + print_r($this->output()); + } + } + + // Below are static methods imported from the Utils class, refactoring in progress + + /** + * @return array $template_data + */ + public static function load(string $url, int $ttl = 0): array + { + if (! $data = Cache::getKey($url, $ttl)) { + $data = Utils::getFile($url); + + // No data returned, bug or incorrect date, don't cache. + if (empty($data)) { + return []; + } + + // Invalid Json, don't cache. + if (! self::isValid($data)) { + return []; + } + + Cache::setKey($url, $data, $ttl); + } + + return self::toArray($data); + } + + public static function isValid(string $data): bool + { + return is_string($data) + && is_array(json_decode($data, true)) + && (json_last_error() == JSON_ERROR_NONE); + } + + /** + * Return an Array from a Json string + * This is an utility function as we use json_decode in multiple places, + * always with the same options. That will make these calls shorter, + * with a more explicit function name and will allow to change default + * values at the app level. + * + * @return array Associative array from a Json string + */ + public static function toArray(string $data): array + { + $data = json_decode( + json: $data, + associative: true, + depth: 512, + ); + + return is_null($data) ? [] : $data; + } +} + diff --git a/app/classes/ReleaseInsights/Nightly.php b/app/classes/ReleaseInsights/Nightly.php index d092d88..84284b5 100644 --- a/app/classes/ReleaseInsights/Nightly.php +++ b/app/classes/ReleaseInsights/Nightly.php @@ -4,8 +4,6 @@ namespace ReleaseInsights; -Use ReleaseInsights\URL; - class Nightly { public string $version; @@ -17,7 +15,7 @@ public function __construct( public string $AUS = URL::Balrog->value, public string $update_status = 'emergency_shutoff/Firefox/nightly', ) { - $this->version = Utils::getJson( + $this->version = Json::load( $this->pd . 'firefox_versions.json', 604800 )['FIREFOX_NIGHTLY']; @@ -39,7 +37,7 @@ public function __construct( } if ($this->auto_updates === false) { - $tmp = Utils::getJson($this->AUS . $this->update_status, 1); + $tmp = Json::load($this->AUS . $this->update_status, 1); $tmp = $tmp['comment'] ?? ''; $this->emergency_message = Utils::secureText($tmp); unset($tmp); diff --git a/app/classes/ReleaseInsights/Release.php b/app/classes/ReleaseInsights/Release.php index 63c186a..e75e651 100644 --- a/app/classes/ReleaseInsights/Release.php +++ b/app/classes/ReleaseInsights/Release.php @@ -5,8 +5,6 @@ namespace ReleaseInsights; use DateTime; -use ReleaseInsights\ReleaseStatus as Status; -use ReleaseInsights\URL; class Release { diff --git a/app/classes/ReleaseInsights/Utils.php b/app/classes/ReleaseInsights/Utils.php index 0ab7f6b..4182e91 100644 --- a/app/classes/ReleaseInsights/Utils.php +++ b/app/classes/ReleaseInsights/Utils.php @@ -7,8 +7,6 @@ use Cache\Cache; use DateTime; use GuzzleHttp\Client; -use Json\Json; -use ReleaseInsights\URL; class Utils { @@ -44,7 +42,7 @@ public static function getCrashesForBuildID(int $buildid): array Cache::setKey($cache_id, $data); } - return self::arrayFromJson($data); + return Json::toArray($data); } /** @@ -78,27 +76,7 @@ public static function getBugsforCrashSignature(string $signature): array Cache::setKey($cache_id, $data); } - return self::arrayFromJson($data); - } - - /** - * Return an Array from a Json string - * This is an utility function as we use json_decode in multiple places, - * always with the same options. That will make these calls shorter, - * with a more explicit function name and will allow to change default - * values at the app level. - * - * @return array Associative array from a Json string - */ - public static function arrayFromJson(string $data): array - { - $data = json_decode( - json: $data, - associative: true, - depth: 512, - ); - - return is_null($data) ? [] : $data; + return Json::toArray($data); } /** @@ -192,7 +170,7 @@ public static function secureText(string $string): string } /** - * getFile code coverage is done through its consumer getJson + * getFile code coverage is done through its main consumer Json::load() */ public static function getFile(string $url): string { @@ -228,37 +206,6 @@ public static function getFile(string $url): string // @codeCoverageIgnoreEnd } - /** - * @return array $template_data - */ - public static function getJson(string $url, int $ttl = 0): array - { - if (! $data = Cache::getKey($url, $ttl)) { - $data = Utils::getFile($url); - - // No data returned, bug or incorrect date, don't cache. - if (empty($data)) { - return []; - } - - // Invalid Json, don't cache. - if (! self::isJson($data)) { - return []; - } - - Cache::setKey($url, $data, $ttl); - } - - return self::arrayFromJson($data); - } - - public static function isJson(string $data): bool - { - return is_string($data) - && is_array(json_decode($data, true)) - && (json_last_error() == JSON_ERROR_NONE); - } - public static function mtrim(string $string): string { $string = explode(' ', $string); @@ -351,21 +298,4 @@ public static function isDateBetweenDates(DateTime $date, DateTime $startDate, D { return $date > $startDate && $date < $endDate; } - - /** - * Utility function to output Json data - * - * @param array $data - */ - public static function renderJson(array $data): void - { - // Output a JSON or JSONP representation of search results - $json = new Json(); - - if (array_key_exists('error', $data)) { - print_r($json->outputError($data['error'])); - } else { - print_r($json->outputContent($data, $_GET['callback'] ?? false)); - } - } } diff --git a/app/controllers/api/esr_releases.php b/app/controllers/api/esr_releases.php index f8561eb..761f835 100644 --- a/app/controllers/api/esr_releases.php +++ b/app/controllers/api/esr_releases.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use ReleaseInsights\Json; + $json = include MODELS . 'api/esr_releases.php'; -ReleaseInsights\Utils::renderJson($json); +(new Json($json))->render(); diff --git a/app/controllers/api/external.php b/app/controllers/api/external.php index bff2801..8bac349 100644 --- a/app/controllers/api/external.php +++ b/app/controllers/api/external.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use ReleaseInsights\Json; + $json = include MODELS . 'api/external.php'; -ReleaseInsights\Utils::renderJson($json); +(new Json($json))->render(); diff --git a/app/controllers/api/firefox_releases.php b/app/controllers/api/firefox_releases.php index dabfb73..2b8f6e5 100644 --- a/app/controllers/api/firefox_releases.php +++ b/app/controllers/api/firefox_releases.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use ReleaseInsights\Json; + $json = include MODELS . 'api/firefox_releases.php'; -ReleaseInsights\Utils::renderJson($json); +(new Json($json))->render(); diff --git a/app/controllers/api/nightly.php b/app/controllers/api/nightly.php index 67fc6f4..b1d1dcd 100644 --- a/app/controllers/api/nightly.php +++ b/app/controllers/api/nightly.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use ReleaseInsights\Json; + $json = include MODELS . 'api/nightly.php'; // We want to send a simplified Json for our public API @@ -11,4 +13,4 @@ $json_for_api[$key] = $values['revision']; } -ReleaseInsights\Utils::renderJson($json_for_api); +(new Json($json_for_api))->render(); \ No newline at end of file diff --git a/app/controllers/api/nightly_crashes.php b/app/controllers/api/nightly_crashes.php index f89b4df..2d8fbbf 100644 --- a/app/controllers/api/nightly_crashes.php +++ b/app/controllers/api/nightly_crashes.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use ReleaseInsights\Json; + $json = include MODELS . 'api/nightly_crashes.php'; -ReleaseInsights\Utils::renderJson($json); +(new Json($json))->render(); diff --git a/app/controllers/api/release_owners.php b/app/controllers/api/release_owners.php index aa87ac6..ae75a79 100644 --- a/app/controllers/api/release_owners.php +++ b/app/controllers/api/release_owners.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use ReleaseInsights\Json; + $json = include MODELS . 'api/release_owners.php'; -ReleaseInsights\Utils::renderJson($json); +(new Json($json))->render(); diff --git a/app/controllers/api/release_schedule.php b/app/controllers/api/release_schedule.php index a01e450..50a2654 100644 --- a/app/controllers/api/release_schedule.php +++ b/app/controllers/api/release_schedule.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use ReleaseInsights\Json; + $json = include MODELS . 'api/release_schedule.php'; -ReleaseInsights\Utils::renderJson($json); +(new Json($json))->render(); diff --git a/app/models/api/nightly.php b/app/models/api/nightly.php index bbf29fd..e6703bc 100644 --- a/app/models/api/nightly.php +++ b/app/models/api/nightly.php @@ -3,7 +3,7 @@ declare(strict_types=1); use Cache\Cache; -use ReleaseInsights\{URL, Utils}; +use ReleaseInsights\{Json, URL, Utils}; $date = Utils::getDate(); @@ -38,7 +38,7 @@ false, stream_context_create($options) ); - $data = Utils::arrayFromJson($data); + $data = Json::toArray($data); $data = array_column($data['hits']['hits'], '_source'); // No data returned, bug or incorrect date, don't cache. diff --git a/app/models/api/nightly_crashes.php b/app/models/api/nightly_crashes.php index cbedad0..2b4e419 100644 --- a/app/models/api/nightly_crashes.php +++ b/app/models/api/nightly_crashes.php @@ -3,7 +3,7 @@ declare(strict_types=1); use Cache\Cache; -use ReleaseInsights\{URL, Utils}; +use ReleaseInsights\{Json, URL, Utils}; $buildid = Utils::getBuildID((int) ($_GET['buildid'] ?? 1)); @@ -12,7 +12,7 @@ // If we can't retrieve cached data, we create and cache it. // We cache because we want to avoid http request latency if (! $data = Cache::getKey($cache_id, 1)) { - $data = Utils::arrayFromJson(file_get_contents($cache_id)); + $data = Json::toArray(file_get_contents($cache_id)); // No data returned, don't cache. if (empty($data)) { diff --git a/app/models/future_release.php b/app/models/future_release.php index a78813b..f899409 100644 --- a/app/models/future_release.php +++ b/app/models/future_release.php @@ -2,7 +2,7 @@ declare(strict_types=1); -use ReleaseInsights\{Bugzilla, Data, Duration, Nightly, Release, URL, Utils, Version}; +use ReleaseInsights\{Bugzilla, Data, Duration, Json, Nightly, Release, URL, Utils, Version}; $requested_version = Version::get(); @@ -100,7 +100,7 @@ // Check current rollout for the beta channel if ((int) $requested_version === BETA) { - $rollout = Utils::getJson(URL::Balrog->value . 'rules/firefox-beta')['backgroundRate']; + $rollout = Json::load(URL::Balrog->value . 'rules/firefox-beta')['backgroundRate']; } return [ diff --git a/app/models/home.php b/app/models/home.php index 7038ab2..357f860 100644 --- a/app/models/home.php +++ b/app/models/home.php @@ -4,6 +4,7 @@ use ReleaseInsights\Data; use ReleaseInsights\ESR; +use ReleaseInsights\Json; use ReleaseInsights\URL; use ReleaseInsights\Utils; use ReleaseInsights\Version; @@ -37,7 +38,7 @@ // Check if we have already shipped a Release Candidate build to the beta channel // Remote balrog API can give a 404, we have a fallback to N/A - $rc_build = Utils::getJson(URL::Balrog->value . 'rules/firefox-beta', 900)['mapping'] ?? 'N/A'; + $rc_build = Json::load(URL::Balrog->value . 'rules/firefox-beta', 900)['mapping'] ?? 'N/A'; if ($rc_build !== 'N/A') { $rc_build = explode('-', (string) $rc_build)[1]; @@ -51,7 +52,7 @@ } // Get the latest nightly build ID, used as a tooltip on the nightly version number -$latest_nightly = Utils::getJson( +$latest_nightly = Json::load( URL::Balrog->value . 'releases/Firefox-mozilla-central-nightly-latest', 900 ); diff --git a/app/models/nightly.php b/app/models/nightly.php index f0c142b..37347c0 100644 --- a/app/models/nightly.php +++ b/app/models/nightly.php @@ -3,7 +3,7 @@ declare(strict_types=1); use BzKarma\Scoring; -use ReleaseInsights\{Bugzilla as Bz, URL, Utils}; +use ReleaseInsights\{Bugzilla as Bz, Json, URL, Utils}; /* We need previous and next days for navigation and changelog @@ -24,7 +24,7 @@ // This is a fallback mechanism for Buildhub which sometimes takes hours to have the latest nightly if (empty($nightlies)) { // Get the latest nightly build ID, used as a tooltip on the nightly version number - $latest_nightly = Utils::getJson( + $latest_nightly = Json::load( URL::Archive->value . 'pub/firefox/nightly/latest-mozilla-central/firefox-' . FIREFOX_NIGHTLY . '.en-US.win64.json', 900 ); @@ -131,7 +131,7 @@ $url = Bz::getBugListLink($bugs); // Bugzilla REST API https://wiki.mozilla.org/Bugzilla:REST_API - $bug_list_details = Utils::getJson(URL::Bugzilla->value . 'rest/bug?include_fields=id,summary,priority,severity,keywords,product,component,type,duplicates,regressions,cf_webcompat_priority,cf_performance_impact,cf_tracking_firefox' . NIGHTLY . ',cf_tracking_firefox' . BETA . ',cf_tracking_firefox' . RELEASE . ',cf_status_firefox' . NIGHTLY . ',cf_status_firefox' . BETA . ',cf_status_firefox' . RELEASE . ',cc,see_also&bug_id=' . implode('%2C', $bugs))['bugs'] ?? []; + $bug_list_details = Json::load(URL::Bugzilla->value . 'rest/bug?include_fields=id,summary,priority,severity,keywords,product,component,type,duplicates,regressions,cf_webcompat_priority,cf_performance_impact,cf_tracking_firefox' . NIGHTLY . ',cf_tracking_firefox' . BETA . ',cf_tracking_firefox' . RELEASE . ',cf_status_firefox' . NIGHTLY . ',cf_status_firefox' . BETA . ',cf_status_firefox' . RELEASE . ',cc,see_also&bug_id=' . implode('%2C', $bugs))['bugs'] ?? []; $bug_list[$dataset['buildid']] = [ 'bugs' => $bug_list_details, diff --git a/app/models/past_release.php b/app/models/past_release.php index a60c781..0700c91 100644 --- a/app/models/past_release.php +++ b/app/models/past_release.php @@ -2,11 +2,11 @@ declare(strict_types=1); -use ReleaseInsights\{Bugzilla as Bz, Release, URL, Utils, Version}; +use ReleaseInsights\{Bugzilla as Bz, Json, Release, URL, Utils, Version}; // Historical data from Product Details -$firefox_releases = Utils::getJson(URL::ProductDetails->value . 'firefox.json')['releases']; -$devedition_releases = Utils::getJson(URL::ProductDetails->value . 'devedition.json')['releases']; +$firefox_releases = Json::load(URL::ProductDetails->value . 'firefox.json')['releases']; +$devedition_releases = Json::load(URL::ProductDetails->value . 'devedition.json')['releases']; $requested_version = Version::get(); if ($requested_version == '14.0') { @@ -150,7 +150,7 @@ // Check current rollout for the release channel if ((int) $requested_version === RELEASE) { - $rollout = Utils::getJson(URL::Balrog->value . 'rules/firefox-release')['backgroundRate']; + $rollout = Json::load(URL::Balrog->value . 'rules/firefox-release')['backgroundRate']; } return [ diff --git a/app/models/pre4_release.php b/app/models/pre4_release.php index f32755d..3d2dd28 100644 --- a/app/models/pre4_release.php +++ b/app/models/pre4_release.php @@ -2,10 +2,10 @@ declare(strict_types=1); -use ReleaseInsights\{URL,Utils, Version}; +use ReleaseInsights\{Json, URL, Utils, Version}; // Historical data from Product Details -$firefox_releases = Utils::getJson(URL::ProductDetails->value . 'firefox.json')['releases']; +$firefox_releases = Json::load(URL::ProductDetails->value . 'firefox.json')['releases']; // Number of dot releases $dot_release_count = count((array) array_filter( diff --git a/composer.json b/composer.json index ae04a76..c8f12bc 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,6 @@ ], "require": { "php": "~8.2.0", - "mozillal10n/json": "~0.1", "twig/twig": "^3.0", "twbs/bootstrap": "^5.2", "components/jquery": "^3.4", diff --git a/tests/Unit/ReleaseInsights/CalendarMonthlyTest.php b/tests/Unit/ReleaseInsights/CalendarMonthlyTest.php index 9e8fbeb..1da8766 100644 --- a/tests/Unit/ReleaseInsights/CalendarMonthlyTest.php +++ b/tests/Unit/ReleaseInsights/CalendarMonthlyTest.php @@ -5,7 +5,6 @@ use ReleaseInsights\CalendarMonthly as CM; test('CalendarMonthly::getMonthsToLastPlannedRelease()', function () { - var_dump(CM::getMonthsToLastPlannedRelease()); expect(CM::getMonthsToLastPlannedRelease()) ->each->toMatch('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/'); }); diff --git a/tests/Unit/ReleaseInsights/JsonTest.php b/tests/Unit/ReleaseInsights/JsonTest.php new file mode 100644 index 0000000..a05b4ae --- /dev/null +++ b/tests/Unit/ReleaseInsights/JsonTest.php @@ -0,0 +1,56 @@ +toBeArray(); + expect(Json::load(__DIR__ . '/../../Files/empty.json')) + ->toBeEmpty() + ->toBeArray(); + expect(Json::load(__DIR__ . '/../../Files/bad.json')) + ->toBeEmpty() + ->toBeArray(); + expect(Json::load(__DIR__ . '/../../Files/iDontExist.json')) + ->toBeEmpty() + ->toBeArray(); +}); + +test('Json::isValid', function () { + $this->assertFalse(Json::isValid('1')); + $this->assertFalse(Json::isValid('[1:]')); +}); + +// Templating function, we capture the output +test('Json->render()', function () { + ob_start(); + (new Json(['aa']))->render(); + $content = ob_get_contents(); + ob_end_clean(); + expect($content) + ->toBeString() + ->toEqual('["aa"]'); + + $obj = new Json(['aa']); + var_dump($obj); + $_GET['callback'] = "myfunc"; + var_dump($obj); + ob_start(); + $obj->render(); + $content = ob_get_contents(); + ob_end_clean(); + expect($content) + ->toBeString() + ->toEqual('myfunc(["aa"])'); + + ob_start(); + (new Json(['error' => 'an error']))->render(); + $content = ob_get_contents(); + ob_end_clean(); + expect($content) + ->toBeString() + ->toEqual( '{ + "error": "an error" +}'); +}); \ No newline at end of file diff --git a/tests/Unit/ReleaseInsights/UtilsTest.php b/tests/Unit/ReleaseInsights/UtilsTest.php index 4346815..13022a6 100644 --- a/tests/Unit/ReleaseInsights/UtilsTest.php +++ b/tests/Unit/ReleaseInsights/UtilsTest.php @@ -63,19 +63,6 @@ unset($_GET['date']); }); -test('Utils::getJson', function () { - expect(U::getJson(__DIR__ . '/../../Files/firefox_versions.json'))->toBeArray(); - expect(U::getJson(__DIR__ . '/../../Files/empty.json')) - ->toBeEmpty() - ->toBeArray(); - expect(U::getJson(__DIR__ . '/../../Files/bad.json')) - ->toBeEmpty() - ->toBeArray(); - expect(U::getJson(__DIR__ . '/../../Files/iDontExist.json')) - ->toBeEmpty() - ->toBeArray(); -}); - test('Utils::mtrim', function ($input, $output) { expect($output)->toEqual(U::mtrim($input)); })->with([ @@ -106,26 +93,6 @@ ['2022-01-10', '2022-01-05', '2022-01-09', false], ]); -// Templating function, we capture the output -test('Utils::renderJson', function () { - ob_start(); - U::renderJson(['aa']); - $content = ob_get_contents(); - ob_end_clean(); - expect($content) - ->toBeString() - ->toEqual('["aa"]'); - - ob_start(); - U::renderJson(['error' => 'an error']); - $content = ob_get_contents(); - ob_end_clean(); - expect($content) - ->toBeString() - ->toEqual( '{ - "error": "an error" -}'); -}); test('Utils::inString', function ($a, $b, $c, $d) { expect(U::inString($a, $b, $c))->toEqual($d); @@ -153,8 +120,3 @@ ->toBeArray() ->not->toBeEmpty(); }); - -test('Utils::isJson', function () { - $this->assertFalse(U::isJson('1')); - $this->assertFalse(U::isJson('[1:]')); -});