ColumnTypeGuesserTest::testConstruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
use \Faker\Generator;
4
5
App::uses('ColumnTypeGuesser', 'FakeSeeder.Lib');
6
7
/**
8
 * ColumnTypeGuesser Test
9
 *
10
 * @coversDefaultClass ColumnTypeGuesser
11
 */
12
class ColumnTypeGuesserTest extends CakeTestCase {
13
14
	/**
15
	 * The faker instance
16
	 *
17
	 * @var null|Generator
18
	 */
19
	protected $_faker = null;
20
21
	/**
22
	 * The object under test
23
	 *
24
	 * @var null|ColumnTypeGuesser
25
	 */
26
	protected $_guesser = null;
27
28
	/**
29
	 * Setup the object under test
30
	 *
31
	 * @return void
32
	 */
33
	public function setUp() {
34
		parent::setUp();
35
36
		$this->_faker = $this->getMock('\\Faker\\Generator', array('__get', '__call'));
37
		$this->_guesser = new ColumnTypeGuesser($this->_faker);
38
	}
39
40
	/**
41
	 * Test the constructor
42
	 *
43
	 * @return void
44
	 * @covers ::__construct
45
	 */
46
	public function testConstruct() {
47
		$guesser = new ColumnTypeGuesser($this->_faker);
0 ignored issues
show
Bug introduced by
It seems like $this->_faker can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
48
		$this->assertAttributeInstanceOf('\\Faker\\Generator', '_generator', $guesser);
49
	}
50
51
	/**
52
	 * Tests the guessFormat method with various types
53
	 *
54
	 * @param string $magicMethod The name of the magic method (__call or __get).
55
	 * @param string $method The name of the method (e.g. randomNumber).
56
	 * @param array $column The column schema with type etc.
57
	 * @return void
58
	 * @covers ::guessFormat
59
	 * @dataProvider formatProvider
60
	 */
61
	public function testGuessFormats($magicMethod, $method, $column) {
62
		$this->_faker->expects($this->at(0))->method($magicMethod)->with(
63
			$this->equalTo($method)
64
		);
65
		$result = $this->_guesser->guessFormat($column);
66
		$result();
67
	}
68
69
	/**
70
	 * Tests the guessFormat method with various types resulting to null
71
	 *
72
	 * @param null|array $column The column schema with type etc.
73
	 * @return void
74
	 * @covers ::guessFormat
75
	 * @dataProvider specialFormatProvider
76
	 */
77
	public function testGuessSpecialFormat($column) {
78
		$result = $this->_guesser->guessFormat($column);
0 ignored issues
show
Bug introduced by
It seems like $column defined by parameter $column on line 77 can also be of type null; however, ColumnTypeGuesser::guessFormat() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
79
		$this->assertEquals($result, null);
80
	}
81
82
	/**
83
	 * A format data provider
84
	 *
85
	 * @return array Formats
86
	 */
87
	public function formatProvider() {
88
		return array(
89
			'boolean' => array('__get', 'boolean', array('type' => 'boolean')),
90
			'integer' => array('__call', 'randomNumber', array('type' => 'integer')),
91
			'bigInteger' => array('__call', 'randomNumber', array('type' => 'biginteger')),
92
			'decimal' => array('__call', 'randomFloat', array('type' => 'decimal')),
93
			'float' => array('__call', 'randomFloat', array('type' => 'float')),
94
			'uuid' => array('__call', 'uuid', array('type' => 'uuid')),
95
			'lexify' => array('__call', 'lexify', array('type' => 'string', 'length' => 4)),
96
			'string' => array('__call', 'text', array('type' => 'string', 'length' => 20)),
97
			'text' => array('__call', 'text', array('type' => 'text')),
98
			'date' => array('__call', 'iso8601', array('type' => 'date')),
99
			'datetime' => array('__call', 'iso8601', array('type' => 'datetime')),
100
			'timestamp' => array('__call', 'iso8601', array('type' => 'timestamp')),
101
			'time' => array('__call', 'iso8601', array('type' => 'time')),
102
		);
103
	}
104
105
	/**
106
	 * A format data provider for resulting to null cases
107
	 *
108
	 * @return array Formats
109
	 */
110
	public function specialFormatProvider() {
111
		return array(
112
			'null' => array(null),
113
			'binary' => array(
114
				array(
115
					'type' => 'binary'
116
				)
117
			),
118
		);
119
	}
120
}
121