Skip to content

Commit

Permalink
Add possibility to choose file types that would be collected
Browse files Browse the repository at this point in the history
  • Loading branch information
1josefnevoral committed Apr 6, 2015
1 parent 494b9e7 commit 02995d1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ errorCollector:
region: 'ap-southeast-1'
bucket: hq-error-log
logDirectory: %appDir%/../log/
collectFileTypes:
- *.html
- *.log
errorStorage: '\HQ\ErrorCollector\Storage\S3Storage'
```

Expand Down
7 changes: 6 additions & 1 deletion src/ErrorCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ class ErrorCollector extends Object {
/** @var string */
private $logDirectory;

/** @var array */
private $collectFileTypes;

public function __construct(
$logDirectory,
array $collectFileTypes,
IErrorStorage $errorStorage = null
) {
$this->logDirectory = $logDirectory;
$this->collectFileTypes = $collectFileTypes;
$this->errorStorage = $errorStorage;
}

Expand All @@ -46,7 +51,7 @@ public function uploadFiles()
throw new \Nette\InvalidStateException('No error storage set.');
}

$files = $this->findFiles(array('*.html', '*.log'), $this->logDirectory);
$files = $this->findFiles($this->collectFileTypes, $this->logDirectory);
$cnt = 0;
/** @var \SplFileInfo $file */
foreach ($files as $file) {
Expand Down
5 changes: 5 additions & 0 deletions src/ErrorCollectorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class ErrorCollectorExtension extends Nette\DI\CompilerExtension {

private $defaults = array(
'logDirectory' => '%appDir%/../log/',
'collectFileTypes' => array(
'*.html',
'*.log'
),
'errorStorage' => '\HQ\ErrorCollector\Storage\S3Storage',
's3' => array(
'bucket' => 'hq-error-log'
Expand Down Expand Up @@ -44,6 +48,7 @@ public function loadConfiguration()
$builder->addDefinition($this->prefix('errorCollector'))
->setClass('HQ\ErrorCollector\ErrorCollector', array(
'logDirectory' => $config['logDirectory'],
'collectFileTypes' => $config['collectFileTypes'],
'errorStorage' => '@errorCollector.storage'
));
}
Expand Down
31 changes: 30 additions & 1 deletion tests/ErrorCollector/ErrorCollectorTestCase.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Tests;

use HQ\ErrorCollector\Storage\IErrorStorage;
use Nette,
Tester,
Tester\Assert;
Expand All @@ -26,11 +27,16 @@ class ErrorCollectorTestCase extends Tester\TestCase {
/** @var string */
private $directory;

/** @var array */
private $collectFileTypes;

public function setUp()
{
$this->mockista = new \Mockista\Registry();

$this->directory = TEMP_DIR;
$this->collectFileTypes = array('*.html', '*.log');

$this->errorStorage = $this->mockista->create('HQ\ErrorCollector\Storage\IErrorStorage', array(
'save' => function($fileName, $filePath, $type) {
@mkdir($this->directory . '/moved/');
Expand All @@ -39,7 +45,7 @@ class ErrorCollectorTestCase extends Tester\TestCase {
}
));

$this->errorCollector = new \HQ\ErrorCollector\ErrorCollector($this->directory, $this->errorStorage);
$this->errorCollector = $this->createErrorCollector($this->directory, $this->collectFileTypes, $this->errorStorage);
}

public function tearDown()
Expand Down Expand Up @@ -88,6 +94,24 @@ class ErrorCollectorTestCase extends Tester\TestCase {
Assert::same(0, $this->errorCollector->uploadFiles());
}

public function testCollectCorrectFileTypes()
{
$files = array(
'temp-file.log',
'temp-log-file.log',
'temp-exception-file.html',
'unknown-file.txt'
);
$this->prepareFiles($files);

$errorCollector = $this->createErrorCollector($this->directory, array('*.html'), $this->errorStorage);

// it should upload only 1 file => html exception
Assert::same(1, $errorCollector->uploadFiles());

// nothing should be uploaded in second run
Assert::same(0, $errorCollector->uploadFiles());
}

public function testGetFileType()
{
Expand Down Expand Up @@ -118,6 +142,11 @@ class ErrorCollectorTestCase extends Tester\TestCase {
sort($arr);
return $arr;
}

private function createErrorCollector($directory, array $collectFileTypes, IErrorStorage $errorStorage)
{
return new \HQ\ErrorCollector\ErrorCollector($directory, $collectFileTypes, $errorStorage);
}
}

\run(new ErrorCollectorTestCase());

0 comments on commit 02995d1

Please sign in to comment.