سلام , متد Config::get() d یک آرایه ای از کانفیگ برنامه برمیگردونه اما زمانی که در تست با آرایه ای که خودش در برنامه برمی گردونه سنجدیده می شه .. تست شکست می خوره.
شکست می خوره ؟ getMethodReturnsValidData چرا تست متد
لطفا راهنمایی کنید.
ConfigTest.php
<?php
namespace Tests\unit;
use App\Helpers\Config;
use PHPUnit\Framework\TestCase;
class ConfigTest extends TestCase
{
/**
* @test
*/
public function getFileContentsMethodReturnsArray()
{
$filecontent = Config::getFileContents('database');
$this->assertIsArray($filecontent);
return $filecontent;
}
/**
* @test
*/
public function getFileContentMethodReturnsExceptionIfFileDosenotExists()
{
$this->expectException('App\Exceptions\ConfigFileNotFoundExeption');
Config::getFileContents('foo');
}
/**
* @test
* @depends getFileContentsMethodReturnsArray
*/
public function getFileContentMethodShouldReturnExpectedArray(array $filecontent)
{
$expecteddata = [
'pdo' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'php_tdd_orm',
'db_user' => 'root',
'db_password' => '123456',
]
];
$this->assertEquals($expecteddata, $filecontent);
}
/**
* @test
*/
public function getMethodReturnsValidData()
{
$config = Config::get('database', 'pdo');
$expecteddata = [
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'php_tdd_orm',
'db_user' => 'root',
'db_password' => '123456',
];
$this->assertEquals($expecteddata, $config);
}
}
Config.php
<?php
namespace App\Helpers;
use App\Exceptions\ConfigFileNotFoundExeption;
class Config
{
public static function getFileContents(string $filename)
{
$filepath = realpath(__DIR__."/../Configs/{$filename}.php");
if(!$filepath) {
throw new ConfigFileNotFoundExeption();
}
return require_once $filepath;
}
public static function get(string $filename,$key = null)
{
$filecontents = self::getFileContents($filename);
if(is_null($key)) return $filecontents;
return $filecontents[$key] ?? null;
}
}
سلام , مشکل استفاده از require_ones هست ..زمانی که فایل وجود داشته باشه bool بر میگردونه .. برای همین باید از require استفاده کنیم.