CollectTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\NewRelicReporting\Test\Unit\Model\Module;
  7. use Magento\NewRelicReporting\Model\Module\Collect;
  8. use Magento\Framework\Module\FullModuleList;
  9. use Magento\Framework\Module\ModuleListInterface;
  10. use Magento\Framework\Module\Manager;
  11. use Magento\NewRelicReporting\Model\Module;
  12. /**
  13. * Class CollectTest
  14. */
  15. class CollectTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\NewRelicReporting\Model\Module\Collect
  19. */
  20. protected $model;
  21. /**
  22. * @var ModuleListInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $moduleListMock;
  25. /**
  26. * @var Manager|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $moduleManagerMock;
  29. /**
  30. * @var fullModuleList|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $fullModuleListMock;
  33. /**
  34. * @var \Magento\NewRelicReporting\Model\ModuleFactory|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $moduleFactoryMock;
  37. /**
  38. * @var \Magento\NewRelicReporting\Model\ResourceModel\Module\CollectionFactory
  39. * |\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $moduleCollectionFactoryMock;
  42. protected function setUp()
  43. {
  44. $this->moduleListMock = $this->getMockBuilder(\Magento\Framework\Module\ModuleListInterface::class)
  45. ->setMethods(['getNames', 'has', 'getAll', 'getOne'])
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->fullModuleListMock = $this->getMockBuilder(\Magento\Framework\Module\FullModuleList::class)
  49. ->setMethods(['getNames', 'getAll'])
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->moduleManagerMock = $this->getMockBuilder(\Magento\Framework\Module\Manager::class)
  53. ->setMethods(['isOutputEnabled'])
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->moduleFactoryMock = $this->createPartialMock(
  57. \Magento\NewRelicReporting\Model\ModuleFactory::class,
  58. ['create']
  59. );
  60. $this->moduleCollectionFactoryMock = $this->createPartialMock(
  61. \Magento\NewRelicReporting\Model\ResourceModel\Module\CollectionFactory::class,
  62. ['create']
  63. );
  64. $this->model = new Collect(
  65. $this->moduleListMock,
  66. $this->fullModuleListMock,
  67. $this->moduleManagerMock,
  68. $this->moduleFactoryMock,
  69. $this->moduleCollectionFactoryMock
  70. );
  71. }
  72. /**
  73. * Tests modules data returns array
  74. *
  75. * @return void
  76. */
  77. public function testGetModuleDataWithoutRefresh()
  78. {
  79. $moduleCollectionMock = $this->getMockBuilder(
  80. \Magento\NewRelicReporting\Model\ResourceModel\Module\Collection::class
  81. )
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $itemMock = $this->createMock(\Magento\NewRelicReporting\Model\Module::class);
  85. $modulesMockArray = [
  86. 'Module_Name' => [
  87. 'name' => 'Name',
  88. 'setup_version' => '2.0.0',
  89. 'sequence' => []
  90. ]
  91. ];
  92. $testChangesMockArray = [
  93. ['entity' => '3',
  94. 'name' => 'Name',
  95. 'active' => 'true',
  96. 'state' => 'enabled',
  97. 'setup_version' => '2.0.0',
  98. 'updated_at' => '2015-09-02 18:38:17'],
  99. ['entity' => '4',
  100. 'name' => 'Name',
  101. 'active' => 'true',
  102. 'state' => 'disabled',
  103. 'setup_version' => '2.0.0',
  104. 'updated_at' => '2015-09-02 18:38:17'],
  105. ['entity' => '5',
  106. 'name' => 'Name',
  107. 'active' => 'true',
  108. 'state' => 'uninstalled',
  109. 'setup_version' => '2.0.0',
  110. 'updated_at' => '2015-09-02 18:38:17']
  111. ];
  112. $itemMockArray = [$itemMock];
  113. $enabledModulesMockArray = [];
  114. $this->moduleCollectionFactoryMock->expects($this->any())
  115. ->method('create')
  116. ->willReturn($moduleCollectionMock);
  117. $this->moduleFactoryMock->expects($this->any())
  118. ->method('create')
  119. ->willReturn($itemMock);
  120. $itemMock->expects($this->any())
  121. ->method('setData')
  122. ->willReturnSelf();
  123. $itemMock->expects($this->any())
  124. ->method('save')
  125. ->willReturnSelf();
  126. $moduleCollectionMock->expects($this->any())
  127. ->method('getItems')
  128. ->willReturn($itemMockArray);
  129. $moduleCollectionMock->expects($this->any())
  130. ->method('getData')
  131. ->willReturn($testChangesMockArray);
  132. $this->fullModuleListMock->expects($this->once())
  133. ->method('getAll')
  134. ->willReturn($modulesMockArray);
  135. $this->fullModuleListMock->expects($this->once())
  136. ->method('getNames')
  137. ->willReturn($enabledModulesMockArray);
  138. $this->moduleListMock->expects($this->once())
  139. ->method('getNames')
  140. ->willReturn($enabledModulesMockArray);
  141. $this->moduleManagerMock->expects($this->any())->method('isOutputEnabled')->will(
  142. $this->returnValue(false)
  143. );
  144. $this->assertInternalType(
  145. 'array',
  146. $this->model->getModuleData()
  147. );
  148. }
  149. /**
  150. * Tests modules data returns array and saving in DB
  151. *
  152. * @dataProvider itemDataProvider
  153. * @return void
  154. */
  155. public function testGetModuleDataRefresh($data)
  156. {
  157. $moduleCollectionMock = $this->getMockBuilder(
  158. \Magento\NewRelicReporting\Model\ResourceModel\Module\Collection::class
  159. )
  160. ->disableOriginalConstructor()
  161. ->getMock();
  162. /** @var \Magento\NewRelicReporting\Model\Module|\PHPUnit_Framework_MockObject_MockObject $itemMock */
  163. $itemMock = $this->createPartialMock(
  164. \Magento\NewRelicReporting\Model\Module::class,
  165. ['getName', 'getData', 'setData', 'getState', 'save']
  166. );
  167. $modulesMockArray = [
  168. 'Module_Name1' => [
  169. 'name' => 'Module_Name1',
  170. 'setup_version' => '2.0.0',
  171. 'sequence' => []
  172. ]
  173. ];
  174. $itemMock->setData($data);
  175. $testChangesMockArray = [
  176. 'entity_id' => '3',
  177. 'name' => 'Name',
  178. 'active' => 'true',
  179. 'state' => 'uninstalled',
  180. 'setup_version' => '2.0.0',
  181. 'some_param' => 'some_value',
  182. 'updated_at' => '2015-09-02 18:38:17'
  183. ];
  184. $itemMockArray = [$itemMock];
  185. $enabledModulesMockArray = ['Module_Name2'];
  186. $allModulesMockArray = ['Module_Name1','Module_Name2'];
  187. $this->moduleCollectionFactoryMock->expects($this->any())
  188. ->method('create')
  189. ->willReturn($moduleCollectionMock);
  190. $this->moduleFactoryMock->expects($this->any())
  191. ->method('create')
  192. ->willReturn($itemMock);
  193. $itemMock->expects($this->any())
  194. ->method('setData')
  195. ->willReturnSelf();
  196. $itemMock->expects($this->any())
  197. ->method('save')
  198. ->willReturnSelf();
  199. $itemMock->expects($this->any())
  200. ->method('getState')
  201. ->willReturn($data['state']);
  202. $itemMock->expects($this->any())
  203. ->method('getName')
  204. ->willReturn($data['name']);
  205. $moduleCollectionMock->expects($this->any())
  206. ->method('getItems')
  207. ->willReturn($itemMockArray);
  208. $itemMock->expects($this->any())
  209. ->method('getData')
  210. ->willReturn($testChangesMockArray);
  211. $this->fullModuleListMock->expects($this->once())
  212. ->method('getAll')
  213. ->willReturn($modulesMockArray);
  214. $this->fullModuleListMock->expects($this->any())
  215. ->method('getNames')
  216. ->willReturn($allModulesMockArray);
  217. $this->moduleListMock->expects($this->any())
  218. ->method('getNames')
  219. ->willReturn($enabledModulesMockArray);
  220. $this->moduleManagerMock->expects($this->any())->method('isOutputEnabled')->will(
  221. $this->returnValue(true)
  222. );
  223. $this->assertInternalType(
  224. 'array',
  225. $this->model->getModuleData()
  226. );
  227. }
  228. /**
  229. * Tests modules data returns array and saving in DB
  230. *
  231. * @dataProvider itemDataProvider
  232. * @return void
  233. */
  234. public function testGetModuleDataRefreshOrStatement($data)
  235. {
  236. $moduleCollectionMock = $this->getMockBuilder(
  237. \Magento\NewRelicReporting\Model\ResourceModel\Module\Collection::class
  238. )
  239. ->disableOriginalConstructor()
  240. ->getMock();
  241. /** @var \Magento\NewRelicReporting\Model\Module|\PHPUnit_Framework_MockObject_MockObject $itemMock */
  242. $itemMock = $this->createPartialMock(
  243. \Magento\NewRelicReporting\Model\Module::class,
  244. ['getName', 'getData', 'setData', 'getState', 'save']
  245. );
  246. $modulesMockArray = [
  247. 'Module_Name1' => [
  248. 'name' => 'Module_Name1',
  249. 'setup_version' => '2.0.0',
  250. 'sequence' => []
  251. ]
  252. ];
  253. $itemMock->setData($data);
  254. $testChangesMockArray = [
  255. 'entity_id' => '3',
  256. 'name' => 'Name',
  257. 'active' => 'false',
  258. 'state' => 'enabled',
  259. 'setup_version' => '2.0.0',
  260. 'some_param' => 'some_value',
  261. 'updated_at' => '2015-09-02 18:38:17'
  262. ];
  263. $itemMockArray = [$itemMock];
  264. $enabledModulesMockArray = ['Module_Name2'];
  265. $allModulesMockArray = ['Module_Name1','Module_Name2'];
  266. $this->moduleCollectionFactoryMock->expects($this->any())
  267. ->method('create')
  268. ->willReturn($moduleCollectionMock);
  269. $this->moduleFactoryMock->expects($this->any())
  270. ->method('create')
  271. ->willReturn($itemMock);
  272. $itemMock->expects($this->any())
  273. ->method('setData')
  274. ->willReturnSelf();
  275. $itemMock->expects($this->any())
  276. ->method('save')
  277. ->willReturnSelf();
  278. $itemMock->expects($this->any())
  279. ->method('getState')
  280. ->willReturn($data['state']);
  281. $itemMock->expects($this->any())
  282. ->method('getName')
  283. ->willReturn($data['name']);
  284. $moduleCollectionMock->expects($this->any())
  285. ->method('getItems')
  286. ->willReturn($itemMockArray);
  287. $itemMock->expects($this->any())
  288. ->method('getData')
  289. ->willReturn($testChangesMockArray);
  290. $this->fullModuleListMock->expects($this->once())
  291. ->method('getAll')
  292. ->willReturn($modulesMockArray);
  293. $this->fullModuleListMock->expects($this->any())
  294. ->method('getNames')
  295. ->willReturn($allModulesMockArray);
  296. $this->moduleListMock->expects($this->any())
  297. ->method('getNames')
  298. ->willReturn($enabledModulesMockArray);
  299. $this->moduleManagerMock->expects($this->any())->method('isOutputEnabled')->will(
  300. $this->returnValue(true)
  301. );
  302. $this->assertInternalType(
  303. 'array',
  304. $this->model->getModuleData()
  305. );
  306. }
  307. /**
  308. * @return array
  309. */
  310. public function itemDataProvider()
  311. {
  312. return [
  313. [
  314. [
  315. 'entity_id' => '1',
  316. 'name' => 'Module_Name1',
  317. 'active' => 'true',
  318. 'state' => 'enabled',
  319. 'setup_version' => '2.0.0'
  320. ]
  321. ],
  322. [
  323. [
  324. 'entity_id' => '2',
  325. 'name' => 'Module_Name2',
  326. 'active' => 'true',
  327. 'state' => 'disabled',
  328. 'setup_version' => '2.0.0'
  329. ]
  330. ],
  331. [
  332. [
  333. 'entity_id' => '2',
  334. 'name' => 'Module_Name2',
  335. 'active' => 'true',
  336. 'state' => 'uninstalled',
  337. 'setup_version' => '2.0.0'
  338. ]
  339. ]
  340. ];
  341. }
  342. }