Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PidConsumerManagerTest.php
Go to the documentation of this file.
1 <?php
7 
13 use \PHPUnit_Framework_MockObject_MockObject as MockObject;
14 
15 class PidConsumerManagerTest extends \PHPUnit\Framework\TestCase
16 {
20  private $filesystemMock;
21 
25  private $pidConsumerManager;
26 
30  protected function setUp()
31  {
32  require_once __DIR__ . '/../../../_files/pid_consumer_functions_mocks.php';
33 
34  $this->filesystemMock = $this->getMockBuilder(Filesystem::class)
35  ->disableOriginalConstructor()
36  ->getMock();
37 
38  $this->pidConsumerManager = new PidConsumerManager($this->filesystemMock);
39  }
40 
47  public function testIsRun($fileExists, $pid, $expectedResult)
48  {
49  $pidFilePath = 'somepath/consumerName.pid';
50 
52  $directoryMock = $this->getMockBuilder(ReadInterface::class)
53  ->getMockForAbstractClass();
54  $directoryMock->expects($this->once())
55  ->method('isExist')
56  ->willReturn($fileExists);
57  $directoryMock->expects($this->any())
58  ->method('readFile')
59  ->with($pidFilePath)
60  ->willReturn($pid);
61 
62  $this->filesystemMock->expects($this->once())
63  ->method('getDirectoryRead')
65  ->willReturn($directoryMock);
66 
67  $this->assertSame($expectedResult, $this->pidConsumerManager->isRun($pidFilePath));
68  }
69 
73  public function isRunDataProvider()
74  {
75  return [
76  ['fileExists' => false, 'pid' => null, false],
77  ['fileExists' => false, 'pid' => 11111, false],
78  ['fileExists' => true, 'pid' => 11111, true],
79  ['fileExists' => true, 'pid' => 77777, false],
80  ];
81  }
82 
83  public function testSavePid()
84  {
85  $pidFilePath = '/var/somePath/pidfile.pid';
86 
88  $writeMock = $this->getMockBuilder(WriteInterface::class)
89  ->getMockForAbstractClass();
90  $this->filesystemMock->expects($this->once())
91  ->method('getDirectoryWrite')
93  ->willReturn($writeMock);
94  $writeMock->expects($this->once())
95  ->method('writeFile')
96  ->with(
97  $pidFilePath,
98  function_exists('posix_getpid') ? posix_getpid() : getmypid(),
99  'w'
100  );
101 
102  $this->pidConsumerManager->savePid($pidFilePath);
103  }
104 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60