1
0
This commit is contained in:
2022-09-21 14:01:45 +02:00
parent 0a30f39eb4
commit cfee4d93d0
349 changed files with 4 additions and 4 deletions

View File

@ -0,0 +1,190 @@
<?php
declare(strict_types=1);
namespace CondorcetPHP\Condorcet\Tests\Utils;
use CondorcetPHP\Condorcet\Throwable\VoteInvalidFormatException;
use CondorcetPHP\Condorcet\Tools\Converters\CondorcetElectionFormat;
use CondorcetPHP\Condorcet\Utils\VoteEntryParser;
use PHPUnit\Framework\TestCase;
class VoteEntryParserTest extends TestCase
{
/**
* @dataProvider voteBadNumericValueProvider()
*/
public function testBadNumericValue(string $entry, string $message): void
{
$this->expectException(VoteInvalidFormatException::class);
$this->expectExceptionMessage($message);
new VoteEntryParser($entry);
}
public function voteBadNumericValueProvider(): iterable
{
yield [
'entry' => 'A>B ^g',
'message' => "the value 'g' is not an integer.",
];
yield [
'entry' => 'A>B ^a*b',
'message' => "the value 'b' is not an integer.",
];
yield [
'entry' => 'A>B*b',
'message' => "the value 'b' is not an integer.",
];
yield [
'entry' => 'A>B ^4Y ',
'message' => "the value '4Y' is not an integer.",
];
}
/**
* @dataProvider voteEntriesProvider()
*/
public function testVotesEntries(string $entry, array $expected): void
{
$parser = new VoteEntryParser($entry);
self::assertSame($entry, $parser->originalEntry);
self::assertSame($expected['comment'], $parser->comment);
self::assertSame($expected['multiple'], $parser->multiple);
self::assertSame($expected['ranking'], $parser->ranking);
self::assertSame($expected['tags'], $parser->tags);
self::assertSame($expected['weight'], $parser->weight);
}
public function voteEntriesProvider(): iterable
{
yield [
'entry' => 'A >B = C>D',
'expected' => [
'comment' => null,
'multiple' => 1,
'ranking' => [
['A'],
['B', 'C'],
['D'],
],
'tags' => null,
'weight' => 1,
],
];
yield [
'entry' => 'tag1, tag2 || A >B = C>D ^ 7 * 42 # One Comment',
'expected' => [
'comment' => 'One Comment',
'multiple' => 42,
'ranking' => [
['A'],
['B', 'C'],
['D'],
],
'tags' => ['tag1', 'tag2'],
'weight' => 7,
],
];
yield [
'entry' => 'A >B = C>D *42#One Comment',
'expected' => [
'comment' => 'One Comment',
'multiple' => 42,
'ranking' => [
['A'],
['B', 'C'],
['D'],
],
'tags' => null,
'weight' => 1,
],
];
yield [
'entry' => 'A^ 7#',
'expected' => [
'comment' => '',
'multiple' => 1,
'ranking' => [
['A'],
],
'tags' => null,
'weight' => 7,
],
];
yield [
'entry' => ' ',
'expected' => [
'comment' => null,
'multiple' => 1,
'ranking' => null,
'tags' => null,
'weight' => 1,
],
];
yield [
'entry' => ' tag1,tag2|| ',
'expected' => [
'comment' => null,
'multiple' => 1,
'ranking' => null,
'tags' => ['tag1', 'tag2'],
'weight' => 1,
],
];
yield [
'entry' => '^7*4 ',
'expected' => [
'comment' => null,
'multiple' => 4,
'ranking' => null,
'tags' => null,
'weight' => 7,
],
];
yield [
'entry' => ' #One Comment',
'expected' => [
'comment' => 'One Comment',
'multiple' => 1,
'ranking' => null,
'tags' => null,
'weight' => 1,
],
];
yield [
'entry' => 'tag1,tag2||'.CondorcetElectionFormat::SPECIAL_KEYWORD_EMPTY_RANKING.'^7*42#FeteDuDindon',
'expected' => [
'comment' => 'FeteDuDindon',
'multiple' => 42,
'ranking' => [],
'tags' => ['tag1', 'tag2'],
'weight' => 7,
],
];
yield [
'entry' => ' '.CondorcetElectionFormat::SPECIAL_KEYWORD_EMPTY_RANKING.' ',
'expected' => [
'comment' => null,
'multiple' => 1,
'ranking' => [],
'tags' => null,
'weight' => 1,
],
];
}
}

View File

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace CondorcetPHP\Condorcet\Tests\Utils;
use CondorcetPHP\Condorcet\Utils\VoteUtil;
use CondorcetPHP\Condorcet\Throwable\VoteInvalidFormatException;
use PHPUnit\Framework\TestCase;
class VoteUtilTest extends TestCase
{
public function testTypeMismatchTagsThrowAnException(): never
{
$this->expectException(VoteInvalidFormatException::class);
$this->expectExceptionMessage('The format of the vote is invalid: every tag must be of type string, array given');
VoteUtil::tagsConvert(['not', 'a', 'string:', []]);
}
public function testEmptyTagsThrowAnException(): never
{
$this->expectException(VoteInvalidFormatException::class);
$this->expectExceptionMessage('The format of the vote is invalid: found empty tag');
VoteUtil::tagsConvert('an , empty, tag , , in, the, middle');
}
/**
* @dataProvider tagsProvider()
*/
public function testTagsGetConverted($tags, $expected): void
{
$this->assertSame($expected, VoteUtil::tagsConvert($tags));
}
public function testGetRankingAsString(): void
{
// Empty ranking
$this->assertEquals('', VoteUtil::getRankingAsString([]));
// String ranking
$this->assertEquals('A > B > C', VoteUtil::getRankingAsString(['A', 'B', 'C']));
// Array ranking
$this->assertEquals('A = B > C', VoteUtil::getRankingAsString([['A', 'B'], 'C']));
// Unsorted array ranking
$this->assertEquals('A = B > C', VoteUtil::getRankingAsString([['B', 'A'], 'C']));
}
public function tagsProvider(): iterable
{
yield 'null tags' => [
'tags' => null,
'expected' => null,
];
yield 'empty string' => [
'tags' => '',
'expected' => null,
];
yield 'empty array' => [
'tags' => [],
'expected' => null,
];
yield 'tags as string' => [
'tags' => 'these, are, some , tags',
'expected' => ['these', 'are', 'some', 'tags'],
];
yield 'tags as array' => [
'tags' => ['these', 'are', 'some', 'more', 'tags'],
'expected' => ['these', 'are', 'some', 'more', 'tags'],
];
}
}