Skip to content

Commit

Permalink
fix: bump test apis for phpunit 9+
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Jan 24, 2025
1 parent 9cc0a91 commit 1fe6f2e
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 40 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,js,json,css,scss,feature}]
indent_size = 2
indent_style = space

[composer.json]
indent_size = 4

# Don't perform any clean-up on thirdparty files
[thirdparty/**]
trim_trailing_whitespace = false
insert_final_newline = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tests export-ignore
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/build
.phpunit.result.cache
/framework
/vendor
/.idea
/cache.properties
/cache.properties
composer.lock
/public
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@
}
},
"prefer-stable": true,
"minimum-stability": "dev"
"minimum-stability": "dev",
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/vendor-plugin": true
}
}
}
26 changes: 12 additions & 14 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="CacheInclude Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="CacheInclude Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
14 changes: 12 additions & 2 deletions src/CacheInclude.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,23 @@ public function process($name, $processor, KeyCreatorInterface $keyCreator)

$result = $processor($name);
$type = 'EXPIRE';
} elseif ($this->cache->has($key)) {
} elseif (method_exists($this->cache, 'has') && $this->cache->has($key)) {
$result = $this->cache->get($key);
$type = 'HIT';
} elseif (method_exists($this->cache, 'hasItem') && $this->cache->hasItem($key)) {
$result = $this->cache->get($key);
$type = 'HIT';
} else {
$result = $processor($name);
$this->runIfCacheLockIsFree(function () use ($key, $name, $result, $keyCreator, $config) {
$this->cache->set($key, $result, $this->getExpiry($config));
if (method_exists($this->cache, 'set')) {
$this->cache->set($key, $result, $this->getExpiry($config));
} else if (method_exists($this->cache, 'save')) {
$this->cache->save($key, $result, $this->getExpiry($config));
} else {
throw new RuntimeException('Cache object does not implement set or save method');
}

$this->addStoredKey($name, $key, $keyCreator);
});
$type = 'MISS';
Expand Down
20 changes: 13 additions & 7 deletions tests/CacheIncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Heyday\CacheInclude\Configs\ArrayConfig;
use Heyday\CacheInclude\KeyCreators\KeyCreatorInterface;
use InvalidArgumentException;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use Symfony\Component\Cache\Simple\ArrayCache;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

class CacheIncludeTest extends SapphireTest
{
Expand All @@ -23,9 +24,11 @@ class CacheIncludeTest extends SapphireTest

protected function setUp(): void
{
Injector::inst()->registerService(new ArrayCache(), CacheInterface::class . '.CacheInclude');
$this->keyCreatorMock = $this->getMockBuilder(KeyCreatorInterface::class)
Injector::inst()->registerService(new ArrayAdapter(), CacheInterface::class . '.CacheInclude');

$this->keyCreatorMock = $this->getMockBuilder(KeyCreatorInterface::class)
->getMock();

$this->cacheinclude = new CacheInclude(
new ArrayConfig(array(
'test' => array(
Expand Down Expand Up @@ -66,15 +69,17 @@ public function testCombinedConfig()
$this->cacheinclude->getCombinedConfig('test')
);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The argument $processor must be an instance of ProcessorInterface or a callable
*/


public function testProcessException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The argument $processor must be an instance of ProcessorInterface or a callable');

$this->cacheinclude->process('test', [], $this->keyCreatorMock);
}


public function testProcessDisabled()
{
$this->cacheinclude->setEnabled(false);
Expand All @@ -90,6 +95,7 @@ function () {
);
}


public function testProcess()
{
$this->keyCreatorMock
Expand Down
22 changes: 13 additions & 9 deletions tests/Configs/ArrayConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function setUp(): void
];
}

protected function tearDown()
protected function tearDown(): void
{
$this->testData = null;
}
Expand All @@ -29,32 +29,36 @@ public function testIteration()
$this->assertEquals($this->testData[$key], $val);
}
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Configs are immutable
*/


public function testSet()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Configs are immutable');

$config = new ArrayConfig($this->testData);
$config[2] = 'Three';
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Configs are immutable
*/


public function testUnset()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Configs are immutable');

$config = new ArrayConfig($this->testData);
unset($config[1]);
}


public function testGet()
{
$config = new ArrayConfig($this->testData);
$this->assertEquals('One', $config[0]);
$this->assertEquals('Two', $config[1]);
}


public function testExists()
{
$config = new ArrayConfig($this->testData);
Expand Down
4 changes: 2 additions & 2 deletions tests/Configs/YamlConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use Symfony\Component\Cache\Simple\ArrayCache;
use Symfony\Component\Yaml\Yaml;

class YamlConfigTest extends \PHPUnit_Framework_TestCase
class YamlConfigTest extends \PHPUnit\Framework\TestCase
{
protected $validYamlData;
protected $invalidYamlData;

protected function setUp()
protected function setUp(): void
{
$this->validYamlData = <<<YAML
Something:
Expand Down
5 changes: 5 additions & 0 deletions tests/Configs/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Something:
expires: +1 week
contains:
- Page
- HomePage
4 changes: 2 additions & 2 deletions tests/KeyCreators/ControllerBasedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ControllerBasedTest extends SapphireTest
*/
protected $requestMock;

protected function setUp()
protected function setUp(): void
{
$kernel = Injector::inst()->get(Kernel::class);
$kernel->setEnvironment(Kernel::DEV);
Expand All @@ -39,7 +39,7 @@ protected function setUp()
$this->keyCreator = new ControllerBased($this->controllerMock);
}

protected function tearDown()
protected function tearDown(): void
{
$this->keyCreator = null;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Processors/ViewableDataProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class ViewableDataProcessorTest extends SapphireTest
{
protected $processor;

protected function setUp()
protected function setUp(): void
{
$this->processor = new ViewableDataProcessor;
}

protected function tearDown()
protected function tearDown(): void
{
$this->processor = null;
}
Expand Down

0 comments on commit 1fe6f2e

Please sign in to comment.