-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from elecena/NanoScript/add-tests
NanoScript: add tests
- Loading branch information
Showing
8 changed files
with
177 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ composer.phar | |
.phpunit.result.cache | ||
.phpunit.cache/ | ||
|
||
log/* | ||
vendor/ | ||
.idea/ | ||
.php-cs-fixer.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
use Nano\Logger\NanoLogger; | ||
|
||
class FailingTestScript extends NanoScript | ||
{ | ||
private ?array $foo = null; | ||
|
||
protected function init(): int | ||
{ | ||
// this will throw an exception | ||
return count($this->foo); | ||
} | ||
|
||
public function run() | ||
{ | ||
} | ||
} | ||
|
||
class WorkingTestScript extends FailingTestScript | ||
{ | ||
public static ?Throwable $throw = null; | ||
|
||
protected function init(): int | ||
{ | ||
return 0; | ||
} | ||
|
||
/** | ||
* @return int | ||
* @throws Throwable | ||
*/ | ||
public function run(): int | ||
{ | ||
if (self::$throw) { | ||
throw self::$throw; | ||
} | ||
|
||
$this->logger->info('Hi!'); | ||
return 42; | ||
} | ||
} | ||
|
||
class NanoScriptTest extends \Nano\NanoBaseTest | ||
{ | ||
public function setUp(): void | ||
{ | ||
WorkingTestScript::$throw = null; | ||
} | ||
|
||
public function testFailingTestScriptInitException() | ||
{ | ||
// register a global logging handler for easier testing | ||
$handler = new Nano\TestLoggingHandler(ident: 'foo'); | ||
NanoLogger::pushHandler($handler); | ||
|
||
try { | ||
Nano::script(__DIR__ . '/..', FailingTestScript::class); | ||
} catch (Throwable) { | ||
} | ||
|
||
// our init method failed, there should be a log message about it | ||
$this->assertInstanceOf(\Monolog\LogRecord::class, $handler->lastRecord); | ||
$this->assertEquals(\Monolog\Level::Error, $handler->lastRecord->level); | ||
|
||
$this->assertEquals('FailingTestScript::init() failed', $handler->lastRecord->message); | ||
$this->assertInstanceOf(TypeError::class, $handler->lastRecord->context['exception']); | ||
} | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
public function testWorkingTestScript() | ||
{ | ||
$instance = Nano::script(__DIR__ . '/..', WorkingTestScript::class); | ||
$this->assertInstanceOf(WorkingTestScript::class, $instance); | ||
$this->assertEquals(42, $instance->run()); | ||
|
||
// make the run() method throw an exception | ||
$ex = new Exception('foo'); | ||
WorkingTestScript::$throw = $ex; | ||
$this->expectExceptionObject($ex); | ||
$instance->run(); | ||
} | ||
|
||
public function testWorkingTestScriptRunAndCatchException() | ||
{ | ||
// make the run() method throw an exception | ||
$ex = new Exception('foo'); | ||
WorkingTestScript::$throw = $ex; | ||
|
||
// register a global logging handler for easier testing | ||
$handler = new Nano\TestLoggingHandler(ident: 'foo'); | ||
NanoLogger::pushHandler($handler); | ||
|
||
try { | ||
Nano::script(__DIR__ . '/..', WorkingTestScript::class)->runAndCatch(); | ||
} catch(Throwable $e) { | ||
$this->assertEquals($ex->getMessage(), $e->getMessage()); | ||
} | ||
|
||
// assert the logging | ||
$this->assertEquals('WorkingTestScript::run() failed', $handler->lastRecord->message); | ||
$this->assertEquals(Monolog\Level::Error, $handler->lastRecord->level); | ||
$this->assertEquals($ex->getMessage(), $handler->lastRecord->context['exception']['message']); | ||
} | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
public function testWorkingTestScriptRunAndCatchReturnsTheValue() | ||
{ | ||
// register a global logging handler for easier testing | ||
$handler = new Nano\TestLoggingHandler(ident: 'foo'); | ||
NanoLogger::pushHandler($handler); | ||
|
||
$ret = Nano::script(__DIR__ . '/..', WorkingTestScript::class)->runAndCatch(); | ||
$this->assertEquals(42, $ret); | ||
|
||
// assert the logging | ||
$this->assertEquals('Hi!', $handler->lastRecord->message); | ||
$this->assertEquals(Monolog\Level::Info, $handler->lastRecord->level); | ||
$this->assertEquals(WorkingTestScript::class, $handler->lastRecord->extra['script_class']); | ||
} | ||
} |