diff --git a/.gitignore b/.gitignore index 51e7368..9a37d2a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ composer.phar composer.lock .DS_Store -example.php -.idea/ \ No newline at end of file +.idea/ +/.phpunit.result.cache +/.phpunit.cache/ \ No newline at end of file diff --git a/composer.json b/composer.json index ec7153b..790556c 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "ext-json": "*" }, "require-dev": { - "phpunit/phpunit": "^6.0 || ^7.0 || ^8.0 || ^9.0" + "phpunit/phpunit": "^9.6" }, "autoload": { "psr-4": { diff --git a/src/Resources/Build.php b/src/Resources/Build.php index 1ed928d..a887152 100644 --- a/src/Resources/Build.php +++ b/src/Resources/Build.php @@ -13,18 +13,20 @@ class Build public function __construct($properties) { - foreach ($properties as $key => $val) { - if ($key != "mods") { - $this->{$key} = $val; + foreach (get_object_vars($this) as $key => $val) { + if ($key !== "mods") { + $this->{$key} = $properties[$key] ?? null; } } - foreach ($properties['mods'] as $mod) { - array_push($this->mods, new Mod($mod)); - } + if (isset($properties['mods'])) { + foreach ($properties['mods'] as $mod) { + $this->mods[] = new Mod($mod); + } - usort($this->mods, function ($a, $b) { - return strcasecmp($a->pretty_name, $b->pretty_name); - }); + usort($this->mods, function ($a, $b) { + return strcasecmp($a->pretty_name, $b->pretty_name); + }); + } } } diff --git a/src/Resources/Mod.php b/src/Resources/Mod.php index 9591cbe..296cac4 100644 --- a/src/Resources/Mod.php +++ b/src/Resources/Mod.php @@ -17,8 +17,8 @@ class Mod public function __construct($properties) { - foreach ($properties as $key => $val) { - $this->{$key} = $val; + foreach (get_object_vars($this) as $key => $val) { + $this->{$key} = $properties[$key] ?? null; } } } \ No newline at end of file diff --git a/src/Resources/Modpack.php b/src/Resources/Modpack.php index 12f7828..e16ff69 100644 --- a/src/Resources/Modpack.php +++ b/src/Resources/Modpack.php @@ -13,12 +13,12 @@ class Modpack public $background; public $recommended; public $latest; - public $builds = []; + public $builds; public function __construct($properties) { - foreach ($properties as $key => $val) { - $this->{$key} = $val; + foreach (get_object_vars($this) as $key => $val) { + $this->{$key} = $properties[$key] ?? null; } } } \ No newline at end of file diff --git a/src/SolderClient.php b/src/SolderClient.php index aad69aa..4e8433c 100644 --- a/src/SolderClient.php +++ b/src/SolderClient.php @@ -39,20 +39,14 @@ public static function factory($url, $key, $headers = [], $handler = null, $time throw new UnauthorizedException('Key failed to validate.', 403); } - $properties = array( - "url" => $url, - "key" => $key, - ); - - return new SolderClient($client, $properties); + return new SolderClient($client, $url, $key); } - protected function __construct($client, $properties) + protected function __construct($client, $url, $key) { $this->client = $client; - foreach ($properties as $key => $val) { - $this->{$key} = $val; - } + $this->url = $url; + $this->key = $key; } private function handle($uri) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index f9c0496..56dbea3 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -102,7 +102,7 @@ public function testGetModpacks() $modpacks = $client->getModpacks(); - $this->assertEquals(2, count($modpacks)); + $this->assertCount(2, $modpacks); $this->assertArrayHasKey('hexxit', $modpacks); } @@ -188,15 +188,15 @@ public function testGetModpack() $modpack = $client->getModpack('hexxit'); - $this->assertObjectHasAttribute('name', $modpack); - $this->assertObjectHasAttribute('display_name', $modpack); - $this->assertObjectHasAttribute('url', $modpack); - $this->assertObjectHasAttribute('icon', $modpack); - $this->assertObjectHasAttribute('logo', $modpack); - $this->assertObjectHasAttribute('background', $modpack); - $this->assertObjectHasAttribute('recommended', $modpack); - $this->assertObjectHasAttribute('latest', $modpack); - $this->assertObjectHasAttribute('builds', $modpack); + $this->assertTrue(property_exists($modpack, 'name')); + $this->assertTrue(property_exists($modpack, 'display_name')); + $this->assertTrue(property_exists($modpack, 'url')); + $this->assertTrue(property_exists($modpack, 'icon')); + $this->assertTrue(property_exists($modpack, 'logo')); + $this->assertTrue(property_exists($modpack, 'background')); + $this->assertTrue(property_exists($modpack, 'recommended')); + $this->assertTrue(property_exists($modpack, 'latest')); + $this->assertTrue(property_exists($modpack, 'builds')); } public function testGetBuildDoesNotExist() @@ -251,11 +251,11 @@ public function testGetBuild() $build = $client->getBuild('hexxit', '1.0.1'); - $this->assertObjectHasAttribute('minecraft', $build); - $this->assertObjectHasAttribute('forge', $build); - $this->assertObjectHasAttribute('java', $build); - $this->assertObjectHasAttribute('memory', $build); - $this->assertObjectHasAttribute('mods', $build); + $this->assertTrue(property_exists($build, 'minecraft')); + $this->assertTrue(property_exists($build, 'forge')); + $this->assertTrue(property_exists($build, 'java')); + $this->assertTrue(property_exists($build, 'memory')); + $this->assertTrue(property_exists($build, 'mods')); } public function testBadPack() diff --git a/tests/DynamicPropertiesTest.php b/tests/DynamicPropertiesTest.php new file mode 100644 index 0000000..861f76c --- /dev/null +++ b/tests/DynamicPropertiesTest.php @@ -0,0 +1,51 @@ + 1, + + 'extra' => 'stuff', + ]; + + $build = new Build($props); + + $this->assertTrue(property_exists($build, 'id')); + $this->assertFalse(property_exists($build, 'extra')); + } + + public function testMod() + { + $props = [ + 'id' => 1, + 'extra' => 'stuff', + ]; + + $mod = new Mod($props); + + $this->assertTrue(property_exists($mod, 'id')); + $this->assertFalse(property_exists($mod, 'extra')); + } + + public function testModpack() + { + $props = [ + 'id' => 1, + 'extra' => 'stuff', + ]; + + $modpack = new Modpack($props); + + $this->assertTrue(property_exists($modpack, 'id')); + $this->assertFalse(property_exists($modpack, 'extra')); + } +}