From da6a906161830ac12dee8f00f63dc75a3ca8d377 Mon Sep 17 00:00:00 2001 From: Austin Passy <367897+thefrosty@users.noreply.github.com> Date: Tue, 5 Mar 2024 10:28:50 -0800 Subject: [PATCH] Add helper method that was missing to get the transient. (#130) * Add helper method that was missing to get the transient. * Add a transients timeout helper method. * Update codecov/codecov-action to v4 to resolve deprecated node v16. * Add PHPUnit test for TransientsTrait. * Update tests. * Add WordPress stubs. * Update PHPUnit bootstrap * Revert "Update PHPUnit bootstrap" This reverts commit fd919e4999fb683318e9537fe95e1be358e8f2a2. * Add WordPress' wp-load.php * Update phpunit xml for coverage. --- .github/workflows/main.yml | 2 +- composer.json | 3 +- phpunit.xml | 6 +-- src/Api/TransientsTrait.php | 31 +++++++++++++ tests/unit/Api/TransientsTraitTest.php | 64 ++++++++++++++++++++++++++ tests/unit/bootstrap.php | 3 +- 6 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 tests/unit/Api/TransientsTraitTest.php diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4af5a8c..9c06f9e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -103,7 +103,7 @@ jobs: - name: Upload coverage to Codecov if: ${{ github.event_name == 'pull_request' }} - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 with: fail_ci_if_error: false diff --git a/composer.json b/composer.json index 80c190a..29b7eef 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "require-dev": { "ext-simplexml": "*", "dealerdirect/phpcodesniffer-composer-installer": "^1.0.0", + "php-stubs/wordpress-stubs": "^6.4", "phpcompatibility/php-compatibility": "^9.3", "phpmd/phpmd": "^2.13", "phpunit/php-code-coverage": "^10", @@ -43,8 +44,8 @@ "slevomat/coding-standard": "^8.8", "squizlabs/php_codesniffer": "^3.9", "szepeviktor/phpstan-wordpress": "^1.0", - "wp-phpunit/wp-phpunit": "^6.4", "wp-coding-standards/wpcs": "dev-develop", + "wp-phpunit/wp-phpunit": "^6.4", "yoast/phpunit-polyfills": "^2.0" }, "suggest": { diff --git a/phpunit.xml b/phpunit.xml index c689248..9130d9c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -11,9 +11,6 @@ ./src - - - @@ -23,4 +20,7 @@ ./tests/unit + + + diff --git a/src/Api/TransientsTrait.php b/src/Api/TransientsTrait.php index 33e8580..b464c9a 100644 --- a/src/Api/TransientsTrait.php +++ b/src/Api/TransientsTrait.php @@ -4,6 +4,8 @@ namespace TheFrosty\WpUtilities\Api; +use function get_transient; +use function is_numeric; use function md5; use function set_transient; use function strlen; @@ -44,6 +46,17 @@ public function getTransientKey(string $input, ?string $key_prefix = null): stri } /** + * Get the transient value. + * @param string $transient Transient name. + * @return mixed + */ + public function getTransient(string $transient): mixed + { + return get_transient($transient); + } + + /** + * Set the transient value. * @param string $transient Transient name. Expected to not be SQL-escaped. Must be 172 characters or fewer. * @param mixed $value Transient value. Must be serializable if non-scalar. Expected to not be SQL-escaped. * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). @@ -53,4 +66,22 @@ public function setTransient(string $transient, mixed $value, int $expiration = { return set_transient($transient, $value, $expiration); } + + /** + * Get the transient timeout value. + * @param string $transient + * @return array|null + */ + public function getTransientTimeout(string $transient): ?int + { + global $wpdb; + $timeout = $wpdb->get_col( + " +SELECT option_value +FROM $wpdb->options +WHERE option_name +LIKE '%_transient_timeout_$transient%'" + ); + return !isset($timeout[0]) || !is_numeric($timeout[0]) ? null : (int)$timeout[0]; + } } diff --git a/tests/unit/Api/TransientsTraitTest.php b/tests/unit/Api/TransientsTraitTest.php new file mode 100644 index 0000000..e12c952 --- /dev/null +++ b/tests/unit/Api/TransientsTraitTest.php @@ -0,0 +1,64 @@ +transientsTrait = new class() { + use TransientsTrait; + }; + $this->reflection = $this->getReflection($this->transientsTrait); + } + + public function testGetTransientKey(): void + { + $input = 'example_input'; + $keyPrefix = 'prefix_'; + $wp_max_transient_chars = $this->reflection->getProperty('wp_max_transient_chars'); + $wp_max_transient_chars->setAccessible(true); + $expectedKey = 'prefix_' . substr( + md5($input), + 0, + $wp_max_transient_chars->getValue($this->transientsTrait) - strlen($keyPrefix) + ); + + $this->assertEquals($expectedKey, $this->transientsTrait->getTransientKey($input, $keyPrefix)); + } + + public function testGetTransient(): void + { + $transientName = 'example_transient'; + $expectedValue = 'example_value'; + + $this->assertEquals($expectedValue, $this->transientsTrait->getTransient($transientName)); + } + + public function testSetTransient(): void + { + $transientName = 'example_transient'; + $value = 'example_value'; + $expiration = 3600; // 1 hour + + $this->assertTrue($this->transientsTrait->setTransient($transientName, $value, $expiration)); + } + + public function testGetTransientTimeout(): void + { + $transientName = 'example_transient'; + + $this->assertEquals(null, $this->transientsTrait->getTransientTimeout($transientName)); + } +} diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index b2faa15..b23e5fb 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -1,5 +1,6 @@