Passed
Branch master (947605)
by smiley
03:06
created

QRCodeTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 14
c 5
b 0
f 1
lcom 1
cbo 4
dl 0
loc 131
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testInstance() 0 5 1
A stringDataProvider() 0 8 1
A testDataCoverage() 0 3 1
A testTypeAndErrorcorrectlevelCoverage() 0 9 3
A testTypeAutoOverride() 0 4 1
A getTypeNumberDataProvider() 0 20 1
A testInternalGetTypeNumber() 0 9 2
A testNoDataException() 0 3 1
A testErrorCorrectLevelException() 0 4 1
A testCodeLengthOverflowException() 0 7 1
1
<?php
2
3
/**
4
 *
5
 * @filesource   QRCodeTest.php
6
 * @created      08.02.2016
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2015 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\QRCodeTest;
13
14
use chillerlan\QRCode\Output\QRString;
15
use chillerlan\QRCode\QRCode;
16
use chillerlan\QRCode\QRConst;
17
use chillerlan\QRCode\QROptions;
18
use ReflectionClass;
19
20
class QRCodeTest extends \PHPUnit_Framework_TestCase{
21
22
	/**
23
	 * @var \chillerlan\QRCode\QROptions
24
	 */
25
	protected $options;
26
27
	/**
28
	 * @var \chillerlan\QRCode\Output\QROutputInterface
29
	 */
30
	protected $output;
31
32
	/**
33
	 * @var \ReflectionClass
34
	 */
35
	protected $reflectionClass;
36
37
	protected function setUp(){
38
		$this->options = new QROptions;
39
		$this->output = new QRString;
40
		$this->reflectionClass = new ReflectionClass(QRCode::class);
41
	}
42
43
	public function testInstance(){
44
		$this->assertInstanceOf(QRString::class, $this->output);
45
		$this->assertInstanceOf(QROptions::class, $this->options);
46
		$this->assertInstanceOf(QRCode::class, $this->reflectionClass->newInstanceArgs(['foobar', $this->output, $this->options]));
47
	}
48
49
	public function stringDataProvider(){
50
		return [
51
			['1234567890'],
52
			['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
53
			['#\\'],
54
			['茗荷'],
55
		];
56
	}
57
58
	/**
59
	 * @dataProvider stringDataProvider
60
	 */
61
	public function testDataCoverage($data){
62
		(new QRCode($data, $this->output))->getRawData();
63
	}
64
65
	/**
66
	 * @dataProvider stringDataProvider
67
	 */
68
	public function testTypeAndErrorcorrectlevelCoverage($data){
69
		foreach(QRConst::MAX_BITS as $type => $x){
70
			foreach(QRConst::RSBLOCK as $eclevel => $y){
71
				$this->options->typeNumber = $type;
0 ignored issues
show
Documentation Bug introduced by
It seems like $type can also be of type string. However, the property $typeNumber is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
72
				$this->options->errorCorrectLevel = $eclevel;
0 ignored issues
show
Documentation Bug introduced by
It seems like $eclevel can also be of type string. However, the property $errorCorrectLevel is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
73
				$this->assertInstanceOf(QRCode::class, new QRCode($data, $this->output, $this->options));
74
			}
75
		}
76
	}
77
78
	/**
79
	 * @dataProvider stringDataProvider
80
	 */
81
	public function testTypeAutoOverride($data){
82
		$this->options->typeNumber = QRCode::TYPE_05;
83
		new QRCode($data, $this->output, $this->options);
84
	}
85
86
87
	public function getTypeNumberDataProvider(){
88
		return [
89
			[true,  QRCode::TYPE_05, 'foobar'],
90
			[false, QRCode::TYPE_05, 'foobar'],
91
			[true,  QRCode::TYPE_10, 'foobar'],
92
			[false, QRCode::TYPE_10, 'foobar'],
93
			[true,  QRCode::TYPE_05, '1234567890'],
94
			[false, QRCode::TYPE_05, '1234567890'],
95
			[true,  QRCode::TYPE_10, '1234567890'],
96
			[false, QRCode::TYPE_10, '1234567890'],
97
			[true,  QRCode::TYPE_05, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
98
			[false, QRCode::TYPE_05, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
99
			[true,  QRCode::TYPE_10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
100
			[false, QRCode::TYPE_10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
101
			[true,  QRCode::TYPE_05, '茗荷'],
102
			[false, QRCode::TYPE_05, '茗荷'],
103
			[true,  QRCode::TYPE_10, '茗荷'],
104
			[false, QRCode::TYPE_10, '茗荷'],
105
		];
106
	}
107
108
	/**
109
	 * @dataProvider getTypeNumberDataProvider
110
	 */
111
	public function testInternalGetTypeNumber($test, $type, $data){
112
		$method = $this->reflectionClass->getMethod('getMatrix');
113
		$method->setAccessible(true);
114
		$this->options->typeNumber = $type;
115
116
		for($i = 0; $i <= 7; $i++){
117
			$method->invokeArgs($this->reflectionClass->newInstanceArgs([$data, $this->output, $this->options]), [$test, $i]);
118
		}
119
	}
120
121
	/**
122
	 * @expectedException \chillerlan\QRCode\QRCodeException
123
	 * @expectedExceptionMessage No data given.
124
	 */
125
	public function testNoDataException(){
126
		$this->reflectionClass->newInstanceArgs(['', $this->output]);
127
	}
128
129
	/**
130
	 * @expectedException \chillerlan\QRCode\QRCodeException
131
	 * @expectedExceptionMessage Invalid error correct level: 42
132
	 */
133
	public function testErrorCorrectLevelException(){
134
		$this->options->errorCorrectLevel = 42;
135
		$this->reflectionClass->newInstanceArgs(['foobar', $this->output, $this->options]);
136
	}
137
138
	/**
139
	 * @expectedException \chillerlan\QRCode\QRCodeException
140
	 * @expectedExceptionMessage code length overflow. (261 > 72bit)
141
	 */
142
	public function testCodeLengthOverflowException(){
143
		$this->options->typeNumber = QRCode::TYPE_01;
144
		$this->options->errorCorrectLevel = QRCode::ERROR_CORRECT_LEVEL_H;
145
146
		$method = $this->reflectionClass->getMethod('getRawData');
147
		$method->invoke($this->reflectionClass->newInstanceArgs(['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', $this->output, $this->options]));
148
	}
149
150
}
151