Skip to content

Commit 98f69e3

Browse files
committed
added tests and license
1 parent 091a144 commit 98f69e3

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

composer.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "frostealth/php-data-storage",
33
"description": "PHP Data Storage",
4-
"version" : "1.0.0",
4+
"keywords": ["storage"],
5+
"license": "MIT",
56
"authors": [
67
{
78
"name": "Kudinov Ivan",
@@ -10,10 +11,18 @@
1011
],
1112
"autoload": {
1213
"psr-4": {
13-
"frostealth\\Storage\\": "src/"
14+
"frostealth\\": "src/"
1415
}
1516
},
1617
"require": {
1718
"php": ">=5.4.0"
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"frostealth\\Storage\\Tests\\": "tests/"
23+
}
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "4.2.*"
1827
}
1928
}

phpunit.xml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="true">
8+
9+
<testsuites>
10+
<testsuite name="Container Tests">
11+
<directory>./tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
15+
</phpunit>
File renamed without changes.

tests/DataTest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace frostealth\Storage\Tests;
4+
5+
use frostealth\Storage\Data;
6+
7+
/**
8+
* Class DataTest
9+
*
10+
* @package frostealth\Storage\Tests
11+
*/
12+
class DataTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function testSetAndGet()
15+
{
16+
$storage = new Data();
17+
18+
$this->assertNull($storage->get('key'));
19+
20+
$value = 'A value';
21+
$storage->set('key', $value);
22+
23+
$this->assertSame($value, $storage->get('key'));
24+
}
25+
26+
public function testHas()
27+
{
28+
$storage = new Data();
29+
30+
$this->assertFalse($storage->has('key'));
31+
32+
$storage->set('key', 'a value');
33+
$this->assertTrue($storage->has('key'));
34+
}
35+
36+
public function testRemoveAndClear()
37+
{
38+
$storage = new Data();
39+
40+
$storage->set('key1', 1);
41+
$storage->set('key2', 2);
42+
$storage->set('key3', 3);
43+
44+
$storage->remove('key3');
45+
$this->assertNull($storage->get('key3'));
46+
47+
$storage->clear();
48+
$this->assertNull($storage->get('key1'));
49+
$this->assertNull($storage->get('key2'));
50+
}
51+
52+
public function testFillAndAll()
53+
{
54+
$arr = [
55+
'key1' => 'one',
56+
'key2' => 'two',
57+
];
58+
$storage = new Data();
59+
60+
$storage->fill($arr);
61+
$this->assertSame($arr['key1'], $storage->get('key1'));
62+
$this->assertSame($arr['key2'], $storage->get('key2'));
63+
$this->assertSame($arr, $storage->all());
64+
65+
$arr = ['a value'];
66+
$storage->fill($arr);
67+
$this->assertSame($arr, $storage->all());
68+
}
69+
70+
public function testKeys()
71+
{
72+
$arr = [
73+
'key1' => 1,
74+
'key2' => 2,
75+
'key3' => 3,
76+
];
77+
$storage = new Data();
78+
79+
$storage->fill($arr);
80+
$this->assertSame(array_keys($arr), $storage->keys());
81+
}
82+
83+
public function testDefault()
84+
{
85+
$storage = new Data();
86+
87+
$this->assertFalse($storage->get('key', false));
88+
$this->assertSame(0, $storage->get('key', 0));
89+
90+
$storage->set('key', 'a value');
91+
$this->assertNotFalse($storage->get('key', false));
92+
}
93+
94+
public function testReplace()
95+
{
96+
$arr1 = [
97+
'key1' => 1,
98+
'key2' => 2,
99+
'key3' => 3,
100+
'key4' => [
101+
'key1' => 'one',
102+
'key2' => 'two',
103+
],
104+
];
105+
$arr2 = [
106+
'key2' => 'two',
107+
'key3' => 'three',
108+
'key4' => [
109+
'key2' => 2,
110+
'key3' => 3,
111+
],
112+
];
113+
$storage = new Data();
114+
115+
$storage->fill($arr1);
116+
$storage->replace($arr2);
117+
$this->assertSame(array_replace($arr1, $arr2), $storage->all());
118+
119+
$storage->fill($arr1);
120+
$storage->replace($arr2, true);
121+
$this->assertSame(array_replace_recursive($arr1, $arr2), $storage->all());
122+
}
123+
}
124+

0 commit comments

Comments
 (0)