Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Update PhpUnit #3405

Draft
wants to merge 9 commits into
base: 8.3
Choose a base branch
from
87 changes: 42 additions & 45 deletions Neos.Cache/Tests/Functional/Backend/PdoBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Neos\Cache\EnvironmentConfiguration;
use Neos\Cache\Tests\BaseTestCase;
use Neos\Cache\Frontend\FrontendInterface;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Testcase for the PDO cache backend
Expand All @@ -33,12 +35,16 @@ class PdoBackendTest extends BaseTestCase
/**
* @var PdoBackend[]
*/
private $backends = [];
private array $backends = [];

/**
* @var \PHPUnit\Framework\MockObject\MockObject|FrontendInterface
*/
private $cache;
private FrontendInterface|MockObject $cache;

protected function setUp(): void
{
$this->cache = $this->createMock(FrontendInterface::class);
$this->cache->method('getIdentifier')->willReturn('TestCache');
$this->setupBackends();
}

protected function tearDown(): void
{
Expand All @@ -47,49 +53,43 @@ protected function tearDown(): void
}
}

public function backendsToTest(): array
{
$this->cache = $this->createMock(FrontendInterface::class);
$this->cache->method('getIdentifier')->willReturn('TestCache');
$this->setupBackends();
return $this->backends;
}

/**
* @test
* @dataProvider backendsToTest
*/
public function setAddsCacheEntry(BackendInterface $backend): void
public function setAddsCacheEntry(): void
{
$backend->flush();
foreach ($this->backends as $backend) {
$backend->flush();

// use data that contains binary junk
$data = random_bytes(2048);
$backend->set('some_entry', $data);
self::assertEquals($data, $backend->get('some_entry'));
// use data that contains binary junk
$data = random_bytes(2048);
$backend->set('some_entry', $data);
self::assertEquals($data, $backend->get('some_entry'));
}
}

/**
* @test
* @dataProvider backendsToTest
*/
public function cacheEntriesCanBeIterated(BackendInterface $backend): void
public function cacheEntriesCanBeIterated(): void
{
$backend->flush();

// use data that contains binary junk
$data = random_bytes(128);
$backend->set('first_entry', $data);
$backend->set('second_entry', $data);
$backend->set('third_entry', $data);

$entries = 0;
foreach ($backend as $entry) {
self::assertEquals($data, $entry);
$entries++;
}
foreach ($this->backends as $backend) {
$backend->flush();

// use data that contains binary junk
$data = random_bytes(128);
$backend->set('first_entry', $data);
$backend->set('second_entry', $data);
$backend->set('third_entry', $data);

self::assertEquals(3, $entries);
$entries = 0;
foreach ($backend as $entry) {
self::assertEquals($data, $entry);
$entries++;
}

self::assertEquals(3, $entries);
}
}

private function setupBackends(): void
Expand All @@ -105,9 +105,8 @@ private function setupBackends(): void
$backend->setup();
$backend->setCache($this->cache);
$backend->flush();
$this->backends['sqlite'] = [$backend];
} catch (\Throwable $t) {
$this->addWarning('SQLite DB is not reachable: ' . $t->getMessage());
$this->backends['sqlite'] = $backend;
} catch (\Throwable) {
}

try {
Expand All @@ -123,9 +122,8 @@ private function setupBackends(): void
$backend->setup();
$backend->setCache($this->cache);
$backend->flush();
$this->backends['mysql'] = [$backend];
} catch (\Throwable $t) {
$this->addWarning('MySQL DB server is not reachable: ' . $t->getMessage());
$this->backends['mysql'] = $backend;
} catch (\Throwable) {
}

try {
Expand All @@ -141,9 +139,8 @@ private function setupBackends(): void
$backend->setup();
$backend->setCache($this->cache);
$backend->flush();
$this->backends['pgsql'] = [$backend];
} catch (\Throwable $t) {
$this->addWarning('PostgreSQL DB server is not reachable: ' . $t->getMessage());
$this->backends['pgsql'] = $backend;
} catch (\Throwable) {
}
}
}
2 changes: 1 addition & 1 deletion Neos.Cache/Tests/Functional/Backend/RedisBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function setUp(): void
['hostname' => '127.0.0.1', 'database' => 0]
);
$this->cache = $this->createMock(FrontendInterface::class);
$this->cache->expects(self::any())->method('getIdentifier')->will(self::returnValue('TestCache'));
$this->cache->expects($this->any())->method('getIdentifier')->willReturn(('TestCache'));
$this->backend->setCache($this->cache);
$this->backend->flush();
}
Expand Down
38 changes: 17 additions & 21 deletions Neos.Cache/Tests/Unit/Backend/AbstractBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,24 @@ class AbstractBackendTest extends BaseTestCase
*/
protected function setUp(): void
{
class_exists(AbstractBackend::class);
$className = 'ConcreteBackend_' . md5(uniqid(mt_rand(), true));
eval('
class ' . $className . ' extends \Neos\Cache\Backend\AbstractBackend {
public function set(string $entryIdentifier, string $data, array $tags = [], int $lifetime = NULL): void {}
public function get(string $entryIdentifier): string {}
public function has(string $entryIdentifier): bool {}
public function remove(string $entryIdentifier): bool {}
public function flush(): void {}
public function flushByTag(string $tag): int {}
public function flushByTags(array $tags): int {}
public function findIdentifiersByTag(string $tag): array {}
public function collectGarbage(): void {}
public function setSomeOption($value) {
$this->someOption = $value;
}
public function getSomeOption() {
return $this->someOption;
}
$this->backend = new class (new EnvironmentConfiguration('Ultraman Neos Testing', '/some/path', PHP_MAXPATHLEN)) extends \Neos\Cache\Backend\AbstractBackend {
protected $someOption;
public function set(string $entryIdentifier, string $data, array $tags = [], int $lifetime = NULL): void {}
public function get(string $entryIdentifier): string {}
public function has(string $entryIdentifier): bool {}
public function remove(string $entryIdentifier): bool {}
public function flush(): void {}
public function flushByTag(string $tag): int {}
public function flushByTags(array $tags): int {}
public function findIdentifiersByTag(string $tag): array {}
public function collectGarbage(): void {}
public function setSomeOption($value) {
$this->someOption = $value;
}
');
$this->backend = new $className(new EnvironmentConfiguration('Ultraman Neos Testing', '/some/path', PHP_MAXPATHLEN));
public function getSomeOption() {
return $this->someOption;
}
};
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Neos.Cache/Tests/Unit/Backend/ApcuBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ public function flushRemovesAllCacheEntries()
public function flushRemovesOnlyOwnEntries()
{
$thisCache = $this->createMock(FrontendInterface::class);
$thisCache->expects(self::any())->method('getIdentifier')->will(self::returnValue('thisCache'));
$thisCache->expects($this->any())->method('getIdentifier')->willReturn(('thisCache'));
$thisBackend = new ApcuBackend($this->getEnvironmentConfiguration(), []);
$thisBackend->setCache($thisCache);

$thatCache = $this->createMock(FrontendInterface::class);
$thatCache->expects(self::any())->method('getIdentifier')->will(self::returnValue('thatCache'));
$thatCache->expects($this->any())->method('getIdentifier')->willReturn(('thatCache'));
$thatBackend = new ApcuBackend($this->getEnvironmentConfiguration(), []);
$thatBackend->setCache($thatCache);

Expand Down
Loading
Loading