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

Commit a1d393a

Browse files
Merge pull request #30 from jrondorf/1.0
Add Yaml format
2 parents 1a06682 + 83f864d commit a1d393a

7 files changed

Lines changed: 1928 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ The Default Format Class: `JSON`
127127
\Filebase\Format\Json::class
128128
```
129129

130+
Additional Format Classes: `Yaml`
131+
```php
132+
\Filebase\Format\Yaml::class
133+
```
130134

131135
## (3) GET (and methods)
132136

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818
},
1919

2020
"require-dev": {
21-
"satooshi/php-coveralls": "^2.0",
22-
"phpunit/phpunit": "5.*"
21+
"php-coveralls/php-coveralls": "^2.0",
22+
"phpunit/phpunit": "5.*",
23+
"symfony/yaml": "^4.1"
2324
},
2425

26+
"suggest": {
27+
"symfony/yaml": "Allows Yaml format"
28+
},
29+
2530
"autoload": {
2631
"psr-4": {
2732
"Filebase\\": "src/"

src/Format/Yaml.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php namespace Filebase\Format;
2+
3+
use Symfony\Component\Yaml\Yaml as YamlParser;
4+
5+
class Yaml implements FormatInterface
6+
{
7+
/**
8+
* @return string
9+
*/
10+
public static function getFileExtension()
11+
{
12+
return 'yaml';
13+
}
14+
15+
16+
//--------------------------------------------------------------------
17+
18+
19+
/**
20+
* @param array $data
21+
* @param bool $pretty
22+
* @return string
23+
* @throws FormatException
24+
*/
25+
public static function encode($data = [], $pretty = true)
26+
{
27+
$encoded = YamlParser::dump((array)$data);
28+
return $encoded;
29+
}
30+
31+
32+
//--------------------------------------------------------------------
33+
34+
35+
/**
36+
* @param $data
37+
* @return mixed
38+
* @throws FormatException
39+
*/
40+
public static function decode($data)
41+
{
42+
$decoded = YamlParser::parse($data);
43+
return $decoded;
44+
}
45+
46+
47+
//--------------------------------------------------------------------
48+
49+
50+
}

tests/BackupYamlTest.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php namespace Filebase;
2+
3+
class BackupYamlTest extends \PHPUnit\Framework\TestCase
4+
{
5+
6+
public function testBackupLocationCustom()
7+
{
8+
$db = new \Filebase\Database([
9+
'dir' => __DIR__.'/databases/mydatabasetobackup',
10+
'backupLocation' => __DIR__.'/databases/storage/backups',
11+
'format' => \Filebase\Format\Yaml::class
12+
]);
13+
14+
$db->backup();
15+
16+
$this->assertEquals(true, true);
17+
}
18+
19+
20+
public function testBackupLocationDefault()
21+
{
22+
23+
$db = new \Filebase\Database([
24+
'dir' => __DIR__.'/databases/mydatabasetobackup',
25+
'format' => \Filebase\Format\Yaml::class
26+
]);
27+
28+
$db->backup();
29+
30+
$this->assertEquals(true, true);
31+
}
32+
33+
34+
public function testBackupCreate()
35+
{
36+
$db = new \Filebase\Database([
37+
'dir' => __DIR__.'/databases/mydatabasetobackup',
38+
'backupLocation' => __DIR__.'/databases/storage/backups',
39+
'format' => \Filebase\Format\Yaml::class
40+
]);
41+
42+
$db->flush(true);
43+
44+
for ($x = 1; $x <= 25; $x++)
45+
{
46+
$user = $db->get(uniqid());
47+
$user->name = 'John';
48+
$user->save();
49+
}
50+
51+
$file = $db->backup()->create();
52+
53+
$db->flush(true);
54+
55+
$this->assertRegExp('/[0-9]+\.zip$/',$file);
56+
}
57+
58+
59+
public function testBackupFind()
60+
{
61+
$db = new \Filebase\Database([
62+
'dir' => __DIR__.'/databases/mydatabasetobackup',
63+
'backupLocation' => __DIR__.'/databases/storage/backups',
64+
'format' => \Filebase\Format\Yaml::class
65+
]);
66+
67+
$db->flush(true);
68+
69+
for ($x = 1; $x <= 25; $x++)
70+
{
71+
$user = $db->get(uniqid());
72+
$user->name = 'John';
73+
$user->save();
74+
}
75+
76+
$db->backup()->create();
77+
78+
$backups = $db->backup()->find();
79+
80+
$this->assertInternalType('array',$backups);
81+
$this->assertNotEmpty($backups);
82+
}
83+
84+
85+
public function testBackupFindSort()
86+
{
87+
$db = new \Filebase\Database([
88+
'dir' => __DIR__.'/databases/mydatabasetobackup',
89+
'backupLocation' => __DIR__.'/databases/storage/backups',
90+
'format' => \Filebase\Format\Yaml::class
91+
]);
92+
93+
$db->flush(true);
94+
95+
for ($x = 1; $x <= 25; $x++)
96+
{
97+
$user = $db->get(uniqid());
98+
$user->name = 'John';
99+
$user->save();
100+
}
101+
102+
$db->backup()->create();
103+
$db->backup()->create();
104+
$last = str_replace('.zip','',$db->backup()->create());
105+
106+
$backups = $db->backup()->find();
107+
$backupCurrent = current($backups);
108+
109+
$lastBackup = str_replace('.zip','',basename($backupCurrent));
110+
111+
$db->flush(true);
112+
113+
$this->assertEquals($last,$lastBackup);
114+
}
115+
116+
117+
public function testBackupCleanup()
118+
{
119+
$db = new \Filebase\Database([
120+
'dir' => __DIR__.'/databases/mydatabasetobackup',
121+
'backupLocation' => __DIR__.'/databases/storage/backups',
122+
'format' => \Filebase\Format\Yaml::class
123+
]);
124+
125+
$backupBefore = $db->backup()->find();
126+
127+
$db->backup()->clean();
128+
$backupAfter = $db->backup()->find();
129+
130+
$this->assertInternalType('array',$backupBefore);
131+
$this->assertNotEmpty($backupBefore);
132+
133+
$this->assertInternalType('array',$backupAfter);
134+
$this->assertEmpty($backupAfter);
135+
}
136+
137+
138+
public function testBackupRestore()
139+
{
140+
$db1 = new \Filebase\Database([
141+
'dir' => __DIR__.'/databases/backupdb',
142+
'backupLocation' => __DIR__.'/databases/storage/backupdb',
143+
'format' => \Filebase\Format\Yaml::class
144+
]);
145+
146+
for ($x = 1; $x <= 25; $x++)
147+
{
148+
$user = $db1->get(uniqid());
149+
$user->name = 'John';
150+
$user->save();
151+
}
152+
153+
$db1->backup()->create();
154+
155+
$items1 = $db1->count();
156+
157+
$db2 = new \Filebase\Database([
158+
'dir' => __DIR__.'/databases/backupdb2',
159+
'backupLocation' => __DIR__.'/databases/storage/backupdb',
160+
'format' => \Filebase\Format\Yaml::class
161+
]);
162+
163+
$db2->backup()->rollback();
164+
$db2->backup()->clean();
165+
166+
$items2 = $db2->count();
167+
168+
$db1->flush(true);
169+
$db2->flush(true);
170+
171+
$this->assertEquals($items1,$items2);
172+
173+
}
174+
175+
176+
}

0 commit comments

Comments
 (0)