Skip to content

Commit

Permalink
Migrate most of tests files to Pest
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed Apr 29, 2024
1 parent 059ddd5 commit 68ba2f2
Show file tree
Hide file tree
Showing 54 changed files with 6,355 additions and 7,559 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codacy_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: vendor/bin/phpunit --coverage-clover clover.xml
run: vendor/bin/pest --coverage-clover clover.xml

- name: Export to Codacy
env:
Expand Down
25 changes: 0 additions & 25 deletions Tests/Datasets/MethodsDatasets.php

This file was deleted.

1 change: 0 additions & 1 deletion Tests/Examples/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use CondorcetPHP\Condorcet\Condorcet;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionProperty;

class ExamplesTest extends TestCase
{
Expand Down
82 changes: 82 additions & 0 deletions Tests/Examples/ReadmeQuickExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);
use CondorcetPHP\Condorcet\{Candidate, Election, Vote};
use CondorcetPHP\Condorcet\Utils\CondorcetUtil;
test('readme quick example', function () {
$myElection1 = new Election;

// Create your own candidate object
$candidate1 = new Candidate('Candidate 1');
$candidate2 = new Candidate('Candidate 2');
$candidate3 = new Candidate('Candidate 3');

// Register your candidates
$myElection1->addCandidate($candidate1);
$myElection1->addCandidate($candidate2);
$myElection1->addCandidate($candidate3);
$candidate4 = $myElection1->addCandidate('Candidate 4');

// Add some votes, by some ways
$myElection1->addVote(
[
$candidate2, // 1
[$candidate1, $candidate4], // 2 - Tie
// Last rank is optionnal. Here it's : $candidate3
]
);

$myElection1->addVote('Candidate 2 > Candidate 3 > Candidate 4 = Candidate 1');

// last rank can also be omitted
$myElection1->parseVotes(
'tagX || Candidate 1 > Candidate 2 = Candidate 4 > Candidate 3 * 4
tagX, tagY || Candidate 3 > Candidate 1 * 3'
);

// Powerfull, it add 7 votes
$myElection1->addVote(new Vote(
[
$candidate4,
$candidate2,
// You can ignore the over. They will be at the last rank in the contexte of each election.
]
));

// Get Result
// Natural Condorcet Winner
$myWinner = $myElection1->getCondorcetWinner();
// Return a candidate object
expect('My winner is ' . $myWinner->getName() . '<br>')->toEqual('My winner is Candidate 1<br>');

// Natural Condorcet Loser
$myLoser = $myElection1->getCondorcetLoser();
// Return a candidate object
expect('My loser is ' . $myLoser->getName())->toEqual('My loser is Candidate 3');

// Schulze Ranking
$myResultBySchulze = $myElection1->getResult('Schulze');

// Return a multi-dimensional array, filled with objects Candidate (multi-dimensional if tie on a rank)
# Echo it easily
expect(CondorcetUtil::format($myResultBySchulze))->toBe([1 => 'Candidate 1', 2 => 'Candidate 2', 3 => 'Candidate 4', 4 => 'Candidate 3']);

// Get Schulze advanced computing data & stats
$mySchulzeStats = $myElection1->getResult('Schulze')->getStats();

// Get Copeland Ranking
$myResultByCopeland = $myElection1->getResult('Copeland');

// Get Pairwise
$myPairwise = $myElection1->getPairwise();

// How long computation time behind us?
$timer = $myElection1->getGlobalTimer();

// SHA-2 checksum and sleep
$myChecksum = $myElection1->getChecksum();
$toStore = serialize($myElection1);
$comeBack = unserialize($toStore);

expect($myChecksum)->toBe($comeBack->getChecksum());
});
58 changes: 58 additions & 0 deletions Tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

use CondorcetPHP\Condorcet\Condorcet;

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

// expect()->extend('toBeOne', function () {
// return $this->toBe(1);
// });

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

// function something()
// {
// // ..
// }


function getMethodList(): array
{
$methods = Condorcet::getAuthMethods(withNonDeterministicMethods: false);
array_walk($methods, static fn(&$m): array => $m = [$m]);

return $methods;
}
88 changes: 0 additions & 88 deletions Tests/ReadmeQuickExampleTest.php

This file was deleted.

12 changes: 12 additions & 0 deletions Tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
//
}
Loading

0 comments on commit 68ba2f2

Please sign in to comment.