Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
JobFactoryTest.php
Go to the documentation of this file.
1 <?php
7 
13 
17 class JobFactoryTest extends \PHPUnit\Framework\TestCase
18 {
22  private $objectManager;
23 
27  private $jobFactory;
28 
29  public function setUp()
30  {
31  $serviceManager =
32  $this->getMockForAbstractClass(\Zend\ServiceManager\ServiceLocatorInterface::class, [], '', false);
33  $status = $this->createMock(\Magento\Setup\Model\Cron\Status::class);
34  $status->expects($this->once())->method('getStatusFilePath')->willReturn('path_a');
35  $status->expects($this->once())->method('getLogFilePath')->willReturn('path_b');
36  $objectManagerProvider = $this->createMock(\Magento\Setup\Model\ObjectManagerProvider::class);
37  $this->objectManager = $this->getMockForAbstractClass(
38  \Magento\Framework\ObjectManagerInterface::class,
39  [],
40  '',
41  false
42  );
43  $objectManagerProvider->expects($this->atLeastOnce())->method('get')->willReturn($this->objectManager);
44 
45  $upgradeCommand = $this->createMock(\Magento\Setup\Console\Command\UpgradeCommand::class);
46  $moduleUninstaller = $this->createMock(\Magento\Setup\Model\ModuleUninstaller::class);
47  $moduleRegistryUninstaller =
48  $this->createMock(\Magento\Setup\Model\ModuleRegistryUninstaller::class);
49  $moduleEnabler = $this->createMock(\Magento\Setup\Console\Command\ModuleEnableCommand::class);
50  $moduleDisabler = $this->createMock(\Magento\Setup\Console\Command\ModuleDisableCommand::class);
51  $maintenanceDisabler = $this->createMock(MaintenanceDisableCommand::class);
52  $maintenanceEnabler = $this->createMock(MaintenanceEnableCommand::class);
53 
54  $updater = $this->createMock(\Magento\Setup\Model\Updater::class);
55  $queue = $this->createMock(\Magento\Setup\Model\Cron\Queue::class);
56 
57  $returnValueMap = [
58  [\Magento\Setup\Model\Updater::class, $updater],
59  [\Magento\Setup\Model\Cron\Status::class, $status],
60  [\Magento\Setup\Console\Command\UpgradeCommand::class, $upgradeCommand],
61  [\Magento\Setup\Model\ObjectManagerProvider::class, $objectManagerProvider],
62  [\Magento\Setup\Model\ModuleUninstaller::class, $moduleUninstaller],
63  [\Magento\Setup\Model\ModuleRegistryUninstaller::class, $moduleRegistryUninstaller],
64  [\Magento\Setup\Console\Command\ModuleDisableCommand::class, $moduleDisabler],
65  [\Magento\Setup\Console\Command\ModuleEnableCommand::class, $moduleEnabler],
66  [MaintenanceDisableCommand::class, $maintenanceDisabler],
67  [MaintenanceEnableCommand::class, $maintenanceEnabler],
68  [\Magento\Setup\Model\Cron\Queue::class, $queue]
69  ];
70 
71  $serviceManager->expects($this->atLeastOnce())
72  ->method('get')
73  ->will($this->returnValueMap($returnValueMap));
74 
75  $this->jobFactory = new JobFactory($serviceManager);
76  }
77 
78  public function testUpgrade()
79  {
80  $this->assertInstanceOf(
81  \Magento\Setup\Model\Cron\AbstractJob::class,
82  $this->jobFactory->create('setup:upgrade', [])
83  );
84  }
85 
86  public function testRollback()
87  {
88  $valueMap = [
89  [
90  \Magento\Framework\App\State\CleanupFiles::class,
91  $this->createMock(\Magento\Framework\App\State\CleanupFiles::class)
92  ],
93  [
94  \Magento\Framework\App\Cache::class,
95  $this->createMock(\Magento\Framework\App\Cache::class)
96  ],
97  [
98  \Magento\Framework\Setup\BackupRollbackFactory::class,
99  $this->createMock(\Magento\Framework\Setup\BackupRollbackFactory::class)
100  ],
101  ];
102  $this->objectManager->expects($this->any())
103  ->method('get')
104  ->will($this->returnValueMap($valueMap));
105 
106  $this->assertInstanceOf(
107  \Magento\Setup\Model\Cron\AbstractJob::class,
108  $this->jobFactory->create('setup:rollback', [])
109  );
110  }
111 
112  public function testComponentUninstall()
113  {
114  $valueMap = [
115  [
116  \Magento\Framework\Module\PackageInfoFactory::class,
117  $this->createMock(\Magento\Framework\Module\PackageInfoFactory::class)
118  ],
119  [
120  \Magento\Framework\Composer\ComposerInformation::class,
121  $this->createMock(\Magento\Framework\Composer\ComposerInformation::class)
122  ],
123  [
124  \Magento\Theme\Model\Theme\ThemeUninstaller::class,
125  $this->createMock(\Magento\Theme\Model\Theme\ThemeUninstaller::class)
126  ],
127  [
128  \Magento\Theme\Model\Theme\ThemePackageInfo::class,
129  $this->createMock(\Magento\Theme\Model\Theme\ThemePackageInfo::class)
130  ],
131  ];
132  $this->objectManager->expects($this->any())
133  ->method('get')
134  ->will($this->returnValueMap($valueMap));
135  $this->assertInstanceOf(
136  \Magento\Setup\Model\Cron\JobComponentUninstall::class,
137  $this->jobFactory->create('setup:component:uninstall', [])
138  );
139  }
140 
145  public function testCreateUnknownJob()
146  {
147  $this->jobFactory->create('unknown', []);
148  }
149 
150  public function testCacheEnable()
151  {
152  $valueMap = [
153  [
154  CacheEnableCommand::class,
155  $this->getMockBuilder(CacheEnableCommand::class)
156  ->disableOriginalConstructor()
157  ->getMock()
158  ]
159  ];
160 
161  $this->objectManager->expects($this->any())
162  ->method('get')
163  ->will($this->returnValueMap($valueMap));
164 
165  $this->assertInstanceOf(
166  \Magento\Setup\Model\Cron\JobSetCache::class,
167  $this->jobFactory->create('setup:cache:enable', [])
168  );
169  }
170 
171  public function testCacheDisable()
172  {
173  $valueMap = [
174  [
175  \Magento\Backend\Console\Command\CacheDisableCommand::class,
176  $this->getMockBuilder(CacheDisableCommand::class)
177  ->disableOriginalConstructor()
178  ->getMock()
179  ]
180  ];
181  $this->objectManager->expects($this->any())->method('get')->will($this->returnValueMap($valueMap));
182 
183  $this->assertInstanceOf(
184  \Magento\Setup\Model\Cron\JobSetCache::class,
185  $this->jobFactory->create('setup:cache:disable', [])
186  );
187  }
188 
189  public function testMaintenanceModeEnable()
190  {
191  $this->assertInstanceOf(
192  \Magento\Setup\Model\Cron\JobSetMaintenanceMode::class,
193  $this->jobFactory->create(JobFactory::JOB_MAINTENANCE_MODE_ENABLE, [])
194  );
195  }
196 
197  public function testMaintenanceModeDisable()
198  {
199  $this->assertInstanceOf(
200  \Magento\Setup\Model\Cron\JobSetMaintenanceMode::class,
201  $this->jobFactory->create(JobFactory::JOB_MAINTENANCE_MODE_DISABLE, [])
202  );
203  }
204 }
205 
206 // functions to override native php functions
207 namespace Magento\Setup\Model\Cron;
208 
212 function fopen()
213 {
214  return 'filestream';
215 }
216 
220 function is_resource()
221 {
222  return true;
223 }
224 
229 {
230  return 'stream';
231 }
$queue
Definition: queue.php:21
$status
Definition: order_status.php:8