Completed
Push — develop ( aba1ed...ebe2d3 )
by Oyebanji Jacob
02:15
created

ModelTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 40.18 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 1 Features 3
Metric Value
wmc 9
c 3
b 1
f 3
lcom 1
cbo 6
dl 45
loc 112
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 17 1
A testGetTableNameReturnsCorrectTableName() 0 9 1
A testGetReturnsAnObjectWhenIdIsFoundInDatabase() 0 10 1
A testGetThrowsModelNotFoundExceptionWhenIdNotFoundInDatabase() 9 9 1
A testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase() 0 7 1
A testUpdateChangesTheValueOfObjectInDatabase() 10 10 1
A testCreateObjectInDatabase() 10 10 1
A testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase() 8 8 1
A testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php 
2
3
use \Mockery as m;
4
use Pyjac\ORM\Model;
5
6
class ModelTest extends PHPUnit_Framework_TestCase
7
{
8
	/**
9
	 * Instance of DatabaseConnection used in test.
10
	 */
11
	protected $databaseConnection;
12
13
	protected $model;
14
15
	protected $sqlStatement;
16
17
	public function setUp(){
18
		
19
20
		$databaseConnectionStringFactory =
21
                        m::mock('Pyjac\ORM\DatabaseConnectionStringFactoryInterface');
22
23
        $databaseConnectionStringFactory->shouldReceive('createDatabaseSourceString')
24
                                             ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:');
25
26
        $this->databaseConnection = m::mock('Pyjac\ORM\DatabaseConnection');
27
28
        $this->sqlStatement = m::mock('\PDOStatement');
29
30
        /*$this->databaseConnection = m::mock('Pyjac\ORM\DatabaseConnection[getInstance,createConnection]',array($databaseConnectionStringFactory));*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
        $this->model =  $this->getMockForAbstractClass('Pyjac\ORM\Model',[$this->databaseConnection]);
32
        //= new DatabaseConnection($databaseConnectionStringFactory);
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
	}
34
35
	public function testGetTableNameReturnsCorrectTableName()
36
    {
37
    	$this->databaseConnection->shouldReceive('createConnection')->with('sqlite::memory:')->once()->andReturn("");
38
        $this->model->expects($this->any())
39
             ->method('getTableName')
40
             ->will($this->returnValue(strtolower(get_class($this->model).'s')));
41
42
        $this->assertEquals($this->model->getTableName(), strtolower(get_class($this->model).'s'));
43
    }
44
45
    public function testGetReturnsAnObjectWhenIdIsFoundInDatabase()
46
    {
47
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
48
    	$this->sqlStatement->shouldReceive('setFetchMode');
49
    	$this->sqlStatement->shouldReceive('execute');
50
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
51
    	$this->sqlStatement->shouldReceive('fetch')->once()->andReturn(new stdClass);
52
53
    	$this->assertInstanceOf('stdClass', $this->model->get(1));
54
    }
55
    /**
56
     * @expectedException Pyjac\ORM\Exception\ModelNotFoundException
57
     */
58 View Code Duplication
    public function testGetThrowsModelNotFoundExceptionWhenIdNotFoundInDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
61
    	$this->sqlStatement->shouldReceive('setFetchMode');
62
    	$this->sqlStatement->shouldReceive('execute');
63
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
64
    	
65
    	$this->assertInstanceOf('stdClass', $this->model->get(1));
66
    }
67
68
    public function testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase()
69
    {
70
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
71
    	$this->sqlStatement->shouldReceive('execute');
72
    	$this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass,new stdClass]);
73
    	$this->assertContainsOnlyInstancesOf('stdClass', $this->model->all());
74
    }
75
76 View Code Duplication
    public function testUpdateChangesTheValueOfObjectInDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
    	$this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
79
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
80
    	$this->sqlStatement->shouldReceive('execute');
81
    	$this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass,new stdClass]);
82
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
83
    
84
    	$this->assertEquals(1, $this->model->update());
85
    }
86
87 View Code Duplication
    public function testCreateObjectInDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
    	$this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
90
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
91
    	$this->sqlStatement->shouldReceive('execute');
92
    	$this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass,new stdClass]);
93
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
94
    
95
    	$this->assertEquals(1, $this->model->create());
96
    }
97
98
99 View Code Duplication
    public function testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
102
    	$this->sqlStatement->shouldReceive('execute');
103
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
104
    	
105
    	$this->assertEquals(true, $this->model->delete(1));
106
    }
107
108 View Code Duplication
    public function testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
111
    	$this->sqlStatement->shouldReceive('execute');
112
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
113
    	
114
    	$this->assertEquals(false, $this->model->delete(1));
115
    }
116
117
}