Skip to content

Commit

Permalink
Add helper method that was missing to get the transient. (#130)
Browse files Browse the repository at this point in the history
* 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 fd919e4.

* Add WordPress' wp-load.php

* Update phpunit xml for coverage.
  • Loading branch information
thefrosty authored Mar 5, 2024
1 parent 59278a8 commit da6a906
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<clover outputFile="./tests/clover.xml"/>
</report>
</coverage>
<php>
<ini name="error_reporting" value="24575"/>
Expand All @@ -23,4 +20,7 @@
<directory phpVersion="8.1" phpVersionOperator=">=" suffix="Test.php">./tests/unit</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="./tests/clover.xml"/>
</logging>
</phpunit>
31 changes: 31 additions & 0 deletions src/Api/TransientsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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).
Expand All @@ -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];
}
}
64 changes: 64 additions & 0 deletions tests/unit/Api/TransientsTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace TheFrosty\WpUtilities\Tests\Api;

use TheFrosty\WpUtilities\Api\TransientsTrait;
use TheFrosty\WpUtilities\Tests\Plugin\Framework\TestCase;

/**
* Trait TransientsTraitTest
* @package TheFrosty\WpUtilities\Tests\Api
*/
class TransientsTraitTest extends TestCase
{
private $transientsTrait;

protected function setUp(): void
{
$this->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));
}
}
3 changes: 2 additions & 1 deletion tests/unit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

require_once dirname( dirname( __DIR__ ) ) . '/vendor/autoload.php';
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
require_once dirname(__DIR__, 2) . '/wordpress/wp-load.php';
require_once __DIR__ . '/Plugin/Framework/Mock/HookProvider.php';
require_once __DIR__ . '/Plugin/Framework/TestCase.php';

0 comments on commit da6a906

Please sign in to comment.