Completed
Pull Request — master (#966)
by Jeroen De
120:13 queued 55:14
created

testLowerAndMixedCaseProduceConsistentChecksum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\PaymentContext\Tests\Unit\Domain;
6
7
use PHPUnit\Framework\TestCase;
8
use WMDE\Fundraising\Frontend\PaymentContext\Domain\ChecksumGenerator;
9
10
/**
11
 * @covers \WMDE\Fundraising\Frontend\PaymentContext\Domain\ChecksumGenerator
12
 *
13
 * @licence GNU GPL v2+
14
 */
15
class ChecksumGeneratorTest extends TestCase {
16
17
	public function testCannotConstructWithLessThanTwoCharacters(): void {
18
		$this->expectException( \InvalidArgumentException::class );
19
		new ChecksumGenerator( [ 'a' ] );
20
	}
21
22
	public function testCanGenerateChecksumWithTwoCharacters(): void {
23
		$generator = new ChecksumGenerator( [ 'a', 'b' ] );
24
25
		$this->assertSame( 'b', $generator->createChecksum( 'aaaa' ) );
26
		$this->assertSame( 'b', $generator->createChecksum( 'aaaaa' ) );
27
	}
28
29
	public function testCanGenerateChecksumWithManyCharacters(): void {
30
		$generator = new ChecksumGenerator( str_split( 'ACDEFKLMNPRSTWXYZ349' ) );
31
32
		$this->assertSame( 'X', $generator->createChecksum( 'AAAU' ) );
33
		$this->assertSame( 'K', $generator->createChecksum( 'AAAA' ) );
34
		$this->assertSame( 'C', $generator->createChecksum( 'QAQA' ) );
35
		$this->assertSame( 'D', $generator->createChecksum( 'ABCD' ) );
36
	}
37
38
	public function testIgnoresDashesUnderscoresAndSpaces(): void {
39
		$generator = new ChecksumGenerator( str_split( 'ACDEFKLMNPRSTWXYZ349' ) );
40
41
		$checksum = $generator->createChecksum( 'CAT' );
42
43
		$this->assertSame( $checksum, $generator->createChecksum( 'C-AT-' ) );
44
		$this->assertSame( $checksum, $generator->createChecksum( '_CAT_' ) );
45
		$this->assertSame( $checksum, $generator->createChecksum( 'C A T' ) );
46
	}
47
48
	public function testLowerAndMixedCaseProduceConsistentChecksum(): void {
49
		$generator = new ChecksumGenerator( str_split( 'ACDEFKLMNPRSTWXYZ349' ) );
50
51
		$this->assertSame( 'X', $generator->createChecksum( 'AAAU' ) );
52
		$this->assertSame( 'X', $generator->createChecksum( 'aAAU' ) );
53
54
		$this->assertSame( 'K', $generator->createChecksum( 'aAaa' ) );
55
		$this->assertSame( 'K', $generator->createChecksum( 'aaaa' ) );
56
57
		$this->assertSame( 'C', $generator->createChecksum( 'QaQa' ) );
58
		$this->assertSame( 'C', $generator->createChecksum( 'qaqa' ) );
59
60
		$this->assertSame( 'D', $generator->createChecksum( 'xxxx' ) );
61
		$this->assertSame( 'D', $generator->createChecksum( 'XXXX' ) );
62
	}
63
64
	public function testChecksumIsOneOfTheExpectedCharacters(): void {
65
		$characters = [ 'A', 'B', 'C' ];
66
		$generator = new ChecksumGenerator( $characters );
67
68
		//$distribution = ['A' => 0, 'B' => 0, 'C' => 0];
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
70
		foreach ( $this->getRandomStrings() as $string ) {
71
			//$distribution[$generator->createChecksum( $string )]++;
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72
			$this->assertContains(
73
				$generator->createChecksum( $string ),
74
				$characters
75
			);
76
		}
77
78
		//var_dump($distribution);exit;
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
	}
80
81
	public function getRandomStrings(): iterable {
82
		$characters = str_split( 'ACDEFKLMNPRSTWXYZ349-' );
83
		$characterCount = count( $characters );
84
85
		for ( $i = 0; $i < 1000; $i++ ) {
86
			yield implode(
87
				'',
88
				array_map(
89
					function() use ( $characters, $characterCount ) {
90
						return $characters[mt_rand( 0, $characterCount - 1 )];
91
					},
92
					array_fill( 0, 10, null )
93
				)
94
			);
95
		}
96
	}
97
98
}
99