Skip to content

Commit 4296c35

Browse files
authored
Merge pull request #26 from cawolf/fix-context-warnings-empty
Context of warnings is empty in JSON result
2 parents 1619be5 + f30fc20 commit 4296c35

4 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* The MIT License (MIT)
4+
*
5+
* Copyright (c) 2016 Christian A. Wolf
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace PhpDA\Entity;
27+
28+
use Symfony\Component\Finder\SplFileInfo;
29+
30+
class JsonSerializableFileInfo implements \JsonSerializable
31+
{
32+
/** @var SplFileInfo */
33+
private $file;
34+
35+
/**
36+
* @param SplFileInfo $file
37+
*/
38+
public function __construct(SplFileInfo $file)
39+
{
40+
$this->file = $file;
41+
}
42+
43+
/**
44+
* @return SplFileInfo
45+
*/
46+
public function getFile()
47+
{
48+
return $this->file;
49+
}
50+
51+
public function jsonSerialize()
52+
{
53+
return $this->__toString();
54+
}
55+
56+
/**
57+
* @return string
58+
*/
59+
public function __toString()
60+
{
61+
return $this->file->__toString();
62+
}
63+
}

src/Parser/Logger.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
namespace PhpDA\Parser;
2727

28+
use PhpDA\Entity\JsonSerializableFileInfo;
2829
use Psr\Log\AbstractLogger;
2930
use Psr\Log\LogLevel;
31+
use Symfony\Component\Finder\SplFileInfo;
3032

3133
class Logger extends AbstractLogger
3234
{
@@ -91,9 +93,23 @@ public function log($level, $message, array $context = array())
9193
$this->entries[$level] = array();
9294
}
9395

96+
array_walk_recursive($context, array($this, 'wrapSPLFileInfo'));
97+
9498
$this->entries[$level][] = array(
9599
'message' => $message,
96100
'context' => $context,
97101
);
98102
}
103+
104+
/**
105+
* items of type SplFileInfo will be wrapped with JsonSerializableFileInfo to be serializable
106+
*
107+
* @param mixed $item
108+
*/
109+
private function wrapSPLFileInfo(&$item)
110+
{
111+
if ($item instanceof SplFileInfo) {
112+
$item = new JsonSerializableFileInfo($item);
113+
}
114+
}
99115
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* The MIT License (MIT)
4+
*
5+
* Copyright (c) 2016 Christian A. Wolf
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
namespace PhpDATest\Entity;
27+
28+
use PhpDA\Entity\JsonSerializableFileInfo;
29+
30+
class JsonSerializableFileInfoTest extends \PHPUnit_Framework_TestCase
31+
{
32+
/** @var JsonSerializableFileInfo */
33+
protected $fixture;
34+
35+
/** @var \Symfony\Component\Finder\SplFileInfo | \Mockery\MockInterface */
36+
protected $file;
37+
38+
protected function setUp()
39+
{
40+
$this->file = \Mockery::mock('Symfony\Component\Finder\SplFileInfo');
41+
$this->file->shouldReceive('__toString')->andReturn('filename');
42+
$this->file->shouldReceive('getPathname')->andReturn('pathname');
43+
$this->file->shouldReceive('getRelativePath')->andReturn('relative/path');
44+
$this->fixture = new JsonSerializableFileInfo($this->file);
45+
}
46+
47+
public function testIsWrapper()
48+
{
49+
$this->assertSame('pathname', $this->fixture->getFile()->getPathname());
50+
$this->assertSame('relative/path', $this->fixture->getFile()->getRelativePath());
51+
}
52+
53+
public function testIsSerializable()
54+
{
55+
$this->assertInstanceOf('\JsonSerializable', $this->fixture);
56+
$this->assertSame('filename', $this->fixture->jsonSerialize());
57+
$this->assertSame('filename', $this->fixture->__toString());
58+
}
59+
}

tests/unit/PhpDATest/Parser/LoggerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
3333
/** @var Logger */
3434
protected $fixture;
3535

36+
/** @var \Symfony\Component\Finder\SplFileInfo | \Mockery\MockInterface */
37+
protected $file;
38+
3639
protected function setUp()
3740
{
41+
$this->file = \Mockery::mock('Symfony\Component\Finder\SplFileInfo');
3842
$this->fixture = new Logger;
3943
}
4044

@@ -50,4 +54,18 @@ public function testLogging()
5054
$this->assertNotEmpty($this->fixture->toString());
5155
$this->assertFalse($this->fixture->isEmpty());
5256
}
57+
58+
public function testLoggingWithWrapping()
59+
{
60+
$this->file->shouldReceive('__toString')->andReturn('filename');
61+
62+
$this->assertSame('', $this->fixture->toString());
63+
$this->assertTrue($this->fixture->isEmpty());
64+
65+
$this->fixture->log(LogLevel::CRITICAL, 'CRITICALfoo', array($this->file));
66+
$this->fixture->log(LogLevel::NOTICE, 'NOTICEfoo', array('NOTICEbar' => $this->file));
67+
68+
$this->assertNotEmpty($this->fixture->toString());
69+
$this->assertFalse($this->fixture->isEmpty());
70+
}
5371
}

0 commit comments

Comments
 (0)