Skip to content

Commit 737af68

Browse files
committed
add Unit tests
1 parent 250e593 commit 737af68

8 files changed

Lines changed: 263 additions & 9 deletions

File tree

.gitignore

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,33 @@
1-
/vendor/
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer itself is not needed
16+
composer.phar
17+
18+
# Mac DS_Store Files
19+
.DS_Store
20+
21+
# phpunit itself is not needed
22+
phpunit.phar
23+
# local phpunit config
24+
/.phpunit.*
25+
26+
# Swoole Auto Complete
27+
swoole-ide-helper.phar
28+
29+
.env
30+
31+
vendor
32+
233
composer.lock

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
NullCache
1+
NullCache | [中文说明](README_CN.md)
22
----
33

44
This Package Is a `Null` implementation of PSR-16 `SimpleCache` with `Basic data validation`
@@ -34,4 +34,4 @@ $logger = new \ihipop\PsrNullCache\SimpleCache\NullCache(false);
3434

3535
# CacheInterfaceProxy
3636

37-
Use this trait when you want to compatible with PSR `CacheInterface` quickly
37+
Use this trait when you want a quick implementation of PSR `CacheInterface` quickly via a Proxy class.

README_CN.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
NullCache | [ENGLISH](README.md)
2+
----
3+
4+
本包是、 `Null` implementation of PSR-16 `SimpleCache` with `Basic data validation`
5+
6+
# Why this Package ?
7+
8+
There is nothing like [NullLogger](https://github.com/php-fig/log/blob/master/Psr/Log/NullLogger.php) of PSR-3 in PSR-16/PSR-6
9+
10+
## I (Or if you) don't want to write these code every where
11+
12+
```php
13+
if ($this->logger){
14+
$this->logger->error($message,$contex);
15+
}
16+
```
17+
18+
Then You need this Package.
19+
20+
## And want some Basic data validation ?
21+
22+
PSR-16 have some special `InvalidArgumentException` to throw when enter with invalid data.
23+
You Will have these check if you use These Package,`InvalidArgumentException` will be thrown when it is necessary ,
24+
to let you know your problem in earlier.
25+
26+
# Usage
27+
28+
In your `__construction` or `DI container` initialization
29+
```
30+
///...
31+
$logger = new \ihipop\PsrNullCache\SimpleCache\NullCache(false);
32+
///...
33+
```
34+
35+
# CacheInterfaceProxy
36+
37+
Use this trait when you want a quick implementation of PSR `CacheInterface` quickly via a Proxy class.

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "ihipop/psr-null-cache",
33
"type": "library",
4-
"homepage": "https://github.com/ihipop/taobao-top",
5-
"description": "NullCache implementation of PSR-16 with Basic data valid check",
4+
"homepage": "https://github.com/ihipop/PSR-NullCache",
5+
"description": "NullCache implementation (Fake Storage) of PSR-16 with Basic data validation",
66
"license": "LGPL-3.0+",
77
"authors": [
88
{
@@ -21,5 +21,8 @@
2121
"psr-4": {
2222
"ihipop\\PsrNullCache\\": "src/"
2323
}
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "^8"
2427
}
2528
}

phpunit.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="tests/bootstrap.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
>
12+
<testsuites>
13+
<testsuite name="Application Test Suite">
14+
<directory>./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
<exclude>
21+
<directory suffix="ServiceProvider.php">src/</directory>
22+
<directory suffix="Exception.php">src/</directory>
23+
<directory suffix="Helpers.php">src/Kernel/Support</directory>
24+
<directory>src/Encryption</directory>
25+
<directory>src/Support</directory>
26+
</exclude>
27+
</whitelist>
28+
</filter>
29+
<php>
30+
<const name="PHPUNIT_RUNNING" value="true"/>
31+
</php>
32+
</phpunit>

src/SimpleCache/NullCache.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ public function __construct(bool $defaultBool = false)
1515
$this->defaultBool = $defaultBool;
1616
}
1717

18+
public static function typeString($object){
19+
return \is_object($object) ? \get_class($object) : \gettype($object);
20+
}
21+
1822
public static function validateKey($key)
1923
{
2024
if (!\is_string($key)) {
21-
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', \is_object($key) ? \get_class($key) : \gettype($key)));
25+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', self::typeString($key)));
2226
}
2327
if ('' === $key) {
2428
throw new InvalidArgumentException('Cache key length must be greater than zero');
@@ -37,7 +41,7 @@ public static function traversToArray($travers)
3741
}
3842
if (!\is_array($travers)) {
3943
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given',
40-
\is_object($travers) ? \get_class($travers) : \gettype($travers)));
44+
self::typeString($travers)));
4145
}
4246

4347
return $travers;
@@ -89,9 +93,11 @@ public function getMultiple($keys, $default = null)
8993
/** @inheritdoc */
9094
public function setMultiple($values, $ttl = null)
9195
{
92-
$values = self::traversToArray($values);
96+
$KeyValues = self::traversToArray($values);
9397

94-
// self::validateKey($values);
98+
foreach ($KeyValues as $key=>$__){
99+
self::validateKey($key);
100+
}
95101

96102
return $this->defaultBool;
97103
}

tests/SimpleCacheNullCacheTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/**
3+
* @author ihipop@gmail.com @ 19-2-27 下午6:22 For 1688-sdk.
4+
*/
5+
6+
use PHPUnit\Framework\TestCase;
7+
8+
class SimpleCacheNullCacheTest extends TestCase
9+
{
10+
11+
protected $nullCache;
12+
protected $invalidKey = [];
13+
14+
public function __construct(?string $name = null, array $data = [], string $dataName = '')
15+
{
16+
parent::__construct($name, $data, $dataName);
17+
$this->nullCache = new \ihipop\PsrNullCache\SimpleCache\NullCache(false);
18+
$this->invalidKey = str_split('{}()/\@:');
19+
}
20+
21+
public function testInvalidKeySet()
22+
{
23+
$invalidKeyChr = array_merge([new stdClass(), ''], $this->invalidKey);
24+
$totalException = count($invalidKeyChr);
25+
$exceptionContainer = [];
26+
foreach ($invalidKeyChr as $key) {
27+
try {
28+
$this->nullCache->set($key, 'foobar');
29+
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
30+
$exceptionContainer[] = $e;
31+
}
32+
}
33+
34+
$this->assertEquals($totalException, count($exceptionContainer));
35+
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
36+
}
37+
38+
public function testInvalidKeyGet()
39+
{
40+
$invalidKeyChr = array_merge([new stdClass(), ''], $this->invalidKey);
41+
$totalException = count($invalidKeyChr);
42+
$exceptionContainer = [];
43+
foreach ($invalidKeyChr as $key) {
44+
try {
45+
$this->nullCache->get($key);
46+
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
47+
$exceptionContainer[] = $e;
48+
}
49+
}
50+
51+
$this->assertEquals($totalException, count($exceptionContainer));
52+
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
53+
}
54+
55+
public function testInvalidKeyDelete()
56+
{
57+
$invalidKeyChr = array_merge([new stdClass(), ''], $this->invalidKey);
58+
$totalException = count($invalidKeyChr);
59+
foreach ($invalidKeyChr as $key) {
60+
try {
61+
$this->nullCache->delete($key);
62+
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
63+
$exceptionContainer[] = $e;
64+
}
65+
}
66+
67+
$this->assertEquals($totalException, count($exceptionContainer));
68+
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
69+
}
70+
71+
public function testInvalidKeyHas()
72+
{
73+
$invalidKeyChr = array_merge([new stdClass(), ''], $this->invalidKey);
74+
$totalException = count($invalidKeyChr);
75+
$exceptionContainer = [];
76+
foreach ($invalidKeyChr as $key) {
77+
try {
78+
$this->nullCache->has($key);
79+
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
80+
$exceptionContainer[] = $e;
81+
}
82+
}
83+
84+
$this->assertEquals($totalException, count($exceptionContainer));
85+
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
86+
}
87+
88+
public function testInvalidKeySetMultiple()
89+
{
90+
$invalidKeyChr = array_merge([''], $this->invalidKey);
91+
$totalException = count($invalidKeyChr);
92+
$exceptionContainer = [];
93+
foreach ($invalidKeyChr as $key) {
94+
try {
95+
$this->nullCache->setMultiple(['foo' => 'bar', $key => 'bar']);
96+
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
97+
$exceptionContainer[] = $e;
98+
}
99+
}
100+
101+
$this->assertEquals($totalException, count($exceptionContainer));
102+
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
103+
}
104+
105+
public function testInvalidKeyGetMultiple()
106+
{
107+
$invalidKeyChr = array_merge([''], $this->invalidKey);
108+
$totalException = count($invalidKeyChr);
109+
$exceptionContainer = [];
110+
foreach ($invalidKeyChr as $key) {
111+
try {
112+
foreach ($this->nullCache->getMultiple(['foo', $key]) as $iter) {
113+
// fo nothing
114+
}
115+
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
116+
$exceptionContainer[] = $e;
117+
}
118+
}
119+
120+
$this->assertEquals($totalException, count($exceptionContainer));
121+
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
122+
}
123+
124+
public function testInvalidKeyDeleteMultiple()
125+
{
126+
$invalidKeyChr = array_merge([''], $this->invalidKey);
127+
$totalException = count($invalidKeyChr);
128+
$exceptionContainer = [];
129+
foreach ($invalidKeyChr as $key) {
130+
try {
131+
$this->nullCache->deleteMultiple(['foo', $key]);
132+
} catch (\Psr\SimpleCache\InvalidArgumentException $e) {
133+
$exceptionContainer[] = $e;
134+
}
135+
}
136+
137+
$this->assertEquals($totalException, count($exceptionContainer));
138+
$this->assertContainsOnlyInstancesOf(\Psr\SimpleCache\InvalidArgumentException::class, $exceptionContainer);
139+
}
140+
}

tests/bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
define('TEST_ROOT', __DIR__);
4+
//define('STUBS_ROOT', __DIR__ . '/stubs');
5+
include __DIR__ . '/../vendor/autoload.php';

0 commit comments

Comments
 (0)