Skip to content

Commit

Permalink
Use attributes in tests instead of annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
TRowbotham committed Dec 15, 2023
1 parent 1d6e0af commit 1c9a580
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 248 deletions.
20 changes: 6 additions & 14 deletions tests/EncodingHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@

namespace Rowbot\URL\Tests;

use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Rowbot\URL\Support\EncodingHelper;

class EncodingHelperTest extends TestCase
{
public static function encodingDataProvider(): array
{
return [
['REPLACEMENT', 'utf-8'],
['UTF-16', 'utf-8'],
['UTF-16LE', 'utf-8'],
['UTF-16BE', 'utf-8'],
['ASCII', 'ascii'],
];
}

/**
* @dataProvider encodingDataProvider
*/
#[TestWith(['REPLACEMENT', 'utf-8'])]
#[TestWith(['UTF-16', 'utf-8'])]
#[TestWith(['UTF-16LE', 'utf-8'])]
#[TestWith(['UTF-16BE', 'utf-8'])]
#[TestWith(['ASCII', 'ascii'])]
public function testReplacementAndUtf16EncodingsGetForcedToUtf8($encoding, $outputEncoding): void
{
self::assertSame($outputEncoding, EncodingHelper::getOutputEncoding($encoding));
Expand Down
4 changes: 2 additions & 2 deletions tests/HostParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rowbot\URL\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rowbot\URL\Component\Host\HostInterface;
use Rowbot\URL\Component\Host\HostParser;
Expand All @@ -16,9 +17,8 @@ class HostParserTest extends TestCase
{
/**
* @see https://url.spec.whatwg.org/#example-host-parsing
*
* @dataProvider exampleDataProvider
*/
#[DataProvider('exampleDataProvider')]
public function testHostParser(string $input, array $output)
{
$parser = new HostParser();
Expand Down
136 changes: 32 additions & 104 deletions tests/Math/MathTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,22 @@

namespace Rowbot\URL\Tests\Math;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Rowbot\URL\Component\Host\Math\NumberInterface;

use const PHP_INT_MAX;

abstract class MathTestCase extends TestCase
{
/**
* @param int|string $number
*/
abstract public function createNumber($number, int $base = 10): NumberInterface;
abstract public function createNumber(int|string $number, int $base = 10): NumberInterface;

public static function intdivNumberProvider(): array
{
return [
[2, '21'],
[0x0D, '3'],
[2, '21'],
[24, '1'],
[-24, '-2'],
];
}

/**
* @dataProvider intdivNumberProvider
*/
#[TestWith([2, '21'])]
#[TestWith([0x0D, '3'])]
#[TestWith([2, '21'])]
#[TestWith([24, '1'])]
#[TestWith([-24, '-2'])]
public function testIntDiv(int $divisor, string $quoient): void
{
$dividend = $this->createNumber(42);
Expand All @@ -49,64 +39,33 @@ public static function equalityNumberProvider(): array
];
}

/**
* @dataProvider equalityNumberProvider
*
* @param $number int|string
*/
public function testIsEqualTo($number, int $base, string $expected): void
#[DataProvider('equalityNumberProvider')]
public function testIsEqualTo(int|string $number, int $base, string $expected): void
{
self::assertTrue($this->createNumber($number, $base)->isEqualTo($this->createNumber($expected)));
}

public static function greaterThanNumberProvider(): array
{
return [
[42, 9000, false],
[9000, 42, true],
[42, 42, false],
[-2, -3, true],
];
}

/**
* @dataProvider greaterThanNumberProvider
*/
#[TestWith([42, 9000, false])]
#[TestWith([9000, 42, true])]
#[TestWith([42, 42, false])]
#[TestWith([-2, -3, true])]
public function testIsGreaterThan(int $number1, int $number2, bool $result): void
{
self::assertSame($result, $this->createNumber($number1)->isGreaterThan($number2));
}

public static function greaterThanOrEqualToNumberProvider(): array
{
return [
[42, 9000, false],
[9000, 42, true],
[42, 42, true],
[-2, -3, true],
];
}

/**
* @dataProvider greaterThanOrEqualToNumberProvider
*/
#[TestWith([42, 9000, false])]
#[TestWith([9000, 42, true])]
#[TestWith([42, 42, true])]
#[TestWith([-2, -3, true])]
public function testIsGreaterThanOrEqualTo(int $number1, int $number2, bool $result): void
{
self::assertSame($result, $this->createNumber($number1)->isGreaterThanOrEqualTo($this->createNumber($number2)));
}

public static function modNumberProvider(): array
{
return [
[2, 2, 0],
[5, 3, 2],
[17, 6, 5],
];
}

/**
* @dataProvider modNumberProvider
*/
#[TestWith([2, 2, 0])]
#[TestWith([5, 3, 2])]
#[TestWith([17, 6, 5])]
public function testMod(int $dividend, int $divisor, int $remainder): void
{
$computedRemainder = $this->createNumber($dividend)->mod($divisor);
Expand All @@ -115,19 +74,10 @@ public function testMod(int $dividend, int $divisor, int $remainder): void
self::assertSame((string) $remainder, (string) $computedRemainder);
}

public static function multipliedByNumberProvider(): array
{
return [
[7, 2, 14],
[5, 3, 15],
[17, 6, 102],
[-4, 3, -12],
];
}

/**
* @dataProvider multipliedByNumberProvider
*/
#[TestWith([7, 2, 14])]
#[TestWith([5, 3, 15])]
#[TestWith([17, 6, 102])]
#[TestWith([-4, 3, -12])]
public function testMultipliedBy(int $multiplicand, int $multiplier, int $product): void
{
$computedProduct = $this->createNumber($multiplicand)->multipliedBy($multiplier);
Expand All @@ -136,18 +86,9 @@ public function testMultipliedBy(int $multiplicand, int $multiplier, int $produc
self::assertSame((string) $product, (string) $computedProduct);
}

public static function additionNumberProvider(): array
{
return [
[6, 6, '12'],
[4, -3, '1'],
[-256, 6, '-250'],
];
}

/**
* @dataProvider additionNumberProvider
*/
#[TestWith([6, 6, '12'])]
#[TestWith([4, -3, '1'])]
#[TestWith([-256, 6, '-250'])]
public function testPlus(int $addend1, int $addend2, string $sum): void
{
$computedSum = $this->createNumber($addend1)->plus($this->createNumber($addend2));
Expand All @@ -156,17 +97,8 @@ public function testPlus(int $addend1, int $addend2, string $sum): void
self::assertSame($sum, (string) $computedSum);
}

public static function powerNumberProvider(): array
{
return [
[256, 2, '65536'],
[2, 2, '4'],
];
}

/**
* @dataProvider powerNumberProvider
*/
#[TestWith([256, 2, '65536'])]
#[TestWith([2, 2, '4'])]
public function testPow(int $base, int $exponent, string $power): void
{
$computedPower = $this->createNumber($base)->pow($exponent);
Expand All @@ -175,12 +107,8 @@ public function testPow(int $base, int $exponent, string $power): void
self::assertSame($power, (string) $computedPower);
}

/**
* @dataProvider equalityNumberProvider
*
* @param int|string $number
*/
public function testToString($number, int $base, string $result): void
#[DataProvider('equalityNumberProvider')]
public function testToString(int|string $number, int $base, string $result): void
{
self::assertSame($result, (string) $this->createNumber($number, $base));
}
Expand Down
5 changes: 2 additions & 3 deletions tests/OriginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rowbot\URL\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rowbot\URL\BasicURLParser;
use Rowbot\URL\Component\Host\HostParser;
Expand Down Expand Up @@ -163,9 +164,7 @@ public static function originProvider(): array
];
}

/**
* @dataProvider originProvider
*/
#[DataProvider('originProvider')]
public function testSameOriginConcept(
Origin $originA,
Origin $originB,
Expand Down
5 changes: 2 additions & 3 deletions tests/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rowbot\URL\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rowbot\URL\Component\OpaquePath;
use Rowbot\URL\Component\PathSegment;
Expand All @@ -30,9 +31,7 @@ public static function isNormalizedWindowsDriveLetterProvider(): array
];
}

/**
* @dataProvider isNormalizedWindowsDriveLetterProvider
*/
#[DataProvider('isNormalizedWindowsDriveLetterProvider')]
public function testIsNormalizedWindowsDriveLetter(string $input, bool $expected): void
{
$s = new PathSegment($input);
Expand Down
23 changes: 7 additions & 16 deletions tests/SchemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Rowbot\URL\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Rowbot\URL\Component\Scheme;
Expand All @@ -24,28 +26,17 @@ public static function specialSchemeNonNullDefaultPortProvider(): iterable
}
}

/**
* @dataProvider specialSchemeNonNullDefaultPortProvider
*/
#[DataProvider('specialSchemeNonNullDefaultPortProvider')]
public function testIsDefaultPortReturnsTrueForNonNullPortSpecialSchemes(string $scheme, int $port): void
{
$scheme = new Scheme($scheme);
self::assertTrue($scheme->isDefaultPort($port));
}

public static function schemeDefaultPortProvider(): array
{
return [
['sftp', 22],
['ssh', 22],
['smtp', 25],
['file', null], // special scheme, but has no default port
];
}

/**
* @dataProvider schemeDefaultPortProvider
*/
#[TestWith(['sftp', 22])]
#[TestWith(['ssh', 22])]
#[TestWith(['smtp', 25])]
#[TestWith(['file', null])] // special scheme, but has no default port
public function testIsDefaultPortReturnsFalseForNonSpecialSchemesAndNullPorts(string $scheme, ?int $port): void
{
$scheme = new Scheme($scheme);
Expand Down
9 changes: 3 additions & 6 deletions tests/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rowbot\URL\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rowbot\URL\String\Exception\RegexException;
use Rowbot\URL\String\Exception\UndefinedIndexException;
Expand Down Expand Up @@ -35,9 +36,7 @@ public static function startsWithTwoAsciiHexDigitsProvider(): array
];
}

/**
* @dataProvider startsWithTwoAsciiHexDigitsProvider
*/
#[DataProvider('startsWithTwoAsciiHexDigitsProvider')]
public function testStartsWithTwoAsciiHexDigits(string $input, bool $expected): void
{
$s = new Utf8String($input);
Expand All @@ -62,9 +61,7 @@ public static function startsWithWindowsDriveLetterProvider(): array
];
}

/**
* @dataProvider startsWithWindowsDriveLetterProvider
*/
#[DataProvider('startsWithWindowsDriveLetterProvider')]
public function testStartsWithWindowsDriveLetter(string $input, bool $expected): void
{
$s = new Utf8String($input);
Expand Down
16 changes: 4 additions & 12 deletions tests/URLRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rowbot\URL\Tests;

use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Rowbot\URL\BasicURLParser;
use Rowbot\URL\String\Utf8String;
Expand All @@ -28,18 +29,9 @@ public function testFileSchemeCreatesOpaqueOrigin(): void
self::assertNull($origin->getEffectiveDomain());
}

public static function urlEqualityProvider(): iterable
{
return [
['file:///C:/Users/Desktop/', 'file:///C|/Users/Desktop/', true, true],
['https://example.com/path/?query#foo', 'https://example.com/path/?query', false, true],
['http://example.com/path/foo/#bar', 'http://example.com/bar/../path/foo/#bar', true, true],
];
}

/**
* @dataProvider urlEqualityProvider
*/
#[TestWith(['file:///C:/Users/Desktop/', 'file:///C|/Users/Desktop/', true, true])]
#[TestWith(['https://example.com/path/?query#foo', 'https://example.com/path/?query', false, true])]
#[TestWith(['http://example.com/path/foo/#bar', 'http://example.com/bar/../path/foo/#bar', true, true])]
public function testEquality(
string $urlA,
string $urlB,
Expand Down
Loading

0 comments on commit 1c9a580

Please sign in to comment.