Completed
Pull Request — master (#649)
by Jeroen De
50:49
created

storeMembershipApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 13
nc 1
nop 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\MembershipApplicationContext\Tests\Integration\DataAccess;
6
7
use Doctrine\ORM\EntityManager;
8
use WMDE\Fundraising\Entities\MembershipApplication;
9
use WMDE\Fundraising\Frontend\MembershipApplicationContext\DataAccess\DoctrineApplicationRepository;
10
use WMDE\Fundraising\Frontend\Tests\TestEnvironment;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Kai Nissen < [email protected] >
15
 */
16
class SerializedDataHandlingTest extends \PHPUnit_Framework_TestCase {
17
18
	/** @dataProvider encodedMembershipDataProvider */
19
	public function testDataFieldOfMembershipApplicationIsInteractedWithCorrectly( $data ) {
20
		$entityManager = TestEnvironment::newInstance()->getFactory()->getEntityManager();
21
22
		$repository = new DoctrineApplicationRepository( $entityManager );
23
		$this->storeMembershipApplication( $entityManager, $data );
24
25
		$donation = $repository->getApplicationById( 1 );
26
		$repository->storeApplication( $donation );
0 ignored issues
show
Bug introduced by
It seems like $donation defined by $repository->getApplicationById(1) on line 25 can be null; however, WMDE\Fundraising\Fronten...ory::storeApplication() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
27
28
		/** @var MembershipApplication $dma */
29
		$dma = $entityManager->find( MembershipApplication::class, 1 );
30
		$this->assertEquals( $data, $dma->getDecodedData() );
31
	}
32
33
	public function encodedMembershipDataProvider() {
34
		return [
35
			[
36
				[
37
					'member_agree' => '1',
38
					'membership_fee_custom' => '',
39
					'confirmationPage' => '10h16 Bestätigung-BEZ',
40
					'confirmationPageCampaign' => 'MT15_WMDE_02',
41
					'token' => '7998466$225c4e182d5b0f3d0e802624826200d21c900267',
42
					'utoken' => 'c73d8d1b3e61a73f50bd37d0bf39f3930d77f02b',
43
				]
44
			],
45
46
			[
47
				[
48
					'member_agree' => '1',
49
					'membership_fee_custom' => '',
50
					'confirmationPage' => '10h16 Bestätigung-BEZ',
51
					'confirmationPageCampaign' => 'MT15_WMDE_02',
52
					'token' => '1676175$26905b01f48c8c471f0617217540a8c82fdde52c',
53
					'utoken' => '87bd8cedc7843525b87f287b1e3299f667eb7a22',
54
				]
55
			],
56
57
			[
58
				[
59
					'member_agree' => '1',
60
					'membership_fee_custom' => '',
61
					'confirmationPage' => '10h16 Bestätigung',
62
					'confirmationPageCampaign' => 'MT15_WMDE_02',
63
					'token' => '3246619$c4e64cc3c49d8f8b8a0bdcf5788a029563ed9598',
64
					'utoken' => '134bf8fce220de781d7b093711f36b5323ccfee5',
65
				]
66
			]
67
		];
68
	}
69
70
	private function storeMembershipApplication( EntityManager $entityManager, array $data ) {
71
		$membershipAppl = new MembershipApplication();
72
73
		$membershipAppl->setApplicantSalutation( 'Frau' );
74
		$membershipAppl->setApplicantTitle( 'Dr.' );
75
		$membershipAppl->setApplicantFirstName( 'Martha' );
76
		$membershipAppl->setApplicantLastName( 'Muster' );
77
		$membershipAppl->setCity( 'Smalltown' );
78
		$membershipAppl->setPostcode( '12345' );
79
		$membershipAppl->setAddress( 'Erlenkamp 12' );
80
		$membershipAppl->setApplicantEmailAddress( '[email protected]' );
81
82
		$membershipAppl->encodeAndSetData( $data );
83
		$entityManager->persist( $membershipAppl );
84
		$entityManager->flush();
85
	}
86
87
}
88