Skip to content

Commit

Permalink
Add purpose to each test and improvements (#16)
Browse files Browse the repository at this point in the history
* Add purpose to each test

* Improve tests
  • Loading branch information
Ekman committed Oct 23, 2018
1 parent a2e2215 commit 9ba100f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
32 changes: 15 additions & 17 deletions tests/LuhnAlgorithmTest.php
Expand Up @@ -46,67 +46,65 @@ public function setUp()
/**
* @dataProvider provideIsValid_success
*/
public function testIsValid_success($number, $expected)
public function testIsValid_success($number, $expected, $purpose)
{
$this->assertEquals($expected, $this->luhn->isValid($number));
$this->assertEquals($expected, $this->luhn->isValid($number), $purpose);
}

public function provideIsValid_success()
{
return [
[new Number('12345', 5), true],
[new Number('410321920', 2), true],
[new Number('3199723370002', 0), true],
[new Number('8914800000397416568', 5), true],
[new Number('12345', 6), false],
[new Number('12345', 5), true, "Valid number"],
[new Number('0', 0), true, "Zero"],
[new Number('92233720368547758072', 8), true, "Larger than INT_MAX"],
[new Number('12345', 6), false, "Invalid number"],
];
}

/**
* @dataProvider provideIsValid_failure
*/
public function testIsValid_failure($number, $exception)
public function testIsValid_failure($number, $exception, $purpose)
{
$this->expectException($exception);
$this->expectException($exception, $purpose);
$this->luhn->isValid($number);
}

public function provideIsValid_failure()
{
return [
[new Number(123, null), \InvalidArgumentException::class],
[new Number(123, null), \InvalidArgumentException::class, "Invalid argument"],
];
}

/**
* @dataProvider provideCalcChecksum_success
*/
public function testCalcChecksum_success($number, $expected)
public function testCalcChecksum_success($number, $expected, $purpose)
{
$this->assertEquals($expected, $this->luhn->calcChecksum($number));
$this->assertEquals($expected, $this->luhn->calcChecksum($number), $purpose);
}

public function provideCalcChecksum_success()
{
return [
[new Number(3199723370002), 50],
[new Number(3199723370002), 50, "Valid checksum"],
];
}

/**
* @dataProvider provideCalcCheckDigit_success
*/
public function testCalcCheckDigit_success($number, $expected)
public function testCalcCheckDigit_success($number, $expected, $purpose)
{
$this->assertEquals($expected, $this->luhn->calcCheckDigit($number));
}

public function provideCalcCheckDigit_success()
{
return [
[new Number(12345), 5],
[new Number(410321920), 2],
[new Number(3199723370002), 0],
[new Number(12345), 5, "Valid number"],
[new Number(559114884), 5, "Swedish company organization ID"],
];
}
}
45 changes: 22 additions & 23 deletions tests/NumberTest.php
Expand Up @@ -33,87 +33,86 @@ class NumberTest extends TestCase
/**
* @dataProvider provideFromString_success
*/
public function testFromString_success($number, $expected)
public function testFromString_success($number, $expected, $purpose)
{
$this->assertEquals($expected, Number::fromString($number));
}

public function provideFromString_success()
{
return [
["410321-9202", new Number('410321920', 2)],
[4103219202, new Number('410321920', 2)],
['89148000003974165685', new Number('8914800000397416568', 5)],
['abc123', new Number('12', 3)],
// Use any number that is larger then PHP_INT_MAX.
[((string) PHP_INT_MAX).'21', new Number(((string) PHP_INT_MAX).'2', 1)],
["410321-9202", new Number('410321920', 2), "String"],
[4103219202, new Number('410321920', 2), "Integer"],
['89148000003974165685', new Number('8914800000397416568', 5), "Large number"],
['abc123', new Number('12', 3), "Character in string"],
['922337203685477580721', new Number('92233720368547758072', 1), "Larger than INT_MAX"],
];
}

/**
* @dataProvider provideFromString_fail
*/
public function testFromString_fail($number, $expected)
public function testFromString_fail($number, $expected, $purpose)
{
$this->expectException($expected);
$this->expectException($expected, $purpose);
Number::fromString($number);
}

public function provideFromString_fail()
{
return [
['', \InvalidArgumentException::class],
['xyz ', \InvalidArgumentException::class],
['', \InvalidArgumentException::class, "Empty string"],
['xyz ', \InvalidArgumentException::class, "Invalid string"],
];
}

/**
* @dataProvider provideToString_success
*/
public function testToString_success($number, $expected)
public function testToString_success($number, $expected, $purpose)
{
$this->assertEquals($expected, (string) $number);
$this->assertEquals($expected, (string) $number, $purpose);
}

public function provideToString_success()
{
return [
[new Number(12345, 5), "123455"]
[new Number(12345, 5), "123455", "Valid number"]
];
}

/**
* @dataProvider provideNew_fail
*/
public function testNew_fail($number, $checkDigit, $expected)
public function testNew_fail($number, $checkDigit, $expected, $purpose)
{
$this->expectException($expected);
$this->expectException($expected, $purpose);
new Number($number, $checkDigit);
}

public function provideNew_fail()
{
return [
['abc123', 1, \InvalidArgumentException::class],
['123 ', null, \InvalidArgumentException::class],
['abc123', 1, \InvalidArgumentException::class, "Invalid number"],
['123 ', null, \InvalidArgumentException::class, "Whitespace"],
];
}

/**
* @dataProvider provideProperties
*/
public function testProperties($input, $checkDigit)
public function testProperties($input, $checkDigit, $purpose)
{
$number = new Number($input, $checkDigit);
$this->assertEquals($input, $number->getNumber());
$this->assertEquals($checkDigit, $number->getCheckDigit());
$this->assertEquals($input, $number->getNumber(), $purpose);
$this->assertEquals($checkDigit, $number->getCheckDigit(), $purpose);
}

public function provideProperties()
{
return [
[123, 1],
[123, null],
[123, 1, "Valid number and checkdigit"],
[123, null, "Valid number and checkdigit (null)"],
];
}
}

0 comments on commit 9ba100f

Please sign in to comment.