Clean-up tests and use PHPStan max level for tests

This commit is contained in:
Ben Ramsey
2019-12-15 00:44:18 -06:00
parent 650cb9f545
commit 81f0f9a9b4
32 changed files with 487 additions and 591 deletions
+37 -14
View File
@@ -2,56 +2,68 @@
namespace Ramsey\Uuid\Test\Generator;
use Exception;
use InvalidArgumentException;
use PHPUnit\Framework\Error\Error as PHPUnitError;
use PHPUnit\Framework\MockObject\MockObject;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Generator\CombGenerator;
use Ramsey\Uuid\Generator\RandomGeneratorInterface;
use Ramsey\Uuid\Test\TestCase;
/**
* Class CombGeneratorTest
* @package Ramsey\Uuid\Test\Generator
* @covers Ramsey\Uuid\Generator\CombGenerator
*/
class CombGeneratorTest extends TestCase
{
/**
* @var int
*/
private $timestampBytes = 6;
public function testGenerateUsesRandomGeneratorWithLengthMinusTimestampBytes()
public function testGenerateUsesRandomGeneratorWithLengthMinusTimestampBytes(): void
{
$length = 10;
$expectedLength = ($length - $this->timestampBytes);
/** @var MockObject & RandomGeneratorInterface $randomGenerator */
$randomGenerator = $this->getMockBuilder(RandomGeneratorInterface::class)->getMock();
$randomGenerator->expects($this->once())
->method('generate')
->with($expectedLength);
/** @var MockObject & NumberConverterInterface $converter */
$converter = $this->getMockBuilder(NumberConverterInterface::class)->getMock();
$generator = new CombGenerator($randomGenerator, $converter);
$generator->generate($length);
}
public function testGenerateCalculatesPaddedHexStringFromCurrentTimestamp()
public function testGenerateCalculatesPaddedHexStringFromCurrentTimestamp(): void
{
/** @var MockObject & RandomGeneratorInterface $randomGenerator */
$randomGenerator = $this->getMockBuilder(RandomGeneratorInterface::class)->getMock();
/** @var MockObject & NumberConverterInterface $converter */
$converter = $this->getMockBuilder(NumberConverterInterface::class)->getMock();
$converter->expects($this->once())
->method('toHex')
->with($this->isType('string'));
$generator = new CombGenerator($randomGenerator, $converter);
$generator->generate(10);
}
public function testGenerateReturnsBinaryStringCreatedFromGeneratorAndConverter()
public function testGenerateReturnsBinaryStringCreatedFromGeneratorAndConverter(): void
{
$length = 20;
$hash = dechex(1234567891);
$timeHash = dechex(1458147405);
/** @var MockObject & RandomGeneratorInterface $randomGenerator */
$randomGenerator = $this->getMockBuilder(RandomGeneratorInterface::class)->getMock();
$randomGenerator->method('generate')
->with(($length - $this->timestampBytes))
->willReturn($hash);
/** @var MockObject & NumberConverterInterface $converter */
$converter = $this->getMockBuilder(NumberConverterInterface::class)->getMock();
$converter->method('toHex')
->with($this->isType('string'))
@@ -66,7 +78,10 @@ class CombGeneratorTest extends TestCase
$this->assertEquals($expected, $returned);
}
public function lengthLessThanSix()
/**
* @return array[]
*/
public function lengthLessThanSix(): array
{
return [[0], [1], [2], [3], [4], [5]];
}
@@ -74,25 +89,33 @@ class CombGeneratorTest extends TestCase
/**
* @dataProvider lengthLessThanSix
* @param int $length
* @throws \Exception
* @throws Exception
*/
public function testGenerateWithLessThanTimestampBytesThrowsException($length)
public function testGenerateWithLessThanTimestampBytesThrowsException($length): void
{
/** @var MockObject & RandomGeneratorInterface $randomGenerator */
$randomGenerator = $this->getMockBuilder(RandomGeneratorInterface::class)->getMock();
/** @var MockObject & NumberConverterInterface $converter */
$converter = $this->getMockBuilder(NumberConverterInterface::class)->getMock();
$generator = new CombGenerator($randomGenerator, $converter);
$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$generator->generate($length);
}
/**
* PHP Unit converts the error to an exception so we can test it.
* @throws Exception
*/
public function testGenerateWithOddNumberOverTimestampBytesCausesError()
public function testGenerateWithOddNumberOverTimestampBytesCausesError(): void
{
/** @var MockObject & RandomGeneratorInterface $randomGenerator */
$randomGenerator = $this->getMockBuilder(RandomGeneratorInterface::class)->getMock();
/** @var MockObject & NumberConverterInterface $converter */
$converter = $this->getMockBuilder(NumberConverterInterface::class)->getMock();
$generator = new CombGenerator($randomGenerator, $converter);
$this->expectException(PHPUnitError::class);