Frederic G. MARAND 2 éve
szülő
commit
44f9853676

+ 41 - 0
ComposerCheck/LoaderBase.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace Fgm\ComposerCheck;
+
+/**
+ * Class LockLoader loads a composer.lock file.
+ */
+abstract class LoaderBase implements LoaderInterface {
+
+  protected $contents;
+
+  protected $file;
+
+  protected $platform = [];
+
+  protected $dev = [];
+
+  protected $run = [];
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getDev() {
+    return $this->dev;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getRun() {
+    return $this->run;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function load(string $file = NULL) {
+    $this->contents = json_decode(file_get_contents($this->file), TRUE);
+  }
+
+}

+ 4 - 0
ComposerCheck/LoaderInterface.php

@@ -36,4 +36,8 @@ interface LoaderInterface {
    */
   public function load();
 
+  /**
+   * Parse the loaded data.
+   */
+  public function parse();
 }

+ 5 - 29
ComposerCheck/LockLoader.php

@@ -5,16 +5,10 @@ namespace Fgm\ComposerCheck;
 /**
  * Class LockLoader loads a composer.lock file.
  */
-class LockLoader implements LoaderInterface {
-
-  protected $file;
+class LockLoader extends LoaderBase implements LoaderInterface {
 
   protected $platform = [];
 
-  protected $dev = [];
-
-  protected $run = [];
-
   /**
    * {@inheritdoc}
    */
@@ -22,29 +16,11 @@ class LockLoader implements LoaderInterface {
     $this->file = realpath("${directory}/composer.lock");
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function getDev() {
-    return $this->dev;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function getRun() {
-    return $this->run;
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public function load() {
-    $lockFile = json_decode(file_get_contents($this->file), TRUE);
-    $lockPackages = $lockFile['packages'];
-    $lockDevPackages = $lockFile['packages-dev'];
+  public function parse() {
+    $lockPackages = $this->contents['packages'];
+    $lockDevPackages = $this->contents['packages-dev'];
 
-    $lockPlatform = $lockFile['platform'];
+    $lockPlatform = $this->contents['platform'];
     array_walk($lockPlatform, function (&$requirement, $component) {
       $requirement = [
         'name' => $component,

+ 4 - 29
ComposerCheck/RequirementsLoader.php

@@ -5,11 +5,7 @@ namespace Fgm\ComposerCheck;
 /**
  * Class RequirementsLoader loads a composer.json file.
  */
-class RequirementsLoader implements LoaderInterface {
-
-  protected $dev = [];
-
-  protected $run = [];
+class RequirementsLoader extends LoaderBase implements LoaderInterface {
 
   /**
    * {@inheritdoc}
@@ -18,33 +14,12 @@ class RequirementsLoader implements LoaderInterface {
     $this->file = realpath("${directory}/composer.json");
   }
 
-  /**
-   * Return the --dev requirements.
-   *
-   * @return array
-   *   A requirements array, keyed by requirement string.
-   */
-  public function getDev() {
-    return $this->dev;
-  }
-
-  /**
-   * Return the non- --dev requirements.
-   *
-   * @return array
-   *   A requirements array, keyed by requirement string.
-   */
-  public function getRun() {
-    return $this->run;
-  }
-
   /**
    * {@inheritdoc}
    */
-  public function load() {
-    $json = json_decode(file_get_contents($this->file), TRUE);
-    $this->run = $json['require'] ?? [];
-    $this->dev = $json['require-dev'] ?? [];
+  public function parse() {
+    $this->run = $this->contents['require'] ?? [];
+    $this->dev = $this->contents['require-dev'] ?? [];
   }
 
 }

+ 59 - 0
ComposerCheck/Tests/LockLoaderTest.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace Fgm\ComposerCheck\Tests;
+
+use Fgm\ComposerCheck\LockLoader;
+
+/**
+ * Class TestLoader allows faking the composer.lock file contents.
+ */
+class TestLoader extends LockLoader {
+  public function  __construct(string $directory) {
+  }
+
+  public function setData($data) {
+    $this->contents['packages'] = $data['packages'];
+    $this->contents['packages-dev'] = $data['packages-dev'];
+    $this->contents['platform'] = $data['platform'];
+  }
+}
+
+/**
+ * Class LockLoaderTest
+ *
+ * @coversDefaultClass Fgm\ComposerCheck\LockLoader
+ */
+class LockLoaderTest extends \PHPUnit_Framework_TestCase {
+
+  /**
+   * @return array $lock
+   *   An array of simplified versions of lock data.
+   */
+  public function lockParsingProvider() {
+    return [
+      [[
+        'packages' => [
+          'r1' => 'R1',
+        ],
+        'packages-dev' => [
+          'd1' => 'D1',
+        ],
+        'platform' => [
+          'p1' => 'P1',
+        ],
+      ]],
+    ];
+  }
+
+  /**
+   * @dataProvider lockParsingProvider
+   */
+  public function testLockParsing($lockData) {
+    $loader = new TestLoader("");
+    $loader->setData($lockData);
+    $loader->parse();
+    $actualRun = $loader->getRun();
+    $actualDev = $loader->getDev();
+    
+  }
+}