ShellModelTruncatorTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 71
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 1
A testConstruct() 0 4 1
A testTruncateModels() 0 8 1
1
<?php
2
3
App::uses('ShellDispatcher', 'Console');
4
App::uses('ConsoleOutput', 'Console');
5
App::uses('ConsoleInput', 'Console');
6
App::uses('Shell', 'Console');
7
App::uses('ShellModelTruncator', 'FakeSeeder.Lib');
8
9
/**
10
 * ShellModelTruncator Test
11
 *
12
 * @coversDefaultClass ShellModelTruncator
13
 */
14
class ShellModelTruncatorTest extends CakeTestCase {
15
16
	/**
17
	 * The fixtures
18
	 *
19
	 * @var array
20
	 */
21
	public $fixtures = array(
22
		'plugin.fake_seeder.apple',
23
		'plugin.fake_seeder.banana',
24
		'plugin.fake_seeder.pear',
25
	);
26
27
	/**
28
	 * The shell object
29
	 *
30
	 * @var null|Shell
31
	 */
32
	protected $_shell = null;
33
34
	/**
35
	 * The object under test
36
	 *
37
	 * @var null|ShellModelTruncator
38
	 */
39
	protected $_truncator = null;
40
41
	/**
42
	 * Setup the object under test
43
	 *
44
	 * @return void
45
	 */
46
	public function setUp() {
47
		parent::setUp();
48
49
		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
50
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
51
		$this->_shell = $this->getMock('Shell',
52
			array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop'),
53
			array($out, $out, $in)
54
		);
55
56
		$this->_truncator = new ShellModelTruncator($this->_shell);
57
	}
58
59
	/**
60
	 * Test the constructor
61
	 *
62
	 * @return void
63
	 * @covers ::__construct
64
	 */
65
	public function testConstruct() {
66
		$truncator = new ShellModelTruncator($this->_shell);
0 ignored issues
show
Bug introduced by
It seems like $this->_shell 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...
67
		$this->assertAttributeInstanceOf('Shell', '_shell', $truncator);
68
	}
69
70
	/**
71
	 * Tests the truncateModels function
72
	 *
73
	 * @return void
74
	 * @covers ::truncateModels
75
	 */
76
	public function testTruncateModels() {
77
		$this->_shell->expects($this->at(0))->method('out')->with($this->equalTo('Truncate model Apple...'));
78
		$this->_shell->expects($this->at(1))->method('out')->with($this->equalTo('Truncate model Banana...'));
79
		$this->_shell->expects($this->at(2))->method('out')->with($this->equalTo('Truncate model Pear...'));
80
81
		$models = array('Apple', 'Banana', 'Pear');
82
		$this->_truncator->truncateModels($models);
83
	}
84
}
85