59 lines
1.3 KiB
PHP
59 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace absdev\Modules\Tests;
|
|
|
|
use absdev\Modules\Exceptions\InvalidJsonException;
|
|
use absdev\Modules\Json;
|
|
use Orchestra\Testbench\TestCase;
|
|
|
|
class JsonTest extends TestCase
|
|
{
|
|
private string $filename;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->app['config']->set('modules.cache.enabled', false);
|
|
|
|
$filename = tempnam(sys_get_temp_dir(), 'absdev-');
|
|
|
|
if ($filename === false) {
|
|
throw new \RuntimeException('Unable to create test file');
|
|
}
|
|
|
|
$this->filename = $filename;
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if (isset($this->filename) && is_file($this->filename)) {
|
|
unlink($this->filename);
|
|
}
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_reads_module_json(): void
|
|
{
|
|
file_put_contents(
|
|
$this->filename,
|
|
'{"name":"Example","enabled":true}'
|
|
);
|
|
|
|
$json = new Json($this->filename);
|
|
|
|
$this->assertSame('Example', $json->get('name'));
|
|
$this->assertTrue($json->get('enabled'));
|
|
}
|
|
|
|
public function test_rejects_invalid_json(): void
|
|
{
|
|
file_put_contents($this->filename, '{invalid');
|
|
|
|
$this->expectException(InvalidJsonException::class);
|
|
|
|
new Json($this->filename);
|
|
}
|
|
}
|