Files
laya-php-sdk/tests/Unit/QuestionTest.php
T

56 lines
1.8 KiB
PHP
Raw Normal View History

2026-10-17 02:01:43 +03:00
<?php
use Laya\Question\ChoiceQuestion;
use Laya\Question\QuestionSet;
use Laya\Question\ScoreQuestion;
use Laya\Question\YesNoQuestion;
it('serialises a choice question', function () {
$q = (new ChoiceQuestion('category', 'Which?'))->option('a', 'First')->option('b', 'Second');
expect($q->name())->toBe('category')
->and($q->toArray())->toBe([
'type' => 'choice',
'instructions' => 'Which?',
'criteria' => ['a' => 'First', 'b' => 'Second'],
]);
});
it('serialises a score question', function () {
$q = new ScoreQuestion('urgency', 'How urgent?', ['low', 'high']);
expect($q->toArray())->toBe([
'type' => 'score',
'instructions' => 'How urgent?',
'criteria' => ['low', 'high'],
]);
});
it('serialises a yes/no question using the noul wire type', function () {
expect((new YesNoQuestion('angry', 'Upset?'))->toArray())
->toBe(['type' => 'noul', 'instructions' => 'Upset?']);
});
it('keys a question set by question name', function () {
$set = QuestionSet::make()
->add(new YesNoQuestion('a', 'A?'))
->add(new YesNoQuestion('b', 'B?'));
expect(array_keys($set->toArray()))->toBe(['a', 'b']);
});
it('replaces an earlier question with the same name', function () {
$set = QuestionSet::make()
->add(new YesNoQuestion('a', 'first'))
->add(new YesNoQuestion('a', 'second'));
expect($set->toArray())->toHaveCount(1)
->and($set->toArray()['a']['instructions'])->toBe('second');
});
it('encodes a choice question without options as a JSON object', function () {
$json = json_encode((new ChoiceQuestion('c', 'x'))->toArray());
expect($json)->toContain('"criteria":{}');
})->todo('CODE_REVIEW #7: empty criteria currently encodes as []');