Skip to content

Commit

Permalink
Protect against dynamic properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyker committed Mar 29, 2023
1 parent f90484e commit 9b2b1b9
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 42 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
composer.phar
composer.lock
.DS_Store
example.php
.idea/
.idea/
/.phpunit.result.cache
/.phpunit.cache/
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
20 changes: 11 additions & 9 deletions src/Resources/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
}
4 changes: 2 additions & 2 deletions src/Resources/Mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
6 changes: 3 additions & 3 deletions src/Resources/Modpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
14 changes: 4 additions & 10 deletions src/SolderClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testGetModpacks()

$modpacks = $client->getModpacks();

$this->assertEquals(2, count($modpacks));
$this->assertCount(2, $modpacks);
$this->assertArrayHasKey('hexxit', $modpacks);
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
51 changes: 51 additions & 0 deletions tests/DynamicPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace TechnicPack\SolderClient\Tests;

use PHPUnit\Framework\TestCase;
use TechnicPack\SolderClient\Resources\Build;
use TechnicPack\SolderClient\Resources\Mod;
use TechnicPack\SolderClient\Resources\Modpack;

class DynamicPropertiesTest extends TestCase
{
public function testBuild()
{
$props = [
'id' => 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'));
}
}

0 comments on commit 9b2b1b9

Please sign in to comment.