Guest User

Untitled

a guest
Sep 12th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. public function getFile($fileName, $path = '/')
  2. {
  3.  
  4. if($this->wrapper == null || $this->connection == null)
  5. throw new LogicException("call method setFTP first");
  6.  
  7. // get file on ftp server
  8. $this->connection->open();
  9. $downloadComplete = $this->wrapper->get($this->tmpDir . $fileName, $path . $fileName);
  10. $this->connection->close();
  11.  
  12. if($downloadComplete == false)
  13. return false; // TODO exception ?
  14.  
  15. // return file downloaded
  16. $this->finder->files()->in(__DIR__);
  17. foreach ($this->finder as $file) {
  18. return $file;
  19. }
  20.  
  21. return false; // TODO exception ?
  22.  
  23. }
  24.  
  25. class FtpServiceTest extends PHPUnit_Framework_TestCase
  26. {
  27.  
  28. protected $connectionMock;
  29. protected $ftpWrapperMock;
  30. protected $finderMock;
  31.  
  32. protected function setUp()
  33. {
  34. $this->connectionMock = $this->getConnectionMock();
  35. $this->ftpWrapperMock = $this->getFTPWrapperMock();
  36. $this->finderMock = $this->getFinderMock();
  37. }
  38.  
  39. protected function tearDown()
  40. {
  41. }
  42.  
  43. private function getFinderMock()
  44. {
  45. return $this->getMockBuilder(Finder::class)
  46. ->disableOriginalConstructor()
  47. ->getMock('Iterator');
  48. }
  49.  
  50. private function getConnectionMock()
  51. {
  52. return $this->getMockBuilder(Connection::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. }
  56.  
  57. private function getFTPWrapperMock()
  58. {
  59. return $this->getMockBuilder(FTPWrapper::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. }
  63.  
  64. // tests
  65. public function testMe()
  66. {
  67.  
  68. // arrange
  69. $host = 'localhost';
  70. $user = 'user';
  71. $password = '1234';
  72.  
  73. $filesArray = new ArrayObject(array(''));
  74.  
  75. $service = new FtpService('var/tmp/');
  76. $service->setFTP($this->connectionMock, $this->ftpWrapperMock);
  77. $service->setFinder($this->finderMock);
  78.  
  79. $this->connectionMock
  80. ->expects($this->once())
  81. ->method('open');
  82.  
  83. $this->ftpWrapperMock
  84. ->expects($this->once())
  85. ->method('get')
  86. ->will($this->returnValue(true));
  87.  
  88. $this->connectionMock
  89. ->expects($this->once())
  90. ->method('close');
  91.  
  92. $this->finderMock
  93. ->expects($this->once())
  94. ->method('files')
  95. ->will($this->returnValue($this->finderMock));
  96.  
  97. $this->finderMock
  98. ->expects($this->once())
  99. ->method('in')
  100. ->will($this->returnValue($filesArray));
  101.  
  102. // act
  103. $file = $service->getFile('/file.zip');
  104.  
  105. // assert
  106. $this->assertInstanceOf(SplFileInfo::class, $file);
  107.  
  108. }
  109.  
  110. }
  111.  
  112. private function getFinderMock()
  113. {
  114. return $this->getMockBuilder(Finder::class)
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. }
  118.  
  119. $this->finderMock->expects($this->once())
  120. ->method('getIterator')
  121. ->willReturn(new ArrayObject([$this->createMock(SplFileInfo::class)]));
Add Comment
Please, Sign In to add comment