Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ModuleListTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Module\ModuleList;
9 
10 class ModuleListTest extends \PHPUnit\Framework\TestCase
11 {
17  private static $allFixture = ['foo' => ['key' => 'value'], 'bar' => ['another' => 'value']];
18 
24  private static $enabledFixture = ['foo' => 1, 'bar' => 0];
25 
29  private $config;
30 
34  private $loader;
35 
39  private $model;
40 
41  protected function setUp()
42  {
43  $this->config = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
44  $this->loader = $this->createMock(\Magento\Framework\Module\ModuleList\Loader::class);
45  $this->model = new ModuleList($this->config, $this->loader);
46  }
47 
48  public function testGetAll()
49  {
50  $this->config->expects($this->exactly(2))->method('resetData');
51  $this->setLoadAllExpectation();
52  $this->setLoadConfigExpectation();
53  $expected = ['foo' => self::$allFixture['foo']];
54  $this->assertSame($expected, $this->model->getAll());
55  $this->assertSame($expected, $this->model->getAll()); // second time to ensure loadAll is called once
56  }
57 
58  public function testGetAllNoData()
59  {
60  $this->loader->expects($this->exactly(2))->method('load')->willReturn([]);
61  $this->setLoadConfigExpectation(false);
62  $this->assertEquals([], $this->model->getAll());
63  $this->assertEquals([], $this->model->getAll());
64  }
65 
66  public function testGetOne()
67  {
68  $this->config->expects($this->exactly(2))->method('resetData');
69  $this->setLoadAllExpectation();
70  $this->setLoadConfigExpectation();
71  $this->assertSame(['key' => 'value'], $this->model->getOne('foo'));
72  $this->assertNull($this->model->getOne('bar'));
73  }
74 
75  public function testGetNames()
76  {
77  $this->config->expects($this->exactly(2))->method('resetData');
78  $this->setLoadAllExpectation(false);
79  $this->setLoadConfigExpectation();
80  $this->assertSame(['foo'], $this->model->getNames());
81  $this->assertSame(['foo'], $this->model->getNames()); // second time to ensure config loader is called once
82  }
83 
84  public function testHas()
85  {
86  $this->config->expects($this->exactly(2))->method('resetData');
87  $this->setLoadAllExpectation(false);
88  $this->setLoadConfigExpectation();
89  $this->assertTrue($this->model->has('foo'));
90  $this->assertFalse($this->model->has('bar'));
91  }
92 
93  public function testIsModuleInfoAvailable()
94  {
95  $this->config->expects($this->once())->method('resetData');
96  $this->setLoadConfigExpectation(true);
97  $this->assertTrue($this->model->isModuleInfoAvailable());
98  }
99 
101  {
102  $this->config->expects($this->at(0))->method('get')->willReturn(['modules' => 'testModule']);
103  $this->config->expects($this->at(1))->method('get')->willReturn(null);
104  $this->assertFalse($this->model->isModuleInfoAvailable());
105  }
106 
113  private function setLoadConfigExpectation($isExpected = true)
114  {
115  if ($isExpected) {
116  $this->config->expects($this->exactly(2))->method('get')->willReturn(self::$enabledFixture);
117  } else {
118  $this->config->expects($this->never())->method('get');
119  }
120  }
121 
128  private function setLoadAllExpectation($isExpected = true)
129  {
130  if ($isExpected) {
131  $this->loader->expects($this->once())->method('load')->willReturn(self::$allFixture);
132  } else {
133  $this->loader->expects($this->never())->method('load');
134  }
135  }
136 }