From 6c5b9fbeedf6d5558f20e8a3d56460e735f342ba Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 3 Jan 2017 18:31:14 +0100 Subject: [PATCH 1/3] Added factory for source location --- src/Model/SourceLocation.php | 14 +++++++++++++ tests/Unit/Model/SourceLocationTest.php | 28 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 tests/Unit/Model/SourceLocationTest.php diff --git a/src/Model/SourceLocation.php b/src/Model/SourceLocation.php index d7a9659..9e0aa76 100644 --- a/src/Model/SourceLocation.php +++ b/src/Model/SourceLocation.php @@ -52,6 +52,20 @@ public function __construct($message, $path, $line, array $context = []) $this->context = $context; } + /** + * Create a source location from your current location. + * + * @param string $message + * @param array $context + * + * @return SourceLocation + */ + public static function createHere($message, array $context = []) + { + $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1); + return new self($message, $trace[0]['file'], $trace[0]['line'], $context); + } + /** * @return string */ diff --git a/tests/Unit/Model/SourceLocationTest.php b/tests/Unit/Model/SourceLocationTest.php new file mode 100644 index 0000000..d37539c --- /dev/null +++ b/tests/Unit/Model/SourceLocationTest.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Extractor\Tests\Unit\Model; + +use Translation\Extractor\Model\SourceLocation; + +class SourceLocationTest extends \PHPUnit_Framework_TestCase +{ + public function testCreateHere() + { + $location = SourceLocation::createHere('foobar', ['foo'=>'bar']); + + $this->assertEquals('/Users/tobias/Workspace/PHPStorm/Translation/extractor/tests/Unit/Model/SourceLocationTest.php', $location->getPath()); + $this->assertEquals(20, $location->getLine()); + + $this->assertEquals('foobar', $location->getMessage()); + $this->assertEquals(['foo'=>'bar'], $location->getContext()); + } +} From ad694884a135fdb0598c9f73116c19fb018c241a Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 3 Jan 2017 18:48:32 +0100 Subject: [PATCH 2/3] bugfix --- src/Model/SourceLocation.php | 2 +- tests/Unit/Model/SourceLocationTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Model/SourceLocation.php b/src/Model/SourceLocation.php index 9e0aa76..0e89811 100644 --- a/src/Model/SourceLocation.php +++ b/src/Model/SourceLocation.php @@ -56,7 +56,7 @@ public function __construct($message, $path, $line, array $context = []) * Create a source location from your current location. * * @param string $message - * @param array $context + * @param array $context * * @return SourceLocation */ diff --git a/tests/Unit/Model/SourceLocationTest.php b/tests/Unit/Model/SourceLocationTest.php index d37539c..9b234bf 100644 --- a/tests/Unit/Model/SourceLocationTest.php +++ b/tests/Unit/Model/SourceLocationTest.php @@ -17,12 +17,12 @@ class SourceLocationTest extends \PHPUnit_Framework_TestCase { public function testCreateHere() { - $location = SourceLocation::createHere('foobar', ['foo'=>'bar']); + $location = SourceLocation::createHere('foobar', ['foo' => 'bar']); - $this->assertEquals('/Users/tobias/Workspace/PHPStorm/Translation/extractor/tests/Unit/Model/SourceLocationTest.php', $location->getPath()); + $this->assertContains('tests/Unit/Model/SourceLocationTest.php', $location->getPath()); $this->assertEquals(20, $location->getLine()); $this->assertEquals('foobar', $location->getMessage()); - $this->assertEquals(['foo'=>'bar'], $location->getContext()); + $this->assertEquals(['foo' => 'bar'], $location->getContext()); } } From e3d06630732eb939f600e5142fd8e9329f9cef98 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 3 Jan 2017 18:49:30 +0100 Subject: [PATCH 3/3] style --- src/Model/SourceLocation.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Model/SourceLocation.php b/src/Model/SourceLocation.php index 0e89811..7217212 100644 --- a/src/Model/SourceLocation.php +++ b/src/Model/SourceLocation.php @@ -63,6 +63,7 @@ public function __construct($message, $path, $line, array $context = []) public static function createHere($message, array $context = []) { $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1); + return new self($message, $trace[0]['file'], $trace[0]['line'], $context); }