Completed
Push — master ( 00092a...39103d )
by wiese
86:17 queued 21:06
created

newUniqueGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\PaymentContext\Tests\Integration\DataAccess;
6
7
use Doctrine\ORM\EntityManager;
8
use WMDE\Euro\Euro;
9
use WMDE\Fundraising\Frontend\DonationContext\DataAccess\DoctrineDonationRepository;
10
use WMDE\Fundraising\Frontend\DonationContext\Domain\Model\Donation;
11
use WMDE\Fundraising\Frontend\DonationContext\Domain\Model\DonationPayment;
12
use WMDE\Fundraising\Frontend\PaymentContext\DataAccess\UniqueTransferCodeGenerator;
13
use WMDE\Fundraising\Frontend\PaymentContext\Domain\Model\BankTransferPayment;
14
use WMDE\Fundraising\Frontend\PaymentContext\Domain\TransferCodeGenerator;
15
use WMDE\Fundraising\Frontend\DonationContext\Tests\Data\ValidDonation;
16
use WMDE\Fundraising\Frontend\Tests\TestEnvironment;
17
18
/**
19
 * @covers \WMDE\Fundraising\Frontend\PaymentContext\DataAccess\UniqueTransferCodeGenerator
20
 *
21
 * @licence GNU GPL v2+
22
 * @author Jeroen De Dauw < [email protected] >
23
 */
24
class UniqueTransferCodeGeneratorTest extends \PHPUnit\Framework\TestCase {
25
26
	/**
27
	 * @var EntityManager
28
	 */
29
	private $entityManager;
30
31
	public function setUp(): void {
32
		$this->entityManager = TestEnvironment::newInstance()->getFactory()->getEntityManager();
33
	}
34
35
	private function newUniqueGenerator(): TransferCodeGenerator {
36
		return new UniqueTransferCodeGenerator(
37
			$this->newFakeGenerator(),
38
			$this->entityManager
39
		);
40
	}
41
42
	private function newFakeGenerator(): TransferCodeGenerator {
43
		return new class() implements TransferCodeGenerator {
44
			private $position = 0;
45
46
			public function generateTransferCode(): string {
47
				return ['first', 'second', 'third'][$this->position++];
48
			}
49
		};
50
	}
51
52
	public function testWhenFirstResultIsUnique_itGetsReturned(): void {
53
		$this->assertSame( 'first', $this->newUniqueGenerator()->generateTransferCode() );
54
	}
55
56
	public function testWhenFirstResultIsNotUnique_secondResultGetsReturned(): void {
57
		$this->storeDonationWithTransferCode( 'first' );
58
		$this->assertSame( 'second', $this->newUniqueGenerator()->generateTransferCode() );
59
	}
60
61
	private function storeDonationWithTransferCode( string $code ): void {
62
		$donation = new Donation(
63
			null,
64
			Donation::STATUS_NEW,
65
			ValidDonation::newDonor(),
66
			new DonationPayment(
67
				Euro::newFromFloat( 13.37 ),
68
				3,
69
				new BankTransferPayment( $code )
70
			),
71
			Donation::OPTS_INTO_NEWSLETTER,
72
			ValidDonation::newTrackingInfo()
73
		);
74
75
		( new DoctrineDonationRepository( $this->entityManager ) )->storeDonation( $donation );
76
	}
77
78
	public function testWhenFirstAndSecondResultsAreNotUnique_thirdResultGetsReturned(): void {
79
		$this->storeDonationWithTransferCode( 'first' );
80
		$this->storeDonationWithTransferCode( 'second' );
81
		$this->assertSame( 'third', $this->newUniqueGenerator()->generateTransferCode() );
82
	}
83
84
}