Skip to content

Commit

Permalink
Merge pull request #837 from PROCERGS/preview-1.21.1
Browse files Browse the repository at this point in the history
v1.21.1
  • Loading branch information
guilhermednt authored Jan 21, 2019
2 parents 06ba6cf + 1e1d77c commit a666f45
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 8 deletions.
51 changes: 51 additions & 0 deletions src/LoginCidadao/BadgesControlBundle/Tests/BadgeMocker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* This file is part of the login-cidadao project or it's bundles.
*
* (c) Guilherme Donato <guilhermednt on github>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LoginCidadao\BadgesControlBundle\Tests;

use LoginCidadao\BadgesControlBundle\Model\BadgeInterface;

class BadgeMocker
{
public static function getBadge($namespace, $name): BadgeInterface
{
return new class($namespace, $name) implements BadgeInterface
{
private $namespace;
private $name;

public function __construct(string $namespace, string $name)
{
$this->namespace = $namespace;
$this->name = $name;
}

public function getNamespace()
{
return $this->namespace;
}

public function getName()
{
return $this->name;
}

public function getData()
{
return true;
}

public function __toString(): string
{
return "{$this->getNamespace()}.{$this->getName()}";
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace LoginCidadao\BadgesControlBundle\Tests\Event;

use LoginCidadao\BadgesControlBundle\Event\EvaluateBadgesEvent;
use LoginCidadao\BadgesControlBundle\Model\BadgeInterface;
use LoginCidadao\BadgesControlBundle\Tests\BadgeMocker;
use LoginCidadao\CoreBundle\Entity\Person;
use PHPUnit\Framework\TestCase;

Expand All @@ -22,15 +22,14 @@ public function testEvent()
$person = new Person();
$this->assertEmpty($person->getBadges());

$badge1 = $this->createMock(BadgeInterface::class);
$badge1->expects($this->any())->method('__toString')->willReturn('badge1');
$badge2 = $this->createMock(BadgeInterface::class);
$badge2->expects($this->any())->method('__toString')->willReturn('badge2');
$badge1 = BadgeMocker::getBadge('namespace1', 'badge1');
$badge2 = BadgeMocker::getBadge('namespace1', 'badge2');
$badge3 = BadgeMocker::getBadge('namespace2', 'badge1');

$event = new EvaluateBadgesEvent($person);
$event->registerBadges([$badge1, $badge2]);
$event->registerBadges([$badge1, $badge2, $badge3]);

$this->assertSame($person, $event->getPerson());
$this->assertCount(2, $person->getBadges());
$this->assertCount(3, $person->getBadges());
}
}
9 changes: 8 additions & 1 deletion src/LoginCidadao/CoreBundle/Entity/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -1053,8 +1053,15 @@ public function getIdCards()

public function getBadges()
{
return /** @scrutinizer ignore-deprecated */
$badges = /** @scrutinizer ignore-deprecated */
$this->badges;
$badgesMap = [];
foreach ($badges as $badge) {
$key = "{$badge->getNamespace()}.{$badge->getName()}";
$badgesMap[$key] = $badge;
}

return $badgesMap;
}

public function mergeBadges(array $badges)
Expand Down
47 changes: 47 additions & 0 deletions src/LoginCidadao/CoreBundle/Tests/Entity/PersonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* This file is part of the login-cidadao project or it's bundles.
*
* (c) Guilherme Donato <guilhermednt on github>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LoginCidadao\CoreBundle\Tests\Entity;

use LoginCidadao\BadgesControlBundle\Tests\BadgeMocker;
use LoginCidadao\CoreBundle\Entity\Person;
use PHPUnit\Framework\TestCase;

class PersonTest extends TestCase
{
public function testBadges()
{
$badge1 = BadgeMocker::getBadge('namespace1', 'badge1');
$badge2 = BadgeMocker::getBadge('namespace1', 'badge2');
$badge3 = BadgeMocker::getBadge('namespace2', 'badge1');

$person = new Person();
$person->mergeBadges([$badge1]);
$person->mergeBadges([$badge2, $badge3]);

$badges = $person->getBadges();

$this->assertCount(3, $badges);
$this->assertContains('namespace1.badge1', array_keys($badges));
$this->assertContains('namespace1.badge2', array_keys($badges));
$this->assertContains('namespace2.badge1', array_keys($badges));

$this->assertSame($badge1, $badges['namespace1.badge1']);
$this->assertSame($badge2, $badges['namespace1.badge2']);
$this->assertSame($badge3, $badges['namespace2.badge1']);
}

public function testEmptyBadges()
{
$person = new Person();

$this->assertEmpty($person->getBadges());
}
}

0 comments on commit a666f45

Please sign in to comment.