From 1203519a28d8eca53ccc2b1f00045f976742baad Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Thu, 4 Sep 2014 00:24:09 +1000 Subject: [PATCH 1/3] testFindingVendorFolder --- src/Concise/Version.php | 17 ++++++++++++++++- tests/Concise/VersionTest.php | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Concise/Version.php b/src/Concise/Version.php index 44811774..73a5d552 100644 --- a/src/Concise/Version.php +++ b/src/Concise/Version.php @@ -4,9 +4,24 @@ class Version { + public function findVendorFolder() + { + $path = __DIR__; + for ($i = 0; $i < 10; ++$i) { + $dir = scandir($path); + if (in_array('vendor', $dir)) { + return "$path/vendor"; + } + $path .= "/.."; + } + + return null; + } + public function getVersionForPackage($packageName) { - $packages = json_decode(file_get_contents(__DIR__ . '/../../vendor/composer/installed.json')); + $vendor = $this->findVendorFolder(__DIR__); + $packages = json_decode(file_get_contents("$vendor/composer/installed.json")); foreach ($packages as $package) { if ($package->name === $packageName) { return $package->version; diff --git a/tests/Concise/VersionTest.php b/tests/Concise/VersionTest.php index 982a25a6..b2f4ef9c 100644 --- a/tests/Concise/VersionTest.php +++ b/tests/Concise/VersionTest.php @@ -24,4 +24,9 @@ public function testWeCanEasilyGetTheConciseVersion() { $this->assert($this->version->getConciseVersion(), equals, $this->version->getVersionForPackage('elliotchance/concise')); } + + public function testFindingVendorFolder() + { + $this->assert($this->version->findVendorFolder(), ends_with, '/vendor'); + } } From 9d26563df7c9c4766f78a2c365adaf370876a322 Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Thu, 4 Sep 2014 00:27:54 +1000 Subject: [PATCH 2/3] testReturnEmptyStringIfVendorFolderCannotBeFound --- src/Concise/Version.php | 4 ++++ tests/Concise/VersionTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Concise/Version.php b/src/Concise/Version.php index 73a5d552..2652d56b 100644 --- a/src/Concise/Version.php +++ b/src/Concise/Version.php @@ -21,6 +21,10 @@ public function findVendorFolder() public function getVersionForPackage($packageName) { $vendor = $this->findVendorFolder(__DIR__); + if (!$vendor) { + return ''; + } + $packages = json_decode(file_get_contents("$vendor/composer/installed.json")); foreach ($packages as $package) { if ($package->name === $packageName) { diff --git a/tests/Concise/VersionTest.php b/tests/Concise/VersionTest.php index b2f4ef9c..3fd6efae 100644 --- a/tests/Concise/VersionTest.php +++ b/tests/Concise/VersionTest.php @@ -29,4 +29,12 @@ public function testFindingVendorFolder() { $this->assert($this->version->findVendorFolder(), ends_with, '/vendor'); } + + public function testReturnEmptyStringIfVendorFolderCannotBeFound() + { + $version = $this->niceMock('Concise\Version') + ->stub('findVendorFolder') + ->done(); + $this->assert($version->getConciseVersion(), is_blank); + } } From fc26bf4249c0323848eb833a5e47d1f93c751cd9 Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Thu, 4 Sep 2014 09:30:25 +1000 Subject: [PATCH 3/3] Dead code --- src/Concise/Version.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Concise/Version.php b/src/Concise/Version.php index 2652d56b..e25a2ee5 100644 --- a/src/Concise/Version.php +++ b/src/Concise/Version.php @@ -14,8 +14,6 @@ public function findVendorFolder() } $path .= "/.."; } - - return null; } public function getVersionForPackage($packageName)