-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PoC Config objects #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
logFile: /var/log/simplesamlphp.log | ||
lines: 1500 | ||
blockSize: 8192 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Module\logpeek\Config; | ||
|
||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\Configuration; | ||
use SimpleSAML\Utils; | ||
use Symfony\Component\Yaml\Yaml; | ||
use Symfony\Component\Yaml\Exception\ParseException; | ||
|
||
/** | ||
* Configuration for the module logpeek. | ||
*/ | ||
|
||
class Logpeek { | ||
/** @var string */ | ||
public const DEFAULT_CONFIGFILE = 'module_logpeek.yml'; | ||
|
||
/** @var int */ | ||
public const MAX_BLOCKSIZE = 8192; | ||
|
||
/** @var int */ | ||
public const DEFAULT_BLOCKSIZE = 8192; | ||
|
||
/** @var int */ | ||
public const DEFAULT_LINES = 500; | ||
|
||
/** @var string */ | ||
private string $logFile; | ||
|
||
/** @var int */ | ||
private int $lines; | ||
|
||
/** @var int */ | ||
private int $blockSize; | ||
|
||
|
||
/** | ||
* @param string|null $logFile | ||
* @param int $blockSize | ||
* @param int $lines | ||
*/ | ||
public function __construct(?string $logFile, ?int $blockSize = null, ?int $lines = null) | ||
{ | ||
if ($logFile === null) { | ||
$config = Configuration::getInstance(); | ||
$loggingDir = $config->getPathValue('loggingdir', 'log/'); | ||
$logFile = $loggingDir . $config->getString('logging.logfile', 'simplesamlphp.log'); | ||
} | ||
|
||
$this->setLogFile($logFile); | ||
$this->setBlockSize($blockSize ?? self::DEFAULT_BLOCKSIZE); | ||
$this->setLines($lines ?? self::DEFAULT_LINES); | ||
} | ||
|
||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getLogFile(): string | ||
{ | ||
return $this->logFile; | ||
} | ||
|
||
|
||
/** | ||
* @param string $logFile | ||
* @return void | ||
*/ | ||
protected function setLogFile(string $logFile): void | ||
{ | ||
$this->logFile = $logFile; | ||
} | ||
|
||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getLines(): int | ||
{ | ||
return $this->lines; | ||
} | ||
|
||
|
||
/** | ||
* @param int $lines | ||
* @return void | ||
*/ | ||
protected function setLines(int $lines): void | ||
{ | ||
Assert::positiveInteger($lines); | ||
$this->lines = $lines; | ||
} | ||
|
||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getBlockSize(): int | ||
{ | ||
return $this->blockSize; | ||
} | ||
|
||
|
||
/** | ||
* @param int $blockSize | ||
* @return void | ||
*/ | ||
protected function setBlockSize(int $blockSize): void | ||
{ | ||
Assert::range($blockSize, 0, self::MAX_BLOCKSIZE); | ||
$this->blockSize = $blockSize; | ||
} | ||
|
||
|
||
/** | ||
* @param string $file | ||
* @return self | ||
*/ | ||
public static function fromPhp(string $configFile = 'module_logpeek.php'): self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For BC compatibility |
||
{ | ||
$configUtils = new Utils\Config(); | ||
$configDir = $configUtils->getConfigDir(); | ||
include($configDir . '/' . $configFile); | ||
|
||
return static::fromArray($config ?? []); | ||
} | ||
|
||
|
||
/** | ||
* @param string $file | ||
* @return self | ||
*/ | ||
public static function fromYaml(string $configFile = self::DEFAULT_CONFIGFILE): self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is completely generic, so could be moved to an AbstractConfigObject |
||
{ | ||
$configUtils = new Utils\Config(); | ||
$configDir = $configUtils->getConfigDir(); | ||
$yamlConfig = Yaml::parse(file_get_contents($configDir . '/' . $configFile)); | ||
|
||
return static::fromArray($yamlConfig ?? []); | ||
} | ||
|
||
|
||
/** | ||
* @param array $config | ||
* @return self | ||
*/ | ||
public static function fromArray(array $config): self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be implemented to assist unit testing |
||
{ | ||
return new self($config['logFile'], $config['blockSize'], $config['lines']); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Configuration; | ||
use SimpleSAML\Logger; | ||
use SimpleSAML\Module\logpeek\Config; | ||
use SimpleSAML\Module\logpeek\Controller; | ||
use SimpleSAML\Session; | ||
use SimpleSAML\Utils; | ||
|
@@ -29,6 +30,9 @@ class LogpeekTest extends TestCase | |
/** @var \SimpleSAML\Configuration */ | ||
protected Configuration $config; | ||
|
||
/** @var \SimpleSAML\Module\logpeek\Config\Logpeek */ | ||
protected Config\Logpeek $moduleConfig; | ||
|
||
/** @var \SimpleSAML\Session */ | ||
protected Session $session; | ||
|
||
|
@@ -77,20 +81,14 @@ public function requireAdmin(): void | |
} | ||
}; | ||
|
||
Configuration::setPreLoadedConfig( | ||
Configuration::loadFromArray( | ||
[ | ||
'logfile' => $this->tmpfile, | ||
'lines' => 1500, | ||
|
||
// Read block size. 8192 is max, limited by fread. | ||
'blocksz' => 8192, | ||
], | ||
'[ARRAY]', | ||
'simplesaml' | ||
), | ||
'module_logpeek.php', | ||
'simplesaml' | ||
$this->moduleConfig = Config\Logpeek::fromArray( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can easily create a test-config from an array |
||
[ | ||
'logFile' => $this->tmpfile, | ||
'lines' => 1500, | ||
|
||
// Read block size. 8192 is max, limited by fread. | ||
'blockSize' => 8192, | ||
] | ||
); | ||
} | ||
|
||
|
@@ -115,8 +113,9 @@ public function testMain(): void | |
'GET' | ||
); | ||
|
||
$c = new Controller\Logpeek($this->config, $this->session); | ||
$c = new Controller\Logpeek($this->config, $this->session, $this->moduleConfig); | ||
$c->setAuthUtils($this->authUtils); | ||
$c->setModuleConfig($this->moduleConfig); | ||
$response = $c->main($request); | ||
|
||
$this->assertTrue($response->isSuccessful()); | ||
|
@@ -133,8 +132,9 @@ public function testMainWithTag(): void | |
['tag' => $this->session->getTrackID()] | ||
); | ||
|
||
$c = new Controller\Logpeek($this->config, $this->session); | ||
$c = new Controller\Logpeek($this->config, $this->session, $this->moduleConfig); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually 'wrong'; |
||
$c->setAuthUtils($this->authUtils); | ||
$c->setModuleConfig($this->moduleConfig); | ||
$response = $c->main($request); | ||
|
||
$this->assertTrue($response->isSuccessful()); | ||
|
@@ -151,8 +151,9 @@ public function testMainWithInvalidTag(): void | |
['tag' => 'WRONG'] | ||
); | ||
|
||
$c = new Controller\Logpeek($this->config, $this->session); | ||
$c = new Controller\Logpeek($this->config, $this->session, $this->moduleConfig); | ||
$c->setAuthUtils($this->authUtils); | ||
$c->setModuleConfig($this->moduleConfig); | ||
|
||
$this->expectException(Exception::class); | ||
$c->main($request); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add many assertions to test the config values