Skip to content

Commit

Permalink
Fix properties not being set
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyker committed Oct 1, 2024
1 parent f79d471 commit bd19156
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/Resources/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

class Build
{
public int $id;
public string $minecraft;
public ?string $java;
public ?int $memory;
public ?string $forge;
public int $id = -1;
public string $minecraft = "";
public ?string $java = null;
public ?int $memory = -1;
public ?string $forge = null;
/**
* @var Mod[] $mods
*/
Expand All @@ -17,8 +17,8 @@ class Build
public function __construct($properties)
{
foreach (get_object_vars($this) as $key => $val) {
if ($key !== "mods") {
$this->{$key} = $properties[$key] ?? null;
if ($key !== "mods" && array_key_exists($key, $properties)) {
$this->{$key} = $properties[$key];
}
}

Expand Down
24 changes: 13 additions & 11 deletions src/Resources/Mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

class Mod
{
public int $id;
public string $name;
public string $version;
public string $md5;
public ?int $filesize;
public string $url;
public string $pretty_name;
public ?string $author;
public ?string $description;
public ?string $link;
public int $id = -1;
public string $name = "";
public string $version = "";
public string $md5 = "";
public ?int $filesize = -1;
public string $url = "";
public string $pretty_name = "";
public ?string $author = null;
public ?string $description = null;
public ?string $link = null;

public function __construct($properties)
{
foreach (get_object_vars($this) as $key => $val) {
$this->{$key} = $properties[$key] ?? null;
if (array_key_exists($key, $properties)) {
$this->{$key} = $properties[$key];
}
}
}
}
12 changes: 6 additions & 6 deletions src/Resources/Modpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

class Modpack
{
public int $id;
public string $name;
public string $display_name;
public ?string $url;
public ?string $recommended;
public ?string $latest;
public int $id = -1;
public string $name = "";
public string $display_name = "";
public ?string $url = null;
public ?string $recommended = null;
public ?string $latest = null;
/**
* @var Build[] $builds
*/
Expand Down
21 changes: 20 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,14 @@ public function testGetModpack()
$this->assertObjectHasProperty('recommended', $modpack);
$this->assertObjectHasProperty('latest', $modpack);
$this->assertObjectHasProperty('builds', $modpack);
$this->assertObjectHasProperty('builds', $modpack);
$this->assertIsArray($modpack->builds);

$this->assertEquals('hexxit', $modpack->name);
$this->assertEquals('Hexxit', $modpack->display_name);
$this->assertEquals(null, $modpack->url);
$this->assertEquals('1.0.10', $modpack->recommended);
$this->assertEquals('1.0.10', $modpack->latest);
$this->assertEquals(["1.0.0","1.0.1","1.0.3","1.0.4","1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.0.10","2.0.0","2.0.1","2.0.1b","2.0.1c"], $modpack->builds);
}

public function testGetBuildDoesNotExist()
Expand Down Expand Up @@ -256,6 +262,19 @@ public function testGetBuild()
$this->assertObjectHasProperty('memory', $build);
$this->assertObjectHasProperty('mods', $build);
$this->assertIsArray($build->mods);

$this->assertEquals(null, $build->forge);
$this->assertEquals('1.5.2', $build->minecraft);
$this->assertEquals(null, $build->java);
$this->assertEquals(null, $build->memory);

$this->assertCount(1, $build->mods);

$mod = $build->mods[0];
$this->assertEquals('armorbar', $mod->name);
$this->assertEquals('v0.7.1', $mod->version);
$this->assertEquals('f323a8d582302ea0abd615a223f8a68b', $mod->md5);
$this->assertEquals('https://mirror.technicpack.net/Technic/mods/armorbar/armorbar-v0.7.1.zip', $mod->url);
}

public function testBadPack()
Expand Down
7 changes: 6 additions & 1 deletion tests/DynamicPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ public function testBuild()
{
$props = [
'id' => 1,

'extra' => 'stuff',
];

$build = new Build($props);

$this->assertTrue(property_exists($build, 'id'));
$this->assertFalse(property_exists($build, 'extra'));

$this->assertEquals(1, $build->id);
}

public function testMod()
Expand All @@ -34,6 +35,8 @@ public function testMod()

$this->assertTrue(property_exists($mod, 'id'));
$this->assertFalse(property_exists($mod, 'extra'));

$this->assertEquals(1, $mod->id);
}

public function testModpack()
Expand All @@ -47,5 +50,7 @@ public function testModpack()

$this->assertTrue(property_exists($modpack, 'id'));
$this->assertFalse(property_exists($modpack, 'extra'));

$this->assertEquals(1, $modpack->id);
}
}

0 comments on commit bd19156

Please sign in to comment.