Remove mocks for ext-uuid functions

This commit is contained in:
Ben Ramsey
2021-09-23 18:49:10 -05:00
parent a3df026d62
commit 852850b7da
12 changed files with 78 additions and 154 deletions
+5 -20
View File
@@ -4,29 +4,14 @@ parameters:
paths:
- ./tests
bootstrapFiles:
- ./tests/phpstan-bootstrap.php
- ./tests/static-analysis/stubs.php
checkMissingIterableValueType: false
reportUnmatchedIgnoredErrors: false
excludes_analyse:
- ./tests/ExpectedBehaviorTest.php
- ./tests/phpstan-bootstrap.php
excludePaths:
analyse:
- ./tests/ExpectedBehaviorTest.php
- ./tests/static-analysis/stubs.php
ignoreErrors:
-
message: "#^Function uuid_create\\(\\) has no return typehint specified\\.$#"
count: 1
path: ./tests/phpstan-bootstrap.php
-
message: "#^Function uuid_create\\(\\) has parameter \\$uuid_type with no typehint specified\\.$#"
count: 1
path: ./tests/phpstan-bootstrap.php
-
message: "#^Function uuid_parse\\(\\) has no return typehint specified\\.$#"
count: 1
path: ./tests/phpstan-bootstrap.php
-
message: "#^Function uuid_parse\\(\\) has parameter \\$uuid with no typehint specified\\.$#"
count: 1
path: ./tests/phpstan-bootstrap.php
-
message: "#^Call to static method Ramsey\\\\Uuid\\\\.+ on a separate line has no effect\\.$#"
paths:
+1 -1
View File
@@ -6,7 +6,7 @@ parameters:
checkMissingIterableValueType: false
reportUnmatchedIgnoredErrors: false
bootstrapFiles:
- ./tests/phpstan-bootstrap.php
- ./tests/static-analysis/stubs.php
ignoreErrors:
-
message: '#^Call to function is_int\(\) with float will always evaluate to false\.$#'
+5 -2
View File
@@ -8,11 +8,14 @@
<projectFiles>
<directory name="./src" />
<directory name="./tests/static-analysis" />
<directory name="./tests/static-analysis"/>
<ignoreFiles>
<file name="./tests/static-analysis/stubs.php"/>
</ignoreFiles>
</projectFiles>
<stubs>
<file name="./tests/phpstan-bootstrap.php"/>
<file name="./tests/static-analysis/stubs.php"/>
</stubs>
</psalm>
@@ -14,6 +14,9 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Generator;
use function uuid_create;
use function uuid_parse;
use const UUID_TYPE_RANDOM;
/**
+3
View File
@@ -14,6 +14,9 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Generator;
use function uuid_create;
use function uuid_parse;
use const UUID_TYPE_TIME;
/**
@@ -22,6 +22,7 @@ class PeclUuidNameGeneratorTest extends TestCase
* @param non-empty-string $ns
*
* @dataProvider provideNamesForHashingTest
* @requires extension uuid
*/
public function testPeclUuidNameGeneratorHashesName(string $ns, string $name, string $algorithm): void
{
@@ -55,7 +56,7 @@ class PeclUuidNameGeneratorTest extends TestCase
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
* @return array<array{ns: string, name: string, algorithm: string}>
*/
public function provideNamesForHashingTest(): array
{
@@ -98,6 +99,7 @@ class PeclUuidNameGeneratorTest extends TestCase
'Unable to hash namespace and name with algorithm \'aBadAlgorithm\''
);
/** @phpstan-ignore-next-line */
$generator->generate($namespace, 'a test name', 'aBadAlgorithm');
}
}
+12 -23
View File
@@ -5,36 +5,25 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Generator;
use Ramsey\Uuid\Generator\PeclUuidRandomGenerator;
use phpmock\mockery\PHPMockery;
use Ramsey\Uuid\Rfc4122\Fields;
use Ramsey\Uuid\Test\TestCase;
use Ramsey\Uuid\Uuid;
use const UUID_TYPE_RANDOM;
class PeclUuidRandomGeneratorTest extends PeclUuidTestCase
class PeclUuidRandomGeneratorTest extends TestCase
{
/**
* @var int
*/
private $length = 10;
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @requires extension uuid
*/
public function testGenerateCreatesUuidUsingPeclUuidMethods(): void
{
PHPMockery::mock('Ramsey\Uuid\Generator', 'uuid_create')
->once()
->with(UUID_TYPE_RANDOM)
->andReturn($this->uuidString);
PHPMockery::mock('Ramsey\Uuid\Generator', 'uuid_parse')
->once()
->with($this->uuidString)
->andReturn($this->uuidBinary);
$generator = new PeclUuidRandomGenerator();
$uuid = $generator->generate($this->length);
$bytes = $generator->generate(10);
$uuid = Uuid::fromBytes($bytes);
$this->assertSame($this->uuidBinary, $uuid);
/** @var Fields $fields */
$fields = $uuid->getFields();
$this->assertSame(16, strlen($bytes));
$this->assertSame(Uuid::UUID_TYPE_RANDOM, $fields->getVersion());
}
}
-20
View File
@@ -1,20 +0,0 @@
<?php
declare(strict_types=1);
namespace Ramsey\Uuid\Test\Generator;
use Ramsey\Uuid\Test\TestCase;
class PeclUuidTestCase extends TestCase
{
/**
* @var string
*/
protected $uuidString = 'b08c6fff-7dc5-e111-9b21-0800200c9a66';
/**
* @var string
*/
protected $uuidBinary = '62303863366666662d376463352d653131312d396232312d303830303230306339613636';
}
+12 -18
View File
@@ -5,31 +5,25 @@ declare(strict_types=1);
namespace Ramsey\Uuid\Test\Generator;
use Ramsey\Uuid\Generator\PeclUuidTimeGenerator;
use phpmock\mockery\PHPMockery;
use Ramsey\Uuid\Rfc4122\Fields;
use Ramsey\Uuid\Test\TestCase;
use Ramsey\Uuid\Uuid;
use const UUID_TYPE_TIME;
class PeclUuidTimeGeneratorTest extends PeclUuidTestCase
class PeclUuidTimeGeneratorTest extends TestCase
{
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @requires extension uuid
*/
public function testGenerateCreatesUuidUsingPeclUuidMethods(): void
{
PHPMockery::mock('Ramsey\Uuid\Generator', 'uuid_create')
->once()
->with(UUID_TYPE_TIME)
->andReturn($this->uuidString);
PHPMockery::mock('Ramsey\Uuid\Generator', 'uuid_parse')
->once()
->with($this->uuidString)
->andReturn($this->uuidBinary);
$generator = new PeclUuidTimeGenerator();
$uuid = $generator->generate();
$bytes = $generator->generate();
$uuid = Uuid::fromBytes($bytes);
$this->assertSame($this->uuidBinary, $uuid);
/** @var Fields $fields */
$fields = $uuid->getFields();
$this->assertSame(16, strlen($bytes));
$this->assertSame(Uuid::UUID_TYPE_TIME, $fields->getVersion());
}
}
+1 -2
View File
@@ -9,5 +9,4 @@
// Ensure floating-point precision is set to 14 (the default) for tests.
ini_set('precision', '14');
require_once __DIR__ . '/../vendor/autoload.php'; // composer autoload
require_once __DIR__ . '/phpstan-bootstrap.php';
require_once __DIR__ . '/../vendor/autoload.php';
-67
View File
@@ -1,67 +0,0 @@
<?php
/**
* PHPStan bootstrap
*
* @codingStandardsIgnoreFile
*/
if (!function_exists('uuid_create')) {
/**
* @param int $type
* @return string
*/
function uuid_create($type = 0)
{
switch ($type) {
case 1:
return \Ramsey\Uuid\v1();
case 4:
default:
return \Ramsey\Uuid\v4();
}
}
}
if (!function_exists('uuid_parse')) {
/**
* @param string $uuid
* @return string
*/
function uuid_parse($uuid)
{
return \Ramsey\Uuid\Uuid::fromString($uuid)->getBytes();
}
}
if (!function_exists('uuid_generate_md5')) {
/**
* @param string $ns
* @param string $name
* @return string
*/
function uuid_generate_md5($ns, $name)
{
return \Ramsey\Uuid\v3($ns, $name);
}
}
if (!function_exists('uuid_generate_sha1')) {
/**
* @param string $ns
* @param string $name
* @return string
*/
function uuid_generate_sha1($ns, $name)
{
return \Ramsey\Uuid\v5($ns, $name);
}
}
if (!defined('UUID_TYPE_TIME')) {
define('UUID_TYPE_TIME', 1);
}
if (!defined('UUID_TYPE_RANDOM')) {
define('UUID_TYPE_RANDOM', 4);
}
+33
View File
@@ -0,0 +1,33 @@
<?php
/**
* Stubs for static analysis
*
* @codingStandardsIgnoreFile
*/
if (!defined('UUID_TYPE_DEFAULT')) {
define('UUID_TYPE_DEFAULT', 0);
}
if (!defined('UUID_TYPE_TIME')) {
define('UUID_TYPE_TIME', 1);
}
if (!defined('UUID_TYPE_RANDOM')) {
define('UUID_TYPE_RANDOM', 4);
}
if (!function_exists('uuid_create')) {
/** @psalm-suppress all */
function uuid_create(int $uuid_type=UUID_TYPE_DEFAULT): string {} // @phpstan-ignore-line
}
if (!function_exists('uuid_generate_md5')) {
/** @psalm-suppress all */
function uuid_generate_md5(string $uuid_ns, string $name): string {} // @phpstan-ignore-line
}
if (!function_exists('uuid_generate_sha1')) {
/** @psalm-suppress all */
function uuid_generate_sha1(string $uuid_ns, string $name): string {} // @phpstan-ignore-line
}
if (!function_exists('uuid_parse')) {
/** @psalm-suppress all */
function uuid_parse(string $uuid): string {} // @phpstan-ignore-line
}