PathTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. <?php
  2. namespace Path\Tests\unit;
  3. use Path\BuiltinProxy;
  4. use Path\Exception\FileExistsException;
  5. use Path\Exception\FileNotFoundException;
  6. use Path\Exception\IOException;
  7. use Path\Path;
  8. use PHPUnit\Framework\MockObject\MockObject;
  9. use PHPUnit\Framework\TestCase;
  10. class TestablePath extends Path {
  11. public function setBuiltin(BuiltinProxy $builtinProxy): void
  12. {
  13. $this->builtin = $builtinProxy;
  14. }
  15. public function cast(string|Path $path): Path
  16. {
  17. return parent::cast($path);
  18. }
  19. }
  20. class PathTest extends TestCase
  21. {
  22. private BuiltinProxy | MockObject $builtin;
  23. public function setUp(): void
  24. {
  25. $this->builtin = $this->getMockBuilder(BuiltinProxy::class)->getMock();
  26. }
  27. public function getMock(string $path, string $methodName): TestablePath | MockObject
  28. {
  29. $mock = $this
  30. ->getMockBuilder(TestablePath::class)
  31. ->setConstructorArgs([$path])
  32. ->setMethodsExcept(['__toString', 'setBuiltin', $methodName])
  33. ->getMock();
  34. $mock->setBuiltin($this->builtin);
  35. return $mock;
  36. }
  37. /**
  38. * Test 'join' method.
  39. */
  40. public function testJoin(): void
  41. {
  42. // One part
  43. $this->assertEquals(
  44. '/home/user',
  45. Path::join('/home', 'user')
  46. );
  47. // Multiple parts
  48. $this->assertEquals(
  49. '/home/user/documents',
  50. Path::join('/home', 'user', 'documents')
  51. );
  52. // Absolute path passed in $parts
  53. $this->assertEquals(
  54. '/user/documents',
  55. Path::join('home', '/user', 'documents')
  56. );
  57. }
  58. public function testToString(): void
  59. {
  60. $path = new Path('/foo/bar');
  61. $this->assertEquals('/foo/bar', $path->__toString());
  62. }
  63. public function testPath()
  64. {
  65. $path = $this->getMock('bar', 'path');
  66. $this->assertEquals(
  67. 'bar',
  68. $path->path()
  69. );
  70. }
  71. public function testEq(): void
  72. {
  73. $path = $this->getMock('bar', 'eq');
  74. $path
  75. ->method('path')
  76. ->willReturn('bar');
  77. $path2 = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  78. $path2
  79. ->method('path')
  80. ->willReturn('bar');
  81. $path3 = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  82. $path3
  83. ->method('path')
  84. ->willReturn('/foo/bar');
  85. $path->method('cast')->willReturnMap(
  86. [
  87. [$path2, $path2],
  88. [$path3, $path3],
  89. ['bar', $path2],
  90. ['/foo/bar', $path3],
  91. ]
  92. );
  93. $this->assertTrue($path->eq($path2));
  94. $this->assertFalse($path->eq($path3));
  95. $this->assertTrue($path->eq('bar'));
  96. $this->assertFalse($path->eq('/foo/bar'));
  97. }
  98. /**
  99. * @throws IOException
  100. */
  101. public function testAbsPath(): void {
  102. $path = $this->getMock('bar', 'absPath');
  103. $this->builtin
  104. ->expects(self::once())
  105. ->method('realpath')
  106. ->with('bar')
  107. ->willReturn('/foo/bar');
  108. $newPath = $this->getMockBuilder(TestablePath::class)
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $newPath->method('path')->willReturn('/foo/bar');
  112. $path->method('cast')->with('/foo/bar')->willReturn($newPath);
  113. $this->assertEquals(
  114. '/foo/bar',
  115. $path->absPath()->path()
  116. );
  117. }
  118. /**
  119. * @throws IOException
  120. */
  121. public function testRealPath()
  122. {
  123. $path = $this->getMock('bar', 'realpath');
  124. $newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  125. $newPath
  126. ->method('eq')
  127. ->with('/foo/bar')
  128. ->willReturn(True);
  129. $path
  130. ->expects(self::once())
  131. ->method('absPath')
  132. ->willReturn($newPath);
  133. $this->assertTrue(
  134. $path->realpath()->eq('/foo/bar')
  135. );
  136. }
  137. public function testAccessFileExist(): void
  138. {
  139. $path = $this->getMock('bar', 'access');
  140. $this->builtin
  141. ->expects(self::once())
  142. ->method('file_exists')
  143. ->with('bar')
  144. ->willReturn(true);
  145. $this->builtin
  146. ->expects(self::never())
  147. ->method('is_readable')
  148. ->with('bar')
  149. ->willReturn(true);
  150. $this->builtin
  151. ->expects(self::never())
  152. ->method('is_writable')
  153. ->with('bar')
  154. ->willReturn(true);
  155. $this->builtin
  156. ->expects(self::never())
  157. ->method('is_executable')
  158. ->with('bar')
  159. ->willReturn(true);
  160. $this->assertTrue(
  161. $path->access(Path::F_OK)
  162. );
  163. }
  164. public function testAccessIsReadable(): void
  165. {
  166. $path = $this->getMock('bar', 'access');
  167. $this->builtin
  168. ->expects(self::never())
  169. ->method('file_exists')
  170. ->with('bar')
  171. ->willReturn(true);
  172. $this->builtin
  173. ->expects(self::once())
  174. ->method('is_readable')
  175. ->with('bar')
  176. ->willReturn(true);
  177. $this->builtin
  178. ->expects(self::never())
  179. ->method('is_writable')
  180. ->with('bar')
  181. ->willReturn(true);
  182. $this->builtin
  183. ->expects(self::never())
  184. ->method('is_executable')
  185. ->with('bar')
  186. ->willReturn(true);
  187. $this->assertTrue(
  188. $path->access(Path::R_OK)
  189. );
  190. }
  191. public function testAccessIsWritable(): void
  192. {
  193. $path = $this->getMock('bar', 'access');
  194. $this->builtin
  195. ->expects(self::never())
  196. ->method('file_exists')
  197. ->with('bar')
  198. ->willReturn(true);
  199. $this->builtin
  200. ->expects(self::never())
  201. ->method('is_readable')
  202. ->with('bar')
  203. ->willReturn(true);
  204. $this->builtin
  205. ->expects(self::once())
  206. ->method('is_writable')
  207. ->with('bar')
  208. ->willReturn(true);
  209. $this->builtin
  210. ->expects(self::never())
  211. ->method('is_executable')
  212. ->with('bar')
  213. ->willReturn(true);
  214. $this->assertTrue(
  215. $path->access(Path::W_OK)
  216. );
  217. }
  218. public function testAccessIsExecutable(): void
  219. {
  220. $path = $this->getMock('bar', 'access');
  221. $this->builtin
  222. ->expects(self::never())
  223. ->method('file_exists')
  224. ->with('bar')
  225. ->willReturn(true);
  226. $this->builtin
  227. ->expects(self::never())
  228. ->method('is_readable')
  229. ->with('bar')
  230. ->willReturn(true);
  231. $this->builtin
  232. ->expects(self::never())
  233. ->method('is_writable')
  234. ->with('bar')
  235. ->willReturn(true);
  236. $this->builtin
  237. ->expects(self::once())
  238. ->method('is_executable')
  239. ->with('bar')
  240. ->willReturn(true);
  241. $this->assertTrue(
  242. $path->access(Path::X_OK)
  243. );
  244. }
  245. public function testAccessInvalidMode(): void
  246. {
  247. $path = $this->getMock('bar', 'access');
  248. $this->builtin
  249. ->expects(self::never())
  250. ->method('file_exists')
  251. ->with('bar')
  252. ->willReturn(true);
  253. $this->builtin
  254. ->expects(self::never())
  255. ->method('is_readable')
  256. ->with('bar')
  257. ->willReturn(true);
  258. $this->builtin
  259. ->expects(self::never())
  260. ->method('is_writable')
  261. ->with('bar')
  262. ->willReturn(true);
  263. $this->builtin
  264. ->expects(self::never())
  265. ->method('is_executable')
  266. ->with('bar')
  267. ->willReturn(true);
  268. $this->expectException(\RuntimeException::class);
  269. $path->access(-1);
  270. }
  271. public function testATime(): void
  272. {
  273. $path = $this->getMock('bar', 'atime');
  274. $this->builtin
  275. ->expects(self::once())
  276. ->method('fileatime')
  277. ->with('bar')
  278. ->willReturn(1000);
  279. $date = '2000-01-01';
  280. $this->builtin
  281. ->expects(self::once())
  282. ->method('date')
  283. ->with('Y-m-d H:i:s', 1000)
  284. ->willReturn($date);
  285. $this->assertEquals(
  286. $date,
  287. $path->atime()
  288. );
  289. }
  290. public function testATimeError(): void
  291. {
  292. $path = $this->getMock('bar', 'atime');
  293. $this->builtin
  294. ->expects(self::once())
  295. ->method('fileatime')
  296. ->with('bar')
  297. ->willReturn(false);
  298. $this->assertEquals(
  299. null,
  300. $path->atime()
  301. );
  302. }
  303. public function testCTime(): void
  304. {
  305. $path = $this->getMock('bar', 'ctime');
  306. $this->builtin
  307. ->expects(self::once())
  308. ->method('filectime')
  309. ->with('bar')
  310. ->willReturn(1000);
  311. $date = '2000-01-01';
  312. $this->builtin
  313. ->expects(self::once())
  314. ->method('date')
  315. ->with('Y-m-d H:i:s', 1000)
  316. ->willReturn($date);
  317. $this->assertEquals(
  318. $date,
  319. $path->ctime()
  320. );
  321. }
  322. public function testCTimeError(): void
  323. {
  324. $path = $this->getMock('bar', 'ctime');
  325. $this->builtin
  326. ->expects(self::once())
  327. ->method('filectime')
  328. ->with('bar')
  329. ->willReturn(false);
  330. $this->assertEquals(
  331. null,
  332. $path->ctime()
  333. );
  334. }
  335. public function testMTime(): void
  336. {
  337. $path = $this->getMock('bar', 'mtime');
  338. $this->builtin
  339. ->expects(self::once())
  340. ->method('filemtime')
  341. ->with('bar')
  342. ->willReturn(1000);
  343. $date = '2000-01-01';
  344. $this->builtin
  345. ->expects(self::once())
  346. ->method('date')
  347. ->with('Y-m-d H:i:s', 1000)
  348. ->willReturn($date);
  349. $this->assertEquals(
  350. $date,
  351. $path->mtime()
  352. );
  353. }
  354. public function testMTimeError(): void
  355. {
  356. $path = $this->getMock('bar', 'mtime');
  357. $this->builtin
  358. ->expects(self::once())
  359. ->method('filemtime')
  360. ->with('bar')
  361. ->willReturn(false);
  362. $this->assertEquals(
  363. null,
  364. $path->mtime()
  365. );
  366. }
  367. public function testIsFile(): void
  368. {
  369. $path = $this->getMock('bar', 'isFile');
  370. $this->builtin
  371. ->expects(self::once())
  372. ->method('is_file')
  373. ->with('bar')
  374. ->willReturn(true);
  375. $this->assertTrue(
  376. $path->isFile()
  377. );
  378. }
  379. public function testIsDir(): void
  380. {
  381. $path = $this->getMock('bar', 'isDir');
  382. $this->builtin
  383. ->expects(self::once())
  384. ->method('is_dir')
  385. ->with('bar')
  386. ->willReturn(true);
  387. $this->assertTrue(
  388. $path->isDir()
  389. );
  390. }
  391. public function testExt(): void
  392. {
  393. $path = $this->getMock('bar.ext', 'ext');
  394. $this->builtin
  395. ->expects(self::once())
  396. ->method('pathinfo')
  397. ->with('bar.ext', PATHINFO_EXTENSION)
  398. ->willReturn('ext');
  399. $this->assertEquals(
  400. 'ext',
  401. $path->ext()
  402. );
  403. }
  404. public function testBaseName(): void
  405. {
  406. $path = $this->getMock('bar.ext', 'basename');
  407. $this->builtin
  408. ->expects(self::once())
  409. ->method('pathinfo')
  410. ->with('bar.ext', PATHINFO_BASENAME)
  411. ->willReturn('bar.ext');
  412. $this->assertEquals(
  413. 'bar.ext',
  414. $path->basename()
  415. );
  416. }
  417. public function testCD(): void
  418. {
  419. $path = $this->getMock('bar', 'cd');
  420. $this->builtin
  421. ->expects(self::once())
  422. ->method('chdir')
  423. ->with('foo')
  424. ->willReturn(true);
  425. $this->assertTrue(
  426. $path->cd('foo')
  427. );
  428. }
  429. public function testChDir(): void
  430. {
  431. $path = $this->getMock('bar', 'chdir');
  432. $path
  433. ->expects(self::once())
  434. ->method('cd')
  435. ->with('foo')
  436. ->willReturn(true);
  437. $this->assertTrue(
  438. $path->cd('foo')
  439. );
  440. }
  441. public function testName(): void
  442. {
  443. $path = $this->getMock('bar.ext', 'name');
  444. $this->builtin
  445. ->expects(self::once())
  446. ->method('pathinfo')
  447. ->with('bar.ext', PATHINFO_FILENAME)
  448. ->willReturn('bar');
  449. $this->assertEquals(
  450. 'bar',
  451. $path->name()
  452. );
  453. }
  454. /**
  455. * @throws IOException
  456. * @throws FileExistsException
  457. */
  458. public function testMkDir(): void
  459. {
  460. $path = $this->getMock('foo', 'mkdir');
  461. $path->method('isDir')->willReturn(False);
  462. $path->method('isFile')->willReturn(False);
  463. $this->builtin
  464. ->expects(self::once())
  465. ->method('mkdir')
  466. ->with('foo', 0777, false)
  467. ->willReturn(true);
  468. $path->mkdir();
  469. }
  470. /**
  471. * @throws IOException
  472. */
  473. public function testMkDirAlreadyExist(): void
  474. {
  475. $path = $this->getMock('foo', 'mkdir');
  476. $path->method('isDir')->willReturn(True);
  477. $this->expectException(FileExistsException::class);
  478. $path->mkdir();
  479. }
  480. /**
  481. * @throws IOException
  482. * @throws FileExistsException
  483. */
  484. public function testMkDirAlreadyExistButRecursive(): void
  485. {
  486. $path = $this->getMock('foo', 'mkdir');
  487. $path->method('isDir')->willReturn(True);
  488. $this->builtin
  489. ->expects(self::never())
  490. ->method('mkdir');
  491. $path->mkdir(0777, true);
  492. }
  493. /**
  494. * @throws IOException
  495. * @throws FileExistsException
  496. */
  497. public function testMkDirFileExists(): void
  498. {
  499. $path = $this->getMock('foo', 'mkdir');
  500. $path->method('isDir')->willReturn(False);
  501. $path->method('isFile')->willReturn(True);
  502. $this->expectException(FileExistsException::class);
  503. $path->mkdir();
  504. }
  505. /**
  506. * @throws IOException
  507. * @throws FileExistsException
  508. */
  509. public function testMkDirFileExistsButRecursive(): void
  510. {
  511. $path = $this->getMock('foo', 'mkdir');
  512. $path->method('isDir')->willReturn(False);
  513. $path->method('isFile')->willReturn(True);
  514. $this->expectException(FileExistsException::class);
  515. $path->mkdir(0777, true);
  516. }
  517. /**
  518. * @throws IOException
  519. * @throws FileExistsException
  520. */
  521. public function testMkDirIoError(): void
  522. {
  523. $path = $this->getMock('foo', 'mkdir');
  524. $path->method('isDir')->willReturn(False);
  525. $path->method('isFile')->willReturn(False);
  526. $this->builtin
  527. ->expects(self::once())
  528. ->method('mkdir')
  529. ->with('foo', 0777, false)
  530. ->willReturn(false);
  531. $this->expectException(IOException::class);
  532. $path->mkdir();
  533. }
  534. /**
  535. * @throws IOException
  536. * @throws FileNotFoundException
  537. */
  538. public function testDeleteIsFile(): void
  539. {
  540. $path = $this->getMock('foo', 'delete');
  541. $path->method('isFile')->willReturn(True);
  542. $path->method('isDir')->willReturn(False);
  543. $this->builtin
  544. ->expects(self::once())
  545. ->method('unlink')
  546. ->with('foo')
  547. ->willReturn(true);
  548. $path->delete();
  549. }
  550. /**
  551. * @throws IOException
  552. * @throws FileNotFoundException
  553. */
  554. public function testDeleteIsFileWithError(): void
  555. {
  556. $path = $this->getMock('foo', 'delete');
  557. $path->method('isFile')->willReturn(True);
  558. $path->method('isDir')->willReturn(False);
  559. $this->builtin
  560. ->expects(self::once())
  561. ->method('unlink')
  562. ->with('foo')
  563. ->willReturn(false);
  564. $this->expectException(IOException::class);
  565. $path->delete();
  566. }
  567. /**
  568. * @throws IOException
  569. * @throws FileNotFoundException
  570. */
  571. public function testDeleteIsDir(): void
  572. {
  573. $path = $this->getMock('foo', 'delete');
  574. $path->method('isFile')->willReturn(False);
  575. $path->method('isDir')->willReturn(True);
  576. $this->builtin
  577. ->expects(self::once())
  578. ->method('rmdir')
  579. ->with('foo')
  580. ->willReturn(true);
  581. $path->delete();
  582. }
  583. /**
  584. * @throws IOException
  585. * @throws FileNotFoundException
  586. */
  587. public function testDeleteIsDirWithError(): void
  588. {
  589. $path = $this->getMock('foo', 'delete');
  590. $path->method('isFile')->willReturn(False);
  591. $path->method('isDir')->willReturn(True);
  592. $this->builtin
  593. ->expects(self::once())
  594. ->method('rmdir')
  595. ->with('foo')
  596. ->willReturn(false);
  597. $this->expectException(IOException::class);
  598. $path->delete();
  599. }
  600. /**
  601. * @throws IOException
  602. * @throws FileNotFoundException
  603. */
  604. public function testDeleteNonExistentFile(): void
  605. {
  606. $path = $this->getMock('foo', 'delete');
  607. $path->method('isFile')->willReturn(False);
  608. $path->method('isDir')->willReturn(False);
  609. $this->builtin
  610. ->expects(self::never())
  611. ->method('unlink');
  612. $this->builtin
  613. ->expects(self::never())
  614. ->method('rmdir');
  615. $this->expectException(FileNotFoundException::class);
  616. $path->delete();
  617. }
  618. /**
  619. * @throws IOException
  620. * @throws FileNotFoundException
  621. * @throws FileExistsException
  622. */
  623. public function testCopy()
  624. {
  625. $path = $this->getMock('foo.ext', 'copy');
  626. $path->method('isFile')->willReturn(True);
  627. $destination = "/bar/foo2.ext";
  628. $this->builtin
  629. ->expects(self::once())
  630. ->method('is_dir')
  631. ->with($destination)
  632. ->willReturn(False);
  633. $this->builtin
  634. ->expects(self::once())
  635. ->method('file_exists')
  636. ->with($destination)
  637. ->willReturn(False);
  638. $this->builtin
  639. ->expects(self::once())
  640. ->method('copy')
  641. ->with('foo.ext', $destination)
  642. ->willReturn(True);
  643. $newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  644. $newPath->method('eq')->with($destination)->willReturn(True);
  645. $path->method('cast')->with($destination)->willReturn($newPath);
  646. $result = $path->copy($destination);
  647. $this->assertTrue(
  648. $result->eq($destination)
  649. );
  650. }
  651. /**
  652. * @throws IOException
  653. * @throws FileNotFoundException
  654. * @throws FileExistsException
  655. */
  656. public function testCopyFileDoesNotExist()
  657. {
  658. $path = $this->getMock('foo.ext', 'copy');
  659. $path->method('isFile')->willReturn(False);
  660. $destination = "/bar/foo2.ext";
  661. $this->builtin
  662. ->expects(self::never())
  663. ->method('is_dir');
  664. $this->builtin
  665. ->expects(self::never())
  666. ->method('file_exists');
  667. $this->builtin
  668. ->expects(self::never())
  669. ->method('copy');
  670. $this->expectException(FileNotFoundException::class);
  671. $path->copy($destination);
  672. }
  673. /**
  674. * @throws IOException
  675. * @throws FileNotFoundException
  676. * @throws FileExistsException
  677. */
  678. public function testCopyDestIsDir()
  679. {
  680. $path = $this->getMock('foo.ext', 'copy');
  681. $path->method('isFile')->willReturn(True);
  682. $path->method('basename')->willReturn('foo.ext');
  683. $destination = "/bar";
  684. $this->builtin
  685. ->expects(self::once())
  686. ->method('is_dir')
  687. ->with($destination)
  688. ->willReturn(True);
  689. $newDest = $destination . "/foo.ext";
  690. $this->builtin
  691. ->expects(self::once())
  692. ->method('file_exists')
  693. ->with($newDest)
  694. ->willReturn(False);
  695. $this->builtin
  696. ->expects(self::once())
  697. ->method('copy')
  698. ->with('foo.ext', $newDest)
  699. ->willReturn(True);
  700. $newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
  701. $newPath->method('eq')->with($newDest)->willReturn(True);
  702. $path->method('cast')->with($newDest)->willReturn($newPath);
  703. $result = $path->copy($destination);
  704. $this->assertTrue(
  705. $result->eq($newDest)
  706. );
  707. }
  708. /**
  709. * @throws IOException
  710. * @throws FileNotFoundException
  711. * @throws FileExistsException
  712. */
  713. public function testCopyFileAlreadyExists()
  714. {
  715. $path = $this->getMock('foo.ext', 'copy');
  716. $path->method('isFile')->willReturn(True);
  717. $destination = "/bar/foo2.ext";
  718. $this->builtin
  719. ->expects(self::once())
  720. ->method('is_dir')
  721. ->with($destination)
  722. ->willReturn(False);
  723. $this->builtin
  724. ->expects(self::once())
  725. ->method('file_exists')
  726. ->with($destination)
  727. ->willReturn(True);
  728. $this->builtin
  729. ->expects(self::never())
  730. ->method('copy');
  731. $this->expectException(FileExistsException::class);
  732. $path->copy($destination);
  733. }
  734. /**
  735. * @throws IOException
  736. * @throws FileNotFoundException
  737. * @throws FileExistsException
  738. */
  739. public function testCopyFileDestIsDirButFileAlreadyExists()
  740. {
  741. $path = $this->getMock('foo.ext', 'copy');
  742. $path->method('isFile')->willReturn(True);
  743. $path->method('basename')->willReturn('foo.ext');
  744. $destination = "/bar";
  745. $this->builtin
  746. ->expects(self::once())
  747. ->method('is_dir')
  748. ->with($destination)
  749. ->willReturn(True);
  750. $newDest = $destination . "/foo.ext";
  751. $this->builtin
  752. ->expects(self::once())
  753. ->method('file_exists')
  754. ->with($newDest)
  755. ->willReturn(True);
  756. $this->builtin
  757. ->expects(self::never())
  758. ->method('copy');
  759. $this->expectException(FileExistsException::class);
  760. $path->copy($destination);
  761. }
  762. /**
  763. * @throws IOException
  764. * @throws FileNotFoundException
  765. * @throws FileExistsException
  766. */
  767. public function testCopyFileWithError()
  768. {
  769. $path = $this->getMock('foo.ext', 'copy');
  770. $path->method('isFile')->willReturn(True);
  771. $destination = "/bar/foo2.ext";
  772. $this->builtin
  773. ->expects(self::once())
  774. ->method('is_dir')
  775. ->with($destination)
  776. ->willReturn(False);
  777. $this->builtin
  778. ->expects(self::once())
  779. ->method('file_exists')
  780. ->with($destination)
  781. ->willReturn(False);
  782. $this->builtin
  783. ->expects(self::once())
  784. ->method('copy')
  785. ->with('foo.ext', $destination)
  786. ->willReturn(False);
  787. $this->expectException(IOException::class);
  788. $path->copy($destination);
  789. }
  790. }