Skip to content

Commit

Permalink
Merge pull request #223 from wmde/company-with-contact
Browse files Browse the repository at this point in the history
Add CompanyContactName
  • Loading branch information
gbirke authored Sep 20, 2023
2 parents 83697ca + 8dae92a commit 120f4ec
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 13 deletions.
10 changes: 8 additions & 2 deletions src/DataAccess/DonorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Address\PostalAddress;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\AnonymousDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\CompanyDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor;

Expand All @@ -31,7 +31,13 @@ public static function createDonorFromEntity( DoctrineDonation $donation ): Dono
);
case 'firma':
return new CompanyDonor(
new CompanyName( $data->getValue( 'firma' ) ),
new CompanyContactName(
$data->getValue( 'firma' ),
$data->getValue( 'vorname' ),
$data->getValue( 'nachname' ),
$data->getValue( 'anrede' ),
$data->getValue( 'titel' )
),
self::createPhysicalAddress( $data ),
$donation->getDonorEmail() ?? ''
);
Expand Down
3 changes: 2 additions & 1 deletion src/Domain/Model/Donor/CompanyDonor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace WMDE\Fundraising\DonationContext\Domain\Model\Donor;

use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Address\PostalAddress;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName;
use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;

class CompanyDonor extends AbstractDonor {
use NewsletterTrait;
use ReceiptTrait;

public function __construct( CompanyName $name, PostalAddress $address, string $emailAddress ) {
public function __construct( CompanyName|CompanyContactName $name, PostalAddress $address, string $emailAddress ) {
$this->name = $name;
$this->physicalAddress = $address;
$this->emailAddress = $emailAddress;
Expand Down
49 changes: 49 additions & 0 deletions src/Domain/Model/Donor/Name/CompanyContactName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare( strict_types = 1 );

namespace WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name;

use WMDE\Fundraising\DonationContext\Domain\Model\DonorName;

class CompanyContactName implements DonorName {

public function __construct(
private readonly string $companyName,
private readonly string $firstName,
private readonly string $lastName,
private readonly string $salutation,
private readonly string $title
) {
}

public function getFullName(): string {
$name = $this->companyName;

if ( $this->title !== '' || $this->firstName !== '' || $this->lastName !== '' ) {
$name .= ' - ' . implode(
' ',
array_filter( [
$this->title,
$this->firstName,
$this->lastName
] )
);
}

return $name;
}

public function toArray(): array {
return array_merge( [
'companyName' => $this->companyName,
],
array_filter( [
'salutation' => $this->salutation,
'title' => $this->title,
'firstName' => $this->firstName,
'lastName' => $this->lastName
] )
);
}
}
3 changes: 3 additions & 0 deletions src/Domain/Model/Donor/Name/CompanyName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use WMDE\Fundraising\DonationContext\Domain\Model\DonorName;

/**
* @deprecated Please use CompanyContactName instead
*/
class CompanyName implements DonorName {

private string $companyName;
Expand Down
10 changes: 8 additions & 2 deletions src/UseCases/AddDonation/AddDonationUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Address\PostalAddress;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\AnonymousDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\CompanyDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;
Expand Down Expand Up @@ -117,7 +117,13 @@ private function getPersonalInfoFromRequest( AddDonationRequest $request ): Dono
);
} elseif ( $donorType->is( DonorType::COMPANY() ) ) {
return new CompanyDonor(
new CompanyName( $request->getDonorCompany() ),
new CompanyContactName(
$request->getDonorCompany(),
$request->getDonorFirstName(),
$request->getDonorLastName(),
$request->getDonorSalutation(),
$request->getDonorTitle()
),
$this->getPhysicalAddressFromRequest( $request ),
$request->getDonorEmailAddress()
);
Expand Down
10 changes: 8 additions & 2 deletions src/UseCases/UpdateDonor/UpdateDonorUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use WMDE\Fundraising\DonationContext\Domain\Model\Donor;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Address\PostalAddress;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\CompanyDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;
Expand Down Expand Up @@ -108,7 +108,13 @@ private function getDonorFromRequest( UpdateDonorRequest $updateDonorRequest ):

} elseif ( $updateDonorRequest->getDonorType()->is( DonorType::COMPANY() ) ) {
return new CompanyDonor(
new CompanyName( $updateDonorRequest->getCompanyName() ),
new CompanyContactName(
$updateDonorRequest->getCompanyName(),
$updateDonorRequest->getFirstName(),
$updateDonorRequest->getLastName(),
$updateDonorRequest->getSalutation(),
$updateDonorRequest->getTitle()
),
$this->getDonorAddressFromRequest( $updateDonorRequest ),
$updateDonorRequest->getEmailAddress()
);
Expand Down
3 changes: 2 additions & 1 deletion tests/Data/ValidDoctrineDonation.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static function newCompanyDonation(): Donation {
'firma' => ValidDonation::DONOR_COMPANY
],
$self->getAddressArray(),
[ 'email' => ValidDonation::DONOR_EMAIL_ADDRESS ]
[ 'email' => ValidDonation::DONOR_EMAIL_ADDRESS ],
$self->getPersonNameArray()
)
);
return $donation;
Expand Down
12 changes: 9 additions & 3 deletions tests/Data/ValidDonation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\AnonymousDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\CompanyDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\EmailDonor;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor;
use WMDE\Fundraising\PaymentContext\Domain\Model\Payment;
Expand Down Expand Up @@ -245,8 +245,14 @@ public static function newCompanyDonor(): CompanyDonor {
);
}

private static function newCompanyName(): CompanyName {
return new CompanyName( self::DONOR_COMPANY );
private static function newCompanyName(): CompanyContactName {
return new CompanyContactName(
self::DONOR_COMPANY,
self::DONOR_FIRST_NAME,
self::DONOR_LAST_NAME,
self::DONOR_SALUTATION,
self::DONOR_TITLE
);
}

public static function newEmailOnlyDonor(): EmailDonor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use WMDE\Fundraising\DonationContext\Authorization\DonationTokens;
use WMDE\Fundraising\DonationContext\Domain\Event\DonationCreatedEvent;
use WMDE\Fundraising\DonationContext\Domain\Model\Donation;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName;
use WMDE\Fundraising\DonationContext\Domain\Model\DonorType;
use WMDE\Fundraising\DonationContext\Domain\Model\ModerationIdentifier;
use WMDE\Fundraising\DonationContext\Domain\Model\ModerationReason;
Expand Down Expand Up @@ -295,7 +295,7 @@ public function testEventIsEmittedAfterDonationWasStored(): void {
$events = $eventEmitter->getEvents();
$this->assertCount( 1, $events, 'Only 1 event should be emitted' );
$this->assertInstanceOf( DonationCreatedEvent::class, $events[0] );
$this->assertInstanceOf( CompanyName::class, $events[0]->getDonor()->getName() );
$this->assertInstanceOf( CompanyContactName::class, $events[0]->getDonor()->getName() );
}

public function testWhenEmailAddressIsBlacklisted_donationIsMarkedAsCancelled(): void {
Expand Down
64 changes: 64 additions & 0 deletions tests/Unit/Domain/Model/CompanyContactNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace WMDE\Fundraising\DonationContext\Tests\Unit\Domain\Model;

use PHPUnit\Framework\TestCase;
use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName;

/**
* @covers \WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName
*/
class CompanyContactNameTest extends TestCase {

public function testGivenCompanyNameOnly_getFullNameReturnsCompanyName(): void {
$companyName = new CompanyContactName( 'Scrooge and Marley', '', '', '', '' );

$this->assertSame( 'Scrooge and Marley', $companyName->getFullName() );
}

public function testGivenNameWithoutSalutation_getFullNameReturnsNameWithoutSalutation(): void {
$companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', '', '' );

$this->assertSame( 'Scrooge and Marley - Ebenezer Scrooge', $companyName->getFullName() );
}

public function testGivenNameWithSalutation_getFullNameReturnsNameWithoutSalutation(): void {
$companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', 'Sir', '' );

$this->assertSame( 'Scrooge and Marley - Ebenezer Scrooge', $companyName->getFullName() );
}

public function testGivenNameWithTitle_getFullNameReturnsNameWithTitle(): void {
$companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', '', 'Prof. Dr.' );

$this->assertSame( 'Scrooge and Marley - Prof. Dr. Ebenezer Scrooge', $companyName->getFullName() );
}

public function testGivenNameWithTitleAndSalutation_getFullNameReturnsNameWithTitle(): void {
$companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', 'Herr', 'Prof. Dr.' );

$this->assertSame( 'Scrooge and Marley - Prof. Dr. Ebenezer Scrooge', $companyName->getFullName() );
}

public function testGivenCompanyNameOnly_toArrayReturnsCompanyName(): void {
$companyName = new CompanyContactName( 'Scrooge and Marley', '', '', '', '' );

$this->assertEquals( [ 'companyName' => 'Scrooge and Marley', ], $companyName->toArray() );
}

public function testToArrayReturnsAllFields(): void {
$companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', 'Herr', 'Prof. Dr.' );

$this->assertEquals(
[
'companyName' => 'Scrooge and Marley',
'salutation' => 'Herr',
'title' => 'Prof. Dr.',
'firstName' => 'Ebenezer',
'lastName' => 'Scrooge'
],
$companyName->toArray()
);
}

}

0 comments on commit 120f4ec

Please sign in to comment.