mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-10-31 17:29:04 +00:00 
			
		
		
		
	Implemented SourceMembershipInformation and tests
This commit is contained in:
		| @@ -0,0 +1,46 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Domain\SourceManagement; | ||||
|  | ||||
| use Doctrine\Common\Collections\Collection; | ||||
| use App\Entity\Source\SourceInterface; | ||||
| use Doctrine\Common\Collections\ArrayCollection; | ||||
|  | ||||
| class SourceMembershipInformation implements SourceMembershipInformationInterface | ||||
| { | ||||
|     /** | ||||
|      * @var SourceInterface | ||||
|      */ | ||||
|     private $source; | ||||
|  | ||||
|     /** | ||||
|      * @var Collection | ||||
|      */ | ||||
|     private $memberships; | ||||
|  | ||||
|     public function __construct(SourceInterface $source) | ||||
|     { | ||||
|         $this->source = $source; | ||||
|     } | ||||
|  | ||||
|     private function itterateOverMemberships(Collection $memberships): void | ||||
|     { | ||||
|         /** | ||||
|          * @var SourceInterface | ||||
|          */ | ||||
|         foreach ($memberships as $membership) { | ||||
|             if (!$this->memberships->contains($membership)) { | ||||
|                 $this->memberships->add($membership); | ||||
|                 $this->itterateOverMemberships($membership->getMemberships()); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function getAllMemberships(): Collection | ||||
|     { | ||||
|         $this->memberships = new ArrayCollection(); | ||||
|         $this->itterateOverMemberships($this->source->getMemberships()); | ||||
|  | ||||
|         return $this->memberships; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,10 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Domain\SourceManagement; | ||||
|  | ||||
| use Doctrine\Common\Collections\Collection; | ||||
|  | ||||
| interface SourceMembershipInformationInterface | ||||
| { | ||||
|     public function getAllMemberships(): Collection; | ||||
| } | ||||
							
								
								
									
										7
									
								
								application/src/Exception/RecursiveException.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								application/src/Exception/RecursiveException.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| <?php | ||||
|  | ||||
| namespace App\Exception; | ||||
|  | ||||
| class RecursiveException extends \Exception | ||||
| { | ||||
| } | ||||
| @@ -0,0 +1,57 @@ | ||||
| <?php | ||||
|  | ||||
| namespace Tests\Unit\Domain\SourceManagement; | ||||
|  | ||||
| use PHPUnit\Framework\TestCase; | ||||
| use App\Domain\SourceManagement\SourceMembershipInformationInterface; | ||||
| use App\Domain\SourceManagement\SourceMembershipInformation; | ||||
| use App\Entity\Source\SourceInterface; | ||||
| use App\Entity\Source\Complex\UserSource; | ||||
| use App\Entity\Source\Primitive\Text\TextSource; | ||||
| use App\Entity\Source\Primitive\Name\FirstNameSource; | ||||
| use Doctrine\Common\Collections\ArrayCollection; | ||||
| use App\Entity\Source\Complex\FullPersonNameSource; | ||||
|  | ||||
| class SourceMembershipInformationTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * @var SourceInterface | ||||
|      */ | ||||
|     protected $source; | ||||
|  | ||||
|     /** | ||||
|      * @var SourceMembershipInformationInterface | ||||
|      */ | ||||
|     protected $sourceMembershipInformation; | ||||
|  | ||||
|     public function setUp(): void | ||||
|     { | ||||
|         $this->source = new UserSource(); | ||||
|         $this->sourceMembershipInformation = new SourceMembershipInformation($this->source); | ||||
|     } | ||||
|  | ||||
|     public function testOneDimension(): void | ||||
|     { | ||||
|         $this->source->getMemberships()->add(new TextSource()); | ||||
|         $this->assertEquals(1, $this->sourceMembershipInformation->getAllMemberships()->count()); | ||||
|     } | ||||
|  | ||||
|     public function testThreeDimension(): void | ||||
|     { | ||||
|         $source1 = new TextSource(); | ||||
|         $source2 = new FirstNameSource(); | ||||
|         $source2->setMemberships(new ArrayCollection([$source1])); | ||||
|         $source3 = new FullPersonNameSource(); | ||||
|         $source3->getMemberships()->add($source2); | ||||
|         $this->source->getMemberships()->add($source3); | ||||
|         $this->assertEquals(3, $this->sourceMembershipInformation->getAllMemberships()->count()); | ||||
|     } | ||||
|  | ||||
|     public function testRecursion(): void | ||||
|     { | ||||
|         $recursiveSource = new UserSource(); | ||||
|         $recursiveSource->getMemberships()->add($this->source); | ||||
|         $this->source->getMemberships()->add($recursiveSource); | ||||
|         $this->assertEquals(2, $this->sourceMembershipInformation->getAllMemberships()->count()); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user