Completed
Push — develop ( 7bc0c1...25f6d3 )
by Oyebanji Jacob
02:07
created

ModelTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 148
Duplicated Lines 22.3 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 5
Bugs 1 Features 5
Metric Value
wmc 13
c 5
b 1
f 5
lcom 1
cbo 6
dl 33
loc 148
rs 10

13 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() 0 9 1
A testCreateObjectInDatabase() 0 9 1
A testSaveShouldCreateNewModelInDatabaseWhenIdNotPresent() 8 8 1
A testSaveShouldUpdateModelInDatabaseIfIdIsPresent() 0 8 1
A testGetPropertiesReturnsExpectedArrayValues() 0 7 1
A testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase() 8 8 1
A testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase() 8 8 1
A testMagicMethodsReturnCorrectResult() 0 12 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
    public function testUpdateChangesTheValueOfObjectInDatabase()
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('rowCount')->once()->andReturn(1);
82
    
83
    	$this->assertEquals(1, $this->model->update());
84
    }
85
86
    public function testCreateObjectInDatabase()
87
    {
88
    	$this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
89
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
90
    	$this->sqlStatement->shouldReceive('execute');
91
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
92
    
93
    	$this->assertEquals(1, $this->model->create());
94
    }
95
96 View Code Duplication
    public function testSaveShouldCreateNewModelInDatabaseWhenIdNotPresent()
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...
97
    {
98
    	$this->model->setProperties(['name' => 'pyjac', 'age' => '419']);
99
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
100
    	$this->sqlStatement->shouldReceive('execute');
101
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
102
    	$this->assertEquals(1, $this->model->save());
103
    }
104
105
    public function testSaveShouldUpdateModelInDatabaseIfIdIsPresent()
106
    {
107
    	$this->model->setProperties(['id' => 2,'name' => 'pyjac', 'age' => '419']);
108
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
109
    	$this->sqlStatement->shouldReceive('execute');
110
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
111
    	$this->assertEquals(1, $this->model->save());
112
    }
113
114
    public function testGetPropertiesReturnsExpectedArrayValues()
115
    {
116
    	$values = ['id' => 2, 'name' => 'pyjac', 'age' => '419'];
117
    	$this->model->setProperties($values);
118
    	
119
    	$this->assertEquals($values, $this->model->getProperties());
120
    }
121
122 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...
123
    {
124
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
125
    	$this->sqlStatement->shouldReceive('execute');
126
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
127
    	
128
    	$this->assertEquals(true, $this->model->delete(1));
129
    }
130
131 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...
132
    {
133
    	$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
134
    	$this->sqlStatement->shouldReceive('execute');
135
    	$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
136
    	
137
    	$this->assertEquals(false, $this->model->delete(1));
138
    }
139
140
    public function testMagicMethodsReturnCorrectResult() {
141
	    /*$this->model->setReturnValue('__get', 'Pyjac', array('name'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
142
	    $this->model->setReturnValue('__get', '10', array('id'));
143
	    $this->model->setReturnValue('__get', '60', array('age'));*/
144
	    $this->model->id = '10';
0 ignored issues
show
Bug introduced by
Accessing id on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
145
	    $this->model->name = 'Pyjac';
0 ignored issues
show
Bug introduced by
Accessing name on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
146
	    $this->model->age = '60';
0 ignored issues
show
Bug introduced by
Accessing age on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
147
148
	    $this->assertEquals($this->model->name, 'Pyjac');
0 ignored issues
show
Bug introduced by
Accessing name on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
149
	    $this->assertEquals($this->model->id , '10');
0 ignored issues
show
Bug introduced by
Accessing id on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
150
	    $this->assertEquals($this->model->age, '60');
0 ignored issues
show
Bug introduced by
Accessing age on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
151
	}
152
153
}