Completed
Push — master ( 1c8af5...5745ff )
by thomas
04:13 queued 02:50
created

EncodeTest::validAddressProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace BitWasp\Test\Unit\Bech32;
4
5
6
use BitWasp\Test\Bech32\Provider\ValidAddresses;
7
use BitWasp\Test\Bech32\TestCase;
8
use BitWasp\Test\Bech32\Util;
9
10
class EncodeTest extends TestCase
11
{
12
    /**
13
     * @return array
14
     */
15
    public function validAddressProvider()
16
    {
17
        return ValidAddresses::load();
18
    }
19
20
    /**
21
     * https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L106
22
     * @param string $hrp
23
     * @param string $bech32
24
     * @param string $hexScript
25
     * @dataProvider validAddressProvider
26
     */
27
    public function testValidAddress($hrp, $bech32, $hexScript)
28
    {
29
        // Check we decode, and that HRP matches test fixture
30
        list ($gotHRP, $data) = \Bitwasp\Bech32\decode($bech32);
31
        $this->assertEquals($hrp, $gotHRP);
32
33
        $decoded = \BitWasp\Bech32\convertBits(array_slice($data, 1), count($data) - 1, 5, 8, false);
34
        $program = '';
35
        foreach ($decoded as $char) {
36
            $program .= chr($char);
37
        }
38
39
        // Check decoded details against our known witness program
40
        $version = $data[0];
41
        $checkWitnessProgram = Util::witnessProgram($version, $program);
42
        $this->assertEquals($hexScript, $checkWitnessProgram);
43
44
        // Simple re-encoding test
45
46
        $encoded = \BitWasp\Bech32\encode($hrp, $data);
47
        $this->assertEquals(strtolower($bech32), $encoded);
48
    }
49
50
}
51