Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit ad2c3a1

Browse files
committed
Added deeps tests for "auto-type-cast" option
1 parent 7804e39 commit ad2c3a1

File tree

2 files changed

+151
-9
lines changed

2 files changed

+151
-9
lines changed

src/Command/PhpfastcacheSetCommand.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
100100
$cacheKey = $input->getArgument('key');
101101
$cacheValue = $input->getArgument('value');
102102
$cacheTtl = $input->getArgument('ttl');
103-
$autoTypeCast = $input->getOption('auto-type-cast');
103+
$autoTypeCast = (bool) $input->getOption('auto-type-cast');
104104

105105
if (\array_key_exists($driver, $caches[ 'drivers' ])) {
106106
$io->section($driver);
@@ -119,10 +119,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
119119
}
120120
}
121121

122-
if($autoTypeCast && $castedCacheValue !== $cacheValue){
123-
$io->success(\sprintf('Cache item "%s" set to "%s" for %d seconds (automatically type-casted to %s)', $cacheKey, $cacheValue, $cacheItem->getTtl(), \gettype($castedCacheValue)));
124-
}else{
125-
$io->success(\sprintf('Cache item "%s" set to "%s" for %d seconds', $cacheKey, $cacheValue, $cacheItem->getTtl()));
122+
if(!$autoTypeCast || $castedCacheValue === $cacheValue) {
123+
$io->success(\sprintf(
124+
'Cache item "%s" set to "%s" for %d seconds',
125+
$cacheKey,
126+
$cacheValue,
127+
$cacheItem->getTtl()
128+
));
129+
} else {
130+
$io->success(\sprintf(
131+
'Cache item "%s" set to "%s" for %d seconds (automatically type-casted to %s)',
132+
$cacheKey,
133+
$cacheValue,
134+
$cacheItem->getTtl(),
135+
\gettype($castedCacheValue)
136+
));
126137
}
127138

128139
$driverInstance->save($cacheItem);
@@ -137,11 +148,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
137148
*/
138149
protected function autoTypeCast($string)
139150
{
140-
if(\in_array($string, ['true', 'false'], true)){
151+
if(\in_array(\strtolower($string), ['true', 'false'], true)){
141152
return $string === 'true';
142153
}
143154

144-
if($string === 'null'){
155+
if(\strtolower($string) === 'null'){
145156
return null;
146157
}
147158

tests/Command/PhpfastcacheSetCommandTest.php

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,139 @@ public function testCommandSetCacheItem()
4848
'--no-interaction' => true
4949
]);
5050

51-
// Travis fix (.*) due to weird console screen width that truncate to next line
52-
$this->assertRegExp('/Cache item "' . $key . '" set to "' . $value . '" for ' . $ttl . '/', $commandTester->getDisplay());
51+
$this->assertContains('Cache item "' . $key . '" set to "' . $value . '" for ' . $ttl, $commandTester->getDisplay());
52+
}
53+
54+
public function testCommandSetCacheItemWithAutomaticTypeCastingBoolean()
55+
{
56+
$value = 'true';
57+
$ttl = \random_int(2, 10);
58+
$key = 'test' . \random_int(1000, 1999);
59+
60+
$command = $this->application->find('phpfastcache:set');
61+
$commandTester = new CommandTester($command);
62+
$commandTester->execute([
63+
'command' => $command->getName(),
64+
'driver' => 'filecache',
65+
'key' => $key,
66+
'value' => $value,
67+
'ttl' => $ttl,
68+
'-a' => 1,
69+
'--no-interaction' => true
70+
]);
71+
72+
$this->assertContains('Cache item "' . $key . '" set to "' . $value . '" for ' . $ttl, $commandTester->getDisplay());
73+
$this->assertContains('(automatically type-casted to boolean)', $commandTester->getDisplay(), true);
74+
}
75+
76+
public function testCommandSetCacheItemWithAutomaticTypeCastingInteger()
77+
{
78+
$value = '1337';
79+
$ttl = \random_int(2, 10);
80+
$key = 'test' . \random_int(1000, 1999);
81+
82+
$command = $this->application->find('phpfastcache:set');
83+
$commandTester = new CommandTester($command);
84+
$commandTester->execute([
85+
'command' => $command->getName(),
86+
'driver' => 'filecache',
87+
'key' => $key,
88+
'value' => $value,
89+
'ttl' => $ttl,
90+
'-a' => 1,
91+
'--no-interaction' => true
92+
]);
93+
94+
$this->assertContains('Cache item "' . $key . '" set to "' . $value . '" for ' . $ttl, $commandTester->getDisplay());
95+
$this->assertContains('(automatically type-casted to integer)', $commandTester->getDisplay(), true);
96+
}
97+
98+
public function testCommandSetCacheItemWithAutomaticTypeCastingFloat()
99+
{
100+
$value = '1337.666';
101+
$ttl = \random_int(2, 10);
102+
$key = 'test' . \random_int(1000, 1999);
103+
104+
$command = $this->application->find('phpfastcache:set');
105+
$commandTester = new CommandTester($command);
106+
$commandTester->execute([
107+
'command' => $command->getName(),
108+
'driver' => 'filecache',
109+
'key' => $key,
110+
'value' => $value,
111+
'ttl' => $ttl,
112+
'-a' => 1,
113+
'--no-interaction' => true
114+
]);
115+
116+
$this->assertContains('Cache item "' . $key . '" set to "' . $value . '" for ' . $ttl, $commandTester->getDisplay());
117+
$this->assertContains('(automatically type-casted to double)', $commandTester->getDisplay(), true);
118+
}
119+
120+
public function testCommandSetCacheItemWithAutomaticTypeCastingNull()
121+
{
122+
$value = 'null';
123+
$ttl = \random_int(2, 10);
124+
$key = 'test' . \random_int(1000, 1999);
125+
126+
$command = $this->application->find('phpfastcache:set');
127+
$commandTester = new CommandTester($command);
128+
$commandTester->execute([
129+
'command' => $command->getName(),
130+
'driver' => 'filecache',
131+
'key' => $key,
132+
'value' => $value,
133+
'ttl' => $ttl,
134+
'-a' => 1,
135+
'--no-interaction' => true
136+
]);
137+
138+
$this->assertContains('Cache item "' . $key . '" set to "' . $value . '" for ' . $ttl, $commandTester->getDisplay());
139+
$this->assertContains('(automatically type-casted to NULL)', $commandTester->getDisplay(), true);
140+
}
141+
142+
public function testCommandSetCacheItemWithAutomaticTypeCastingJson()
143+
{
144+
$value = '{"test": 1337}';
145+
$ttl = \random_int(2, 10);
146+
$key = 'test' . \random_int(1000, 1999);
147+
148+
$command = $this->application->find('phpfastcache:set');
149+
$commandTester = new CommandTester($command);
150+
$commandTester->execute([
151+
'command' => $command->getName(),
152+
'driver' => 'filecache',
153+
'key' => $key,
154+
'value' => $value,
155+
'ttl' => $ttl,
156+
'-a' => 1,
157+
'--no-interaction' => true
158+
]);
159+
160+
$this->assertContains('Cache item "' . $key . '" set to "' . $value . '" for ' . $ttl, $commandTester->getDisplay());
161+
$this->assertContains('(automatically type-casted to array)', $commandTester->getDisplay(), true);
162+
}
163+
164+
public function testCommandSetCacheItemWithoutAutomaticTypeCasting()
165+
{
166+
$value = 'null';
167+
$ttl = \random_int(2, 10);
168+
$key = 'test' . \random_int(1000, 1999);
169+
170+
$command = $this->application->find('phpfastcache:set');
171+
$commandTester = new CommandTester($command);
172+
$commandTester->execute([
173+
'command' => $command->getName(),
174+
'driver' => 'filecache',
175+
'key' => $key,
176+
'value' => $value,
177+
'ttl' => $ttl,
178+
'-a' => 0,
179+
'--no-interaction' => true
180+
]);
181+
182+
$this->assertContains('Cache item "' . $key . '" set to "' . $value . '" for ' . $ttl, $commandTester->getDisplay());
183+
$this->assertNotContains('(automatically type-casted to NULL)', $commandTester->getDisplay(), true);
53184
}
54185

55186
/**

0 commit comments

Comments
 (0)