Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CacheTest.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Setup\Model\ConfigOptionsList\Cache as CacheConfigOptionsList;
13 
14 class CacheTest extends \PHPUnit\Framework\TestCase
15 {
19  private $configOptionsList;
20 
24  private $validatorMock;
25 
29  private $deploymentConfigMock;
30 
34  protected function setUp()
35  {
36  $this->validatorMock = $this->createMock(RedisConnectionValidator::class);
37  $this->deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
38 
39  $this->configOptionsList = new CacheConfigOptionsList($this->validatorMock);
40  }
41 
45  public function testGetOptions()
46  {
47  $options = $this->configOptionsList->getOptions();
48  $this->assertCount(5, $options);
49 
50  $this->assertArrayHasKey(0, $options);
51  $this->assertInstanceOf(SelectConfigOption::class, $options[0]);
52  $this->assertEquals('cache-backend', $options[0]->getName());
53 
54  $this->assertArrayHasKey(1, $options);
55  $this->assertInstanceOf(TextConfigOption::class, $options[1]);
56  $this->assertEquals('cache-backend-redis-server', $options[1]->getName());
57 
58  $this->assertArrayHasKey(2, $options);
59  $this->assertInstanceOf(TextConfigOption::class, $options[2]);
60  $this->assertEquals('cache-backend-redis-db', $options[2]->getName());
61 
62  $this->assertArrayHasKey(3, $options);
63  $this->assertInstanceOf(TextConfigOption::class, $options[3]);
64  $this->assertEquals('cache-backend-redis-port', $options[3]->getName());
65 
66  $this->assertArrayHasKey(4, $options);
67  $this->assertInstanceOf(TextConfigOption::class, $options[4]);
68  $this->assertEquals('cache-backend-redis-password', $options[4]->getName());
69  }
70 
74  public function testCreateConfigCacheRedis()
75  {
76  $this->deploymentConfigMock->method('get')->willReturn('');
77 
78  $expectedConfigData = [
79  'cache' => [
80  'frontend' => [
81  'default' => [
82  'backend' => 'Cm_Cache_Backend_Redis',
83  'backend_options' => [
84  'server' => '',
85  'port' => '',
86  'database' => '',
87  'password' => ''
88  ]
89  ]
90  ]
91  ]
92  ];
93 
94  $configData = $this->configOptionsList->createConfig(['cache-backend'=>'redis'], $this->deploymentConfigMock);
95 
96  $this->assertEquals($expectedConfigData, $configData->getData());
97  }
98 
103  {
104  $expectedConfigData = [
105  'cache' => [
106  'frontend' => [
107  'default' => [
108  'backend' => 'Cm_Cache_Backend_Redis',
109  'backend_options' => [
110  'server' => 'localhost',
111  'port' => '1234',
112  'database' => '5',
113  'password' => ''
114  ]
115  ]
116  ]
117  ]
118  ];
119  $options = [
120  'cache-backend' => 'redis',
121  'cache-backend-redis-server' => 'localhost',
122  'cache-backend-redis-port' => '1234',
123  'cache-backend-redis-db' => '5'
124  ];
125 
126  $configData = $this->configOptionsList->createConfig($options, $this->deploymentConfigMock);
127 
128  $this->assertEquals($expectedConfigData, $configData->getData());
129  }
130 
134  public function testValidateWithValidInput()
135  {
136  $options = [
137  'cache-backend' => 'redis',
138  'cache-backend-redis-server' => 'localhost',
139  ];
140  $this->validatorMock->expects($this->once())
141  ->method('isValidConnection')
142  ->with(['host'=>'localhost', 'db'=>'', 'port'=>'', 'password'=>''])
143  ->willReturn(true);
144 
145  $errors = $this->configOptionsList->validate($options, $this->deploymentConfigMock);
146 
147  $this->assertEmpty($errors);
148  }
149 
154  {
155  $invalidCacheOption = 'clay-tablet';
156  $options = ['cache-backend' => $invalidCacheOption];
157 
158  $errors = $this->configOptionsList->validate($options, $this->deploymentConfigMock);
159 
160  $this->assertCount(1, $errors);
161  $this->assertEquals("Invalid cache handler 'clay-tablet'", $errors[0]);
162  }
163 }
$errors
Definition: overview.phtml:9