Completed
Pull Request — master (#546)
by thomas
132:26 queued 62:26
created

AbstractTestCase::calcMapScriptFlags()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\RpcTest;
4
5
use BitWasp\Bitcoin\Network\NetworkInterface;
6
use BitWasp\Bitcoin\Script\Interpreter\Interpreter;
7
8
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @var array
12
     */
13
    private $scriptFlagNames;
14
15
    /**
16
     * @var NetworkInterface
17
     */
18
    protected $network;
19
20
    /**
21
     * AbstractTestCase constructor.
22
     * @param null $name
23
     * @param array $data
24
     * @param string $dataName
25
     */
26
    public function __construct($name = null, array $data = [], $dataName = '')
27
    {
28
        parent::__construct($name, $data, $dataName);
29
    }
30
31
    /**
32
     * @param string $name
33
     * @return array
34
     */
35
    public function jsonDataFile($name)
36
    {
37
        $contents = $this->dataFile($name);
38
        $decoded = json_decode($contents, true);
39
        if (false === $decoded || json_last_error() !== JSON_ERROR_NONE) {
40
            throw new \RuntimeException('Invalid JSON file ' . $name);
41
        }
42
43
        return $decoded;
44
    }
45
46
    /**
47
     * @param string $filename
48
     * @return string
49
     */
50
    public function dataFile($filename)
51
    {
52
        $contents = file_get_contents($this->dataPath($filename));
53
        if (false === $contents) {
54
            throw new \RuntimeException('Failed to data file ' . $filename);
55
        }
56
        return $contents;
57
    }
58
59
    /**
60
     * @param string $file
61
     * @return string
62
     */
63
    public function dataPath($file)
64
    {
65
        return __DIR__ . '/../tests/Data/' . $file;
66
    }
67
68
69
    /**
70
     * @return array
71
     */
72
    public function calcMapScriptFlags()
73
    {
74
        if (null === $this->scriptFlagNames) {
75
            $this->scriptFlagNames = [
76
                "NONE" => Interpreter::VERIFY_NONE,
77
                "P2SH" => Interpreter::VERIFY_P2SH,
78
                "STRICTENC" => Interpreter::VERIFY_STRICTENC,
79
                "DERSIG" => Interpreter::VERIFY_DERSIG,
80
                "LOW_S" => Interpreter::VERIFY_LOW_S,
81
                "SIGPUSHONLY" => Interpreter::VERIFY_SIGPUSHONLY,
82
                "MINIMALDATA" => Interpreter::VERIFY_MINIMALDATA,
83
                "NULLDUMMY" => Interpreter::VERIFY_NULL_DUMMY,
84
                "DISCOURAGE_UPGRADABLE_NOPS" => Interpreter::VERIFY_DISCOURAGE_UPGRADABLE_NOPS,
85
                "CLEANSTACK" => Interpreter::VERIFY_CLEAN_STACK,
86
                "CHECKLOCKTIMEVERIFY" => Interpreter::VERIFY_CHECKLOCKTIMEVERIFY,
87
                "CHECKSEQUENCEVERIFY" => Interpreter::VERIFY_CHECKSEQUENCEVERIFY,
88
                "WITNESS" => Interpreter::VERIFY_WITNESS,
89
                "DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM" => Interpreter::VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM,
90
                "MINIMALIF" => Interpreter::VERIFY_MINIMALIF,
91
                "NULLFAIL" => Interpreter::VERIFY_NULLFAIL,
92
            ];
93
        }
94
95
        return $this->scriptFlagNames;
96
    }
97
98
    /**
99
     * @param string $string
100
     * @return int
101
     */
102
    public function getScriptFlagsFromString($string)
103
    {
104
        $mapFlagNames = $this->calcMapScriptFlags();
105
        if (strlen($string) === 0) {
106
            return Interpreter::VERIFY_NONE;
107
        }
108
109
        $flags = 0;
110
        $words = explode(",", $string);
111
        foreach ($words as $word) {
112
            if (!isset($mapFlagNames[$word])) {
113
                throw new \RuntimeException('Unknown verification flag: ' . $word);
114
            }
115
116
            $flags |= $mapFlagNames[$word];
117
        }
118
119
        return $flags;
120
    }
121
122
}
123