-
-
Notifications
You must be signed in to change notification settings - Fork 502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Make uuid mockable #23
Comments
Why do you need to mock the method? |
There's no need for Uuid to be mockable, especially since you can create a Uuid from a static string ( |
Here is my scenario. I have a class Acme/Uuid that contains two methods:
I want to test the |
I think you're coupling your test to your implementation. I would probably test it like this: <?php
namespace Acme;
use Acme\Uuid;
use PHPUnit_Framework_TestCase;
final class UuidTest extends PHPUnit_Framework_TestCase {
/**
* @test
*/
public function generates_uuid () {
assertThat(Uuid::generate(), isInstanceOf(Uuid::class));
}
/**
* @test
*/
public function generates_UUID4_compatible_values () {
assertThat(Uuid::generate(), logicalNot(equalTo(Uuid::generate())));
}
/**
* @test
*/
public function knows_when_UUID_is_valid () {
$aVersion4Uuid = Uuid::fromString('b6543b53-0d87-4de0-af80-3777f26fef02');
assertThat($aVersion4Uuid->isValidUuid4(), isTrue());
}
/**
* @test
*/
public function knows_when_UUID_is_invalid () {
$aVersion1Uuid = Uuid::fromString('677da381-ac38-11e3-a5e2-0800200c9a66');
assertThat($aVersion1Uuid->isValidUuid4(), isFalse());
}
} But so far I'm not seeing the advantage of your layer of composition over the default implementation. |
Hi Marijn and thanks for your reply. The Acme/Uuid class contains the non-static method generate() which returns a random string and not an instance of public function generate()
{
return (string) \Rhumsaa\Uuid\Uuid::uuid4();
}
|
@dimsav, there are a few ways you can go about this. The first is through dependency injection with something like this: public function generate($generator = '\Rhumsaa\Uuid\Uuid')
{
return (string) $generator::uuid4();
} In this way, you can create a mock class and pass the name of it in as the generator, so at test time, you can test that your code does what it should. Kind of like this: class Bar
{
public static function uuid4()
{
return 'foobar';
}
}
$f = new Foo;
var_dump($f->generate('Bar')); However, it might not be easy for you to approach it from this way, depending on how the rest of your code is structured, so an approach like this might be better: public function generate()
{
$uuidGenerator = $this->getUuidGenerator();
return (string) $uuidGenerator::uuid4();
}
public function getUuidGenerator()
{
return '\Rhumsaa\Uuid\Uuid';
} Now, none of your code external to this class should have to change, and you can mock Further still, you can do something like this: public function generate()
{
return $this->getUuidV4();
}
/**
* @codeCoverageIgnore
*/
public function getUuidV4()
{
return (string) \Rhumsaa\Uuid\Uuid::uuid4();
} Now, you're telling PHPUnit/XDebug to ignore the |
@ramsey, I tried your suggestions hoping to find a solution to the problem. None of them worked, unfortunately. public function generate()
{
$uuidGenerator = $this->getUuidGenerator();
return (string) $uuidGenerator::uuid4();
}
public function getUuidGenerator()
{
return '\Rhumsaa\Uuid\Uuid';
} If we want to test the above script, we have the following issues:
public function generate()
{
return $this->getUuidV4();
}
/**
* @codeCoverageIgnore
*/
public function getUuidV4()
{
return (string) \Rhumsaa\Uuid\Uuid::uuid4();
} I can't consider the above code as a solution to this issue, as we ignore the method we want to test. Static methods are death to testability. I wish this repo helped its users by allowing them to write testable code. |
Sorry mate, that's uncalled for. Quite a few good suggestions have been made here. |
You're welcome 😄
If you look more closely to my example you see that it returns an instance of
You wanted to test if Does this clarify the intent of my tests a bit better? If you have questions I'll be happy to answer but don't get snarky with comments about testability, that's not cool. |
Still, its not a string.
No, I wanted to test if the result of Thanks |
I've merged #25 into the 3.0 branch, which effectively closes out this issue. |
Nice thanks! |
Hi and thanks for your nice package!
I tried today to write some tests to classes using
Uuid::uuid4()
but it was not possible. The current structure of the class doesn't allow it to be mocked in unit tests since there is no way to get an instance of the class.The text was updated successfully, but these errors were encountered: