<?php
use PHPUnit\Framework\TestCase;
class RegexTest extends TestCase
{
public function testCreateId() : void
{
$p = '/^id_[\da-f]{13}$/';
$foo = $this->createMock('Foo');
$foo->expects($this->once())
->method('useId')
->with(new PHPUnit\Framework\Constraint\RegularExpression($p))
->will($this->returnValue(true));
$bar = new Bar($foo);
$id = $bar->createId();
$this->assertMatchesRegularExpression($p, $id);
}
}
// class to test
class Bar
{
private $foo;
public function __construct(Foo $foo)
{
$this->foo = $foo;
}
public function createId()
{
$id = uniqid('id_');
$this->foo->useId($id);
return $id;
}
}
// dependency of Bar
class Foo
{
public function useId($id)
{
// ... doing stuff with id
return true;
}
}