rosavox/lib/alveolata/api/test.spec.php

125 lines
3 KiB
PHP
Raw Normal View History

2025-05-23 07:33:29 +00:00
<?php
// require_once(DIR_ALVEOLATA . '/definitions.php');
require_once(DIR_ALVEOLATA . '/api/functions.php');
\alveolata\test\add(
[
'name' => 'alveolata',
'sections' => [
[
'name' => 'api',
'sections' => [
[
'name' => 'register',
'cases' => [
[
'name' => 'throw exception on multiple registration for same name',
'procedure' => function ($assert) {
$subject = \alveolata\api\make();
$assert->crashes(
function () use (&$subject) {
\alveolata\api\register(
$subject,
'foo'
);
\alveolata\api\register(
$subject,
'foo'
);
}
);
}
],
]
],
[
'name' => 'call',
'setup' => function (&$env) {
$subject = \alveolata\api\make();
\alveolata\api\register(
$subject,
'increment',
[
'execution' => function ($version, $environment, $input) {
$output = ($input + 1);
return $output;
},
'restriction' => function ($version, $environment) {
return true;
},
'input_type' => function ($version) {
return [
'kind' => 'integer',
];
},
'output_type' => function ($version) {
return [
'kind' => 'integer',
];
},
]
);
$env['subject'] = $subject;
},
'cases' => [
[
'name' => 'throw exception on malformed input',
'procedure' => function ($assert, &$env) {
// execution & assertions
{
$input = '42';
$assert->crashes(
function () use ($env, $input) {
$output_actual = \alveolata\api\call(
$env['subject'],
'increment',
[
'input' => $input,
'environment' => [],
'checklevel_restriction' => \alveolata\api\enum_checklevel::hard,
'checklevel_input' => \alveolata\api\enum_checklevel::hard,
'checklevel_output' => \alveolata\api\enum_checklevel::hard
]
);
},
\alveolata\api\exception_invalid_input::class
);
}
},
],
[
'name' => 'all_good',
'procedure' => function ($assert, &$env) {
// execution
{
$input = 42;
$output_actual = \alveolata\api\call(
$env['subject'],
'increment',
[
'input' => $input,
'environment' => [],
'checklevel_restriction' => \alveolata\api\enum_checklevel::hard,
'checklevel_input' => \alveolata\api\enum_checklevel::hard,
'checklevel_output' => \alveolata\api\enum_checklevel::hard,
]
);
}
// assertions
{
$output_expected = 43;
$assert->equal($output_actual, $output_expected);
}
},
],
]
],
]
]
]
]
);