Skip to content

Commit

Permalink
Merge pull request #214 from cweagans/patch-collection-serialization
Browse files Browse the repository at this point in the history
Make sure that Patch and PatchCollection objects can be correctly serialized/deserialized
  • Loading branch information
cweagans authored Jun 3, 2018
2 parents b0dc9cd + 28d5ef1 commit d3b9741
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
42 changes: 41 additions & 1 deletion src/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace cweagans\Composer;

class Patch
class Patch implements \JsonSerializable
{
/**
* The package that the patch belongs to.
Expand Down Expand Up @@ -38,4 +38,44 @@ class Patch
* @var int $depth
*/
public $depth;

/**
* Create a Patch from a serialized representation.
*
* @param $json
* A json_encode'd representation of a Patch.
*
* @return Patch
* A Patch with all of the serialized properties set.
*/
public static function fromJson($json)
{
if (!is_object($json)) {
$json = json_decode($json);
}
$properties = ['package', 'description', 'url', 'sha1', 'depth'];
$patch = new static();

foreach ($properties as $property) {
if (isset($json->{$property})) {
$patch->{$property} = $json->{$property};
}
}

return $patch;
}

/**
* {@inheritDoc}
*/
public function jsonSerialize()
{
return [
'package' => $this->package,
'description' => $this->description,
'url' => $this->url,
'sha1' => $this->sha1,
'depth' => $this->depth,
];
}
}
38 changes: 37 additions & 1 deletion src/PatchCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace cweagans\Composer;

class PatchCollection
class PatchCollection implements \JsonSerializable
{
/**
* A deep list of patches to apply.
Expand Down Expand Up @@ -41,4 +41,40 @@ public function getPatchesForPackage($package)

return [];
}

/**
* Create a PatchCollection from a serialized representation.
*
* @param $json
* A json_encode'd representation of a PatchCollection.
*
* @return PatchCollection
* A PatchCollection with all of the serialized patches included.
*/
public static function fromJson($json)
{
if (!is_object($json)) {
$json = json_decode($json);
}
$collection = new static();

foreach ($json->patches as $package => $patches) {
foreach ($patches as $patch_json) {
$patch = Patch::fromJson($patch_json);
$collection->addPatch($patch);
}
}

return $collection;
}

/**
* {@inheritDoc}
*/
public function jsonSerialize()
{
return [
'patches' => $this->patches,
];
}
}
31 changes: 31 additions & 0 deletions tests/unit/PatchCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,35 @@ public function testPatchCollectionAddRetrieve()
}
}
}

public function testSerializeDeserialize()
{
$collection = new PatchCollection();

$patch1 = new Patch();
$patch1->package = 'some/package';
$patch1->description = 'patch1';

$patch2 = new Patch();
$patch2->package = 'another/package';
$patch2->description = 'patch2';

$collection->addPatch($patch1);
$collection->addPatch($patch2);

$json = json_encode($collection);

$new_collection = PatchCollection::fromJson($json);

$this->assertEquals($collection, $new_collection);

foreach ([ 'some/package', 'another/package'] as $package_name) {
// We should get 1 patch for each package.
$this->assertCount(1, $new_collection->getPatchesForPackage($package_name));

foreach ($new_collection->getPatchesForPackage($package_name) as $patch) {
$this->assertEquals($package_name, $patch->package);
}
}
}
}
26 changes: 26 additions & 0 deletions tests/unit/PatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace cweagans\Composer\Tests;

use Codeception\Test\Unit;
use cweagans\Composer\Patch;

class PatchTest extends Unit
{

public function testSerializeDeserialize()
{
$patch = new Patch();
$patch->package = 'drupal/drupal';
$patch->url = 'https://google.com';
$patch->description = "Test description";
$patch->depth = 0;
$patch->sha1 = sha1('asdf');

$json = json_encode($patch);

$new_patch = Patch::fromJson($json);

$this->assertEquals($patch, $new_patch);
}
}

0 comments on commit d3b9741

Please sign in to comment.