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 May 1, 2024
1 parent 059ddd5 commit 0c0fbde
Show file tree
Hide file tree
Showing 59 changed files with 7,165 additions and 8,182 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
16 changes: 16 additions & 0 deletions Tests/AlgoTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Tests;

use CondorcetPHP\Condorcet\Tools\Converters\{DavidHillFormat, DebianFormat};
use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class AlgoTestCase extends BaseTestCase
{
public static DavidHillFormat $tidemanA77;
public static DebianFormat $debian2020;
public static DebianFormat $debian2007;
public static DebianFormat $debian2006;
}
25 changes: 0 additions & 25 deletions Tests/Datasets/MethodsDatasets.php

This file was deleted.

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

declare(strict_types=1);

namespace Tests;

use CondorcetPHP\Condorcet\Election;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;

#[RequiresPhpExtension('pdo_sqlite')]
class DriversTestCase extends TestCase
{
protected function hashVotesList(Election $elec): string
{
$c = 0;
$voteCompil = '';
foreach ($elec->getVotesManager() as $oneVote) {
$c++;
$voteCompil .= (string) $oneVote;
}

return $c . '||' . hash('md5', $voteCompil);
}
}
80 changes: 30 additions & 50 deletions Tests/Examples/ExamplesTest.php
Original file line number Diff line number Diff line change
@@ -1,63 +1,43 @@
<?php

declare(strict_types=1);

namespace CondorcetPHP\Condorcet\Tests\Examples;

use CondorcetPHP\Condorcet\Condorcet;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionProperty;

class ExamplesTest extends TestCase
{
protected static string $condorcetDefaultMethod;

public static function setUpBeforeClass(): void
{
self::$condorcetDefaultMethod = Condorcet::getDefaultMethod();
}

protected function tearDown(): void
{
Condorcet::$UseTimer = (new ReflectionClass(Condorcet::class))->getProperty('UseTimer')->getDefaultValue();
Condorcet::setDefaultMethod(self::$condorcetDefaultMethod);
beforeEach(function () {
$this->condorcetDefaultMethod = Condorcet::getDefaultMethod();
});
afterEach(function () {
Condorcet::$UseTimer = (new ReflectionClass(Condorcet::class))->getProperty('UseTimer')->getDefaultValue();
Condorcet::setDefaultMethod($this->condorcetDefaultMethod);
});

test('overview example', function () {
try {
include __DIR__.'/../../Examples/1. Overview.php';
} catch (\Exception $e) {
throw $e;
}

expect(true)->toBeTrue();
});

public function testOverviewExample(): void
{
try {
include __DIR__.'/../../Examples/1. Overview.php';
} catch (\Exception $e) {
throw $e;
}

expect(true)->toBeTrue();
test('advanced object management example', function () {
try {
include __DIR__.'/../../Examples/2. AdvancedObjectManagement.php';
} catch (\Exception $e) {
throw $e;
}

public function testAdvancedObjectManagementExample(): void
{
try {
include __DIR__.'/../../Examples/2. AdvancedObjectManagement.php';
} catch (\Exception $e) {
throw $e;
}
expect(true)->toBeTrue();
});

expect(true)->toBeTrue();
}
test('global html example', function () {
$this->expectOutputRegex('/\<\/html\>/');

public function testGlobalHtmlExample(): void
{
$this->expectOutputRegex('/\<\/html\>/');
include __DIR__.'/../../Examples/Examples-with-html/A.Global_Example.php';
});

include __DIR__.'/../../Examples/Examples-with-html/A.Global_Example.php';
}
test('ranking manipulation html example', function () {
$this->expectOutputRegex('/\<\/html\>/');

public function testRankingManipulationHtmlExample(): void
{
$this->expectOutputRegex('/\<\/html\>/');

include __DIR__.'/../../Examples/Examples-with-html/B.Ranking_Manipulation.php';
}
}
include __DIR__.'/../../Examples/Examples-with-html/B.Ranking_Manipulation.php';
});
83 changes: 83 additions & 0 deletions Tests/Examples/ReadmeQuickExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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());
});
57 changes: 57 additions & 0 deletions Tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\AlgoTestCase::class)->in('src/Algo');

/*
|--------------------------------------------------------------------------
| 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 getMethodList(): array
{
$methods = Condorcet::getAuthMethods(withNonDeterministicMethods: false);
array_walk($methods, static fn(&$m): array => $m = [$m]);

return $methods;
}

function hasPDO(): bool
{
return \extension_loaded('pdo_sqlite');
}
Loading

0 comments on commit 0c0fbde

Please sign in to comment.