Skip to content

Commit

Permalink
refactor: add Jsonable support for MailFolderChildList
Browse files Browse the repository at this point in the history
refs #8
  • Loading branch information
DDEV-Local User committed Jun 12, 2022
1 parent aab47d5 commit 2e9bb79
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
23 changes: 21 additions & 2 deletions src/Mail/Client/Folder/MailFolderChildList.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
*
* @package Conjoon\Mail\Client\Folder
*/
class MailFolderChildList extends AbstractList implements Arrayable
class MailFolderChildList extends AbstractList implements Arrayable, Jsonable
{
// -------------------------
// AbstractList
Expand All @@ -68,7 +68,7 @@ public function getEntityType(): string


// -------------------------
// Jsonable
// Arrayable, Jsonable
// -------------------------

/**
Expand All @@ -83,7 +83,26 @@ public function toArray(): array
$data[] = $mailFolder->toArray();
}

return $data;
}


/**
* @inheritdoc
*/
public function toJson(JsonStrategy $jsonStrategy = null): array
{
$data = [];

foreach ($this->data as $mailFolder) {
$data[] = $jsonStrategy
? $jsonStrategy->toJson($mailFolder)
: $mailFolder->toArray();
}

return $data;
}



}
3 changes: 0 additions & 3 deletions tests/Mail/Client/Data/MailAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ public function testToJson()
->willReturn($account->toArray());

$this->assertEquals($config, $account->toJson($strategyMock));



}


Expand Down
51 changes: 51 additions & 0 deletions tests/Mail/Client/Folder/MailFolderChildListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use Conjoon\Mail\Client\Folder\MailFolderChildList;
use Conjoon\Util\AbstractList;
use Conjoon\Util\Arrayable;
use Conjoon\Util\Jsonable;
use Conjoon\Util\JsonStrategy;
use Tests\TestCase;

/**
Expand All @@ -55,6 +57,7 @@ public function testClass()
$mailFolderChildList = new MailFolderChildList();
$this->assertInstanceOf(AbstractList::class, $mailFolderChildList);
$this->assertInstanceOf(Arrayable::class, $mailFolderChildList);
$this->assertInstanceOf(Jsonable::class, $mailFolderChildList);

$this->assertSame(MailFolder::class, $mailFolderChildList->getEntityType());
}
Expand Down Expand Up @@ -93,4 +96,52 @@ public function testToArray()
"data" => []
]], $mailFolderChildList->toArray());
}


/**
* Tests constructor
*/
public function testToJson()
{

$data = [
"name" => "INBOX",
"unreadMessages" => 5,
"totalMessages" => 10,
"folderType" => MailFolder::TYPE_INBOX
];

$folder = new MailFolder(
new FolderKey("dev", "INBOX"),
$data
);

$mailFolderChildList = new MailFolderChildList();

$mailFolderChildList[] = $folder;

$this->assertEquals(
$mailFolderChildList->toArray(),
$mailFolderChildList->toJson()
);

// w/ strategy



$strategyMock =
$this->getMockBuilder(JsonStrategy::class)
->getMockForAbstractClass();

$strategyMock
->expects($this->exactly(1))
->method("toJson")
->with($folder)
->willReturn($folder->toArray());

$this->assertEquals($folder->toArray(), $folder->toJson($strategyMock));


}

}

0 comments on commit 2e9bb79

Please sign in to comment.