Skip to content

Commit

Permalink
test: Add BackupObj test & transfer EntryObj to Dto namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Apr 14, 2024
1 parent da1e7d1 commit ba93bdb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Dto/BackupObj.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function toArray(): array
return [
'real_name' => $this->name,
'name' => $this->name,
'created_at' => Carbon::createFromTimestamp($this->createdAt)->format(config('env-editor.timeFormat')),
'modified_at' => Carbon::createFromTimestamp($this->modifiedAt)->format(config('env-editor.timeFormat')),
'created_at' => $this->createdAt->format(config('env-editor.timeFormat')),
'modified_at' => $this->modifiedAt->format(config('env-editor.timeFormat')),
'raw_content' => $this->rawContent,
'path' => $this->path,
'entries' => $this->entries->toArray(),
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Dto/BackupObjTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace GeoSot\EnvEditor\Tests\Unit\Dto;

use GeoSot\EnvEditor\Dto\BackupObj;
use GeoSot\EnvEditor\Dto\EntryObj;
use GeoSot\EnvEditor\Tests\TestCase;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Test;

class BackupObjTest extends TestCase
{
#[Test]
public function it_returns_data_as_array(): void
{
$this->app['config']->set('env-editor.timeFormat', 'd/m H:i');
$this->freezeTime();
$now = now();

$data = [
'name' => 'foo-file',
'createdAt' => $now->clone()->subHour(),
'modifiedAt' => $now->clone(),
'rawContent' => 'dummy-content',
'path' => 'foo-path',
'entries' => new Collection([
new EntryObj('key',
'value', 1, 1),
]),
];

$dto = new BackupObj(...$data);

$expected = [
'name' => 'foo-file',
'real_name' => 'foo-file',
'created_at' => $now->clone()->subHour()->format('d/m H:i'),
'modified_at' => $now->clone()->format('d/m H:i'),
'raw_content' => 'dummy-content',
'path' => 'foo-path',
'entries' => $data['entries']->toArray(),
];
$this->assertEquals($expected, $dto->jsonSerialize());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace GeoSot\EnvEditor\Tests\Unit\Helpers;
namespace GeoSot\EnvEditor\Tests\Unit\Dto;

use GeoSot\EnvEditor\Dto\EntryObj;
use GeoSot\EnvEditor\Tests\TestCase;
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/Helpers/FilesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace GeoSot\EnvEditor\Tests\Unit\Helpers;

use GeoSot\EnvEditor\Dto\EntryObj;
use GeoSot\EnvEditor\EnvEditor;
use GeoSot\EnvEditor\Exceptions\EnvException;
use GeoSot\EnvEditor\Helpers\EnvFilesManager;
use GeoSot\EnvEditor\Tests\TestCase;
use Illuminate\Config\Repository;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

Expand Down Expand Up @@ -130,7 +132,10 @@ public function back_up_current_env_works_and_returns_bool(): void
$backUps = $manager->getAllBackUps();

$this->assertEquals(1, $backUps->count());
$this->assertEquals($backUps->first()->rawContent, $content);
$backup = $backUps->first();
$this->assertInstanceOf(Collection::class, $backup->entries);
$this->assertInstanceOf(EntryObj::class, $backup->entries->first());
$this->assertEquals($backup->rawContent, $content);

unlink($file);
}
Expand Down

0 comments on commit ba93bdb

Please sign in to comment.