Skip to content

Commit 14719f9

Browse files
authored
Merge pull request #27 from wmde/trim-inputs
Trim string inputs
2 parents 4d3a7d0 + 497254b commit 14719f9

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

src/UseCases/ChangeAddress/ChangeAddressRequest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getSalutation(): string {
4242

4343
public function setSalutation( string $salutation ): self {
4444
$this->assertIsWritable();
45-
$this->salutation = $salutation;
45+
$this->salutation = trim( $salutation );
4646
return $this;
4747
}
4848

@@ -52,7 +52,7 @@ public function getCompany(): string {
5252

5353
public function setCompany( string $company ): self {
5454
$this->assertIsWritable();
55-
$this->company = $company;
55+
$this->company = trim( $company );
5656
return $this;
5757
}
5858

@@ -62,7 +62,7 @@ public function getTitle(): string {
6262

6363
public function setTitle( string $title ): self {
6464
$this->assertIsWritable();
65-
$this->title = $title;
65+
$this->title = trim( $title );
6666
return $this;
6767
}
6868

@@ -72,7 +72,7 @@ public function getFirstName(): string {
7272

7373
public function setFirstName( string $firstName ): self {
7474
$this->assertIsWritable();
75-
$this->firstName = $firstName;
75+
$this->firstName = trim( $firstName );
7676
return $this;
7777
}
7878

@@ -82,7 +82,7 @@ public function getLastName(): string {
8282

8383
public function setLastName( string $lastName ): self {
8484
$this->assertIsWritable();
85-
$this->lastName = $lastName;
85+
$this->lastName = trim( $lastName );
8686
return $this;
8787
}
8888

@@ -92,7 +92,7 @@ public function getAddress(): string {
9292

9393
public function setAddress( string $address ): self {
9494
$this->assertIsWritable();
95-
$this->address = $address;
95+
$this->address = trim( $address );
9696
return $this;
9797
}
9898

@@ -102,7 +102,7 @@ public function getPostcode(): string {
102102

103103
public function setPostcode( string $postcode ): self {
104104
$this->assertIsWritable();
105-
$this->postcode = $postcode;
105+
$this->postcode = trim( $postcode );
106106
return $this;
107107
}
108108

@@ -112,7 +112,7 @@ public function getCity(): string {
112112

113113
public function setCity( string $city ): self {
114114
$this->assertIsWritable();
115-
$this->city = $city;
115+
$this->city = trim( $city );
116116
return $this;
117117
}
118118

@@ -122,7 +122,7 @@ public function getCountry(): string {
122122

123123
public function setCountry( string $country ): self {
124124
$this->assertIsWritable();
125-
$this->country = $country;
125+
$this->country = trim( $country );
126126
return $this;
127127
}
128128

@@ -150,7 +150,7 @@ public function getIdentifier(): string {
150150

151151
public function setIdentifier( string $identifier ): self {
152152
$this->assertIsWritable();
153-
$this->identifier = $identifier;
153+
$this->identifier = trim( $identifier );
154154
return $this;
155155
}
156156

src/UseCases/ChangeAddress/ChangeAddressResponse.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
class ChangeAddressResponse {
88

9+
public const ERROR_ADDRESS_NOT_FOUND = 'Address not found';
10+
911
/**
1012
* @var array<string>
1113
*/

src/UseCases/ChangeAddress/ChangeAddressUseCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct( AddressChangeRepository $addressChangeRepository )
2020
public function changeAddress( ChangeAddressRequest $request ): ChangeAddressResponse {
2121
$addressChange = $this->addressChangeRepository->getAddressChangeByUuid( $request->getIdentifier() );
2222
if ( $addressChange === null ) {
23-
return ChangeAddressResponse::newErrorResponse( [ 'Unknown address.' ] );
23+
return ChangeAddressResponse::newErrorResponse( [ ChangeAddressResponse::ERROR_ADDRESS_NOT_FOUND ] );
2424
}
2525

2626
$newIdentifier = AddressChangeId::fromString( AddressChangeBuilder::generateUuid() );
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
declare( strict_types=1 );
3+
4+
namespace WMDE\Fundraising\AddressChangeContext\Tests\Unit\UseCases;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressChange;
8+
use WMDE\Fundraising\AddressChangeContext\UseCases\ChangeAddress\ChangeAddressRequest;
9+
10+
/**
11+
* @covers \WMDE\Fundraising\AddressChangeContext\UseCases\ChangeAddress\ChangeAddressRequest
12+
*/
13+
class ChangeAddressRequestTest extends TestCase {
14+
public function testAccessors(): void {
15+
$request = new ChangeAddressRequest();
16+
$request->setSalutation( 'Herr' );
17+
$request->setTitle( 'Dr.' );
18+
$request->setFirstName( 'Bruce' );
19+
$request->setLastName( 'Wayne' );
20+
$request->setCompany( 'Wayne Enterprises' );
21+
$request->setPostcode( '66484' );
22+
$request->setAddress( 'Fledergasse 9' );
23+
$request->setCity( 'Battweiler' );
24+
$request->setCountry( 'ZZ' );
25+
$request->setAddressType( AddressChange::ADDRESS_TYPE_PERSON );
26+
$request->setDonationReceipt( true );
27+
$request->setIdentifier( '0caffee' );
28+
$request->setIsOptOutOnly( false );
29+
$optOutRequest = new ChangeAddressRequest();
30+
$optOutRequest->setIsOptOutOnly( true );
31+
32+
$this->assertSame( 'Herr', $request->getSalutation() );
33+
$this->assertSame( 'Dr.', $request->getTitle() );
34+
$this->assertSame( 'Bruce', $request->getFirstName() );
35+
$this->assertSame( 'Wayne', $request->getLastName() );
36+
$this->assertSame( 'Wayne Enterprises', $request->getCompany() );
37+
$this->assertSame( 'Fledergasse 9', $request->getAddress() );
38+
$this->assertSame( '66484', $request->getPostcode() );
39+
$this->assertSame( 'Battweiler', $request->getCity() );
40+
$this->assertSame( 'ZZ', $request->getCountry() );
41+
$this->assertSame( AddressChange::ADDRESS_TYPE_PERSON, $request->getAddressType() );
42+
$this->assertSame( '0caffee', $request->getIdentifier() );
43+
$this->assertTrue( $request->isPersonal() );
44+
$this->assertFalse( $request->isCompany() );
45+
$this->assertFalse( $request->isOptedOutOfDonationReceipt() );
46+
$this->assertTrue( $request->hasAddressChangeData() );
47+
$this->assertFalse( $optOutRequest->hasAddressChangeData() );
48+
}
49+
50+
public function testStringFieldsAreTrimmed(): void {
51+
$request = new ChangeAddressRequest();
52+
$request->setSalutation( ' Herr ' );
53+
$request->setTitle( 'Dr. ' );
54+
$request->setFirstName( ' Bruce ' );
55+
$request->setLastName( 'Wayne ' );
56+
$request->setCompany( ' Wayne Enterprises' );
57+
$request->setPostcode( "66484 \n" );
58+
$request->setAddress( "\t Fledergasse 9 " );
59+
$request->setCity( ' Battweiler ' );
60+
$request->setCountry( ' ZZ ' );
61+
$request->setIdentifier( ' 0caffee ' );
62+
63+
$this->assertSame( 'Herr', $request->getSalutation() );
64+
$this->assertSame( 'Dr.', $request->getTitle() );
65+
$this->assertSame( 'Bruce', $request->getFirstName() );
66+
$this->assertSame( 'Wayne', $request->getLastName() );
67+
$this->assertSame( 'Wayne Enterprises', $request->getCompany() );
68+
$this->assertSame( 'Fledergasse 9', $request->getAddress() );
69+
$this->assertSame( '66484', $request->getPostcode() );
70+
$this->assertSame( 'Battweiler', $request->getCity() );
71+
$this->assertSame( 'ZZ', $request->getCountry() );
72+
$this->assertSame( '0caffee', $request->getIdentifier() );
73+
}
74+
}

tests/Unit/UseCases/ChangeAddressUseCaseTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressChange;
1010
use WMDE\Fundraising\AddressChangeContext\Domain\Model\AddressChangeId;
1111
use WMDE\Fundraising\AddressChangeContext\UseCases\ChangeAddress\ChangeAddressRequest;
12+
use WMDE\Fundraising\AddressChangeContext\UseCases\ChangeAddress\ChangeAddressResponse;
1213
use WMDE\Fundraising\AddressChangeContext\UseCases\ChangeAddress\ChangeAddressUseCase;
1314

1415
/**
1516
* @covers \WMDE\Fundraising\AddressChangeContext\UseCases\ChangeAddress\ChangeAddressUseCase
17+
* @covers \WMDE\Fundraising\AddressChangeContext\UseCases\ChangeAddress\ChangeAddressResponse
1618
*/
1719
class ChangeAddressUseCaseTest extends TestCase {
1820

@@ -47,6 +49,7 @@ public function testGivenAddressChangeRequestIdentifierCannotBeFound_errorRespon
4749
$useCase = new ChangeAddressUseCase( $mockAddressChangeRepository );
4850
$response = $useCase->changeAddress( $this->newChangeAddressRequest() );
4951
$this->assertFalse( $response->isSuccess() );
52+
$this->assertEquals( [ ChangeAddressResponse::ERROR_ADDRESS_NOT_FOUND ], $response->getErrors() );
5053
}
5154

5255
public function testGivenValidOptOutOnlyChangeRequest_successResponseIsReturned(): void {

0 commit comments

Comments
 (0)