PathTest.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Path\Tests\unit;
  3. use Path\BuiltinProxy;
  4. use Path\Path;
  5. use PHPUnit\Framework\MockObject\MockObject;
  6. use PHPUnit\Framework\TestCase;
  7. class TestablePath extends Path {
  8. public function setBuiltin(BuiltinProxy $builtinProxy): void
  9. {
  10. $this->builtin = $builtinProxy;
  11. }
  12. }
  13. class PathTest extends TestCase
  14. {
  15. private BuiltinProxy | MockObject $builtin;
  16. public function setUp(): void
  17. {
  18. $this->builtin = $this->getMockBuilder(BuiltinProxy::class)->getMock();
  19. }
  20. public function getPathMockForMethod(string $path, string $methodName): TestablePath | MockObject
  21. {
  22. $mock = $this
  23. ->getMockBuilder(TestablePath::class)
  24. ->setConstructorArgs([$path])
  25. ->setMethodsExcept(['setBuiltin', $methodName])
  26. ->getMock();
  27. $mock->setBuiltin($this->builtin);
  28. return $mock;
  29. }
  30. public function testAbsPath(): void {
  31. $path = $this->getPathMockForMethod('bar', 'absPath');
  32. $this->builtin
  33. ->expects(self::once())
  34. ->method('realpath')
  35. ->with('bar')
  36. ->willReturn('/foo/bar');
  37. $this->assertEquals('/foo/bar', $path->absPath());
  38. }
  39. }