Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Dec 27, 2023
1 parent 9bf29cc commit 9fc741d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 38 deletions.
24 changes: 12 additions & 12 deletions tests/Cms/Files/FileActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FileActionsTest extends TestCase
public function setUp(): void
{
Dir::make($this->tmp = __DIR__ . '/tmp');
$this->app = $this->app();
$this->app = static::app();
}

public function tearDown(): void
Expand All @@ -25,11 +25,11 @@ public function tearDown(): void
Dir::remove($this->tmp);
}

public function app()
public static function app()
{
return new App([
'roots' => [
'index' => $this->tmp
'index' => __DIR__ . '/tmp'
],
'site' => [
'children' => [
Expand Down Expand Up @@ -58,9 +58,9 @@ public function app()
]);
}

public function appWithLanguages()
public static function appWithLanguages()
{
return $this->app()->clone([
return static::app()->clone([
'languages' => [
[
'code' => 'en',
Expand All @@ -75,19 +75,19 @@ public function appWithLanguages()
]);
}

public function parentProvider()
public static function parentProvider()
{
$app = $this->app();
$app = static::app();

return [
[$app->site()],
[$app->site()->children()->first()]
];
}

public function fileProvider()
public static function fileProvider(): array
{
$app = $this->app();
$app = static::app();

return [
[$app->site()->file()],
Expand Down Expand Up @@ -118,9 +118,9 @@ public function testChangeName(File $file)
$this->assertFileDoesNotExist($file->storage()->contentFile('published', 'default'));
}

public function fileProviderMultiLang()
public static function fileProviderMultiLang(): array
{
$app = $this->appWithLanguages();
$app = static::appWithLanguages();

return [
[$app->site()->file()],
Expand All @@ -133,7 +133,7 @@ public function fileProviderMultiLang()
*/
public function testChangeNameMultiLang(File $file)
{
$app = $this->appWithLanguages();
$app = static::appWithLanguages();
$app->impersonate('kirby');

// create an empty dummy file
Expand Down
17 changes: 10 additions & 7 deletions tests/Cms/Helpers/HelperFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Kirby\Cms;

use Exception;
use Kirby\Cms\App as Kirby;
use Kirby\Filesystem\Asset;
use Kirby\Filesystem\Dir;
use Kirby\Image\QrCode;
use Kirby\Toolkit\Collection;
use Kirby\Toolkit\Obj;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Error\Deprecated;

class HelperFunctionsTest extends TestCase
{
Expand Down Expand Up @@ -189,17 +189,20 @@ public function testCssWithArray()

public function testDeprecated()
{
// the deprecation warnings are always triggered in testing mode,
// so we cannot test it with disabled debug mode
set_error_handler(
static function ($errno, $errstr) {
restore_error_handler();
throw new Exception($errstr, $errno);
},
E_USER_DEPRECATED
);

try {
deprecated('The xyz method is deprecated.');
} catch (Deprecated $e) {
Assert::fail('Expected deprecation warning was not generated');
} catch (Exception $e) {
$this->assertSame('The xyz method is deprecated.', $e->getMessage());
return;
}

Assert::fail('Expected deprecation warning was not generated');
}

public function testDumpOnCli()
Expand Down
67 changes: 49 additions & 18 deletions tests/Cms/Helpers/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kirby\Cms;

use Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\Obj;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -40,55 +41,78 @@ public function tearDown(): void
*/
public function testDeprecated()
{
// the deprecation warnings are always triggered in testing mode,
// so we cannot test it with disabled debug mode
set_error_handler(
static function ($errno, $errstr) {
restore_error_handler();
throw new Exception($errstr, $errno);
},
E_USER_DEPRECATED
);

try {
Helpers::deprecated('The xyz method is deprecated.');
} catch (Deprecated $e) {
Assert::fail('Expected deprecation warning was not generated');
} catch (Exception $e) {
$this->assertSame('The xyz method is deprecated.', $e->getMessage());
return;
}

Assert::fail('Expected deprecation warning was not generated');
}

/**
* @covers ::deprecated
*/
public function testDeprecatedKeyUndefined()
{
set_error_handler(
static function ($errno, $errstr) {
restore_error_handler();
throw new Exception($errstr, $errno);
},
E_USER_DEPRECATED
);

try {
Helpers::deprecated('The xyz method is deprecated.', 'my-key');
} catch (Deprecated $e) {
Assert::fail('Expected deprecation warning was not generated');
} catch (Exception $e) {
$this->assertSame('The xyz method is deprecated.', $e->getMessage());
return;
}

Assert::fail('Expected deprecation warning was not generated');
}

/**
* @covers ::deprecated
*/
public function testDeprecatedActivated()
{
set_error_handler(
static function ($errno, $errstr) {
restore_error_handler();
throw new Exception($errstr, $errno);
},
E_USER_DEPRECATED
);

try {
Helpers::$deprecations = ['my-key' => true];
Helpers::deprecated('The xyz method is deprecated.', 'my-key');
} catch (Deprecated $e) {
Assert::fail('Expected deprecation warning was not generated');
} catch (Exception $e) {
$this->assertSame('The xyz method is deprecated.', $e->getMessage());
return;
}

Assert::fail('Expected deprecation warning was not generated');
}

/**
* @covers ::deprecated
*/
public function testDeprecatedKeyDeactivated()
{
set_error_handler(
static function ($errno, $errstr) {
restore_error_handler();
throw new Exception($errstr, $errno);
},
E_USER_DEPRECATED
);

Helpers::$deprecations = ['my-key' => false];
$this->assertFalse(Helpers::deprecated('The xyz method is deprecated.', 'my-key'));
}
Expand Down Expand Up @@ -211,6 +235,14 @@ public function testHandleErrorsWarningCaughtCallbackValue()
*/
public function testHandleErrorsWarningNotCaught()
{
set_error_handler(
static function ($errno, $errstr) {
restore_error_handler();
throw new Exception($errstr, $errno);
},
E_WARNING
);

try {
Helpers::handleErrors(
fn () => trigger_error('Some warning', E_USER_WARNING),
Expand All @@ -223,12 +255,11 @@ function (int $errno, string $errstr) {
},
'handled'
);
} catch (Warning $e) {

Assert::fail('Expected warning was not generated');
} catch (Exception $e) {
$this->assertSame('Some warning', $e->getMessage());
return;
}

Assert::fail('Expected warning was not generated');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Session/FileSessionStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function tearDown(): void
}

Dir::remove($this->root);
$this->assertDirectoryNotExists($this->root);
$this->assertDirectoryDoesNotExist($this->root);
}

/**
Expand Down
File renamed without changes.

0 comments on commit 9fc741d

Please sign in to comment.