Completed
Pull Request — master (#4)
by Oyebanji Jacob
02:33
created

ModelTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 155
Duplicated Lines 21.94 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 8
Bugs 3 Features 4
Metric Value
wmc 14
c 8
b 3
f 4
lcom 1
cbo 6
dl 34
loc 155
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
A testGetTableNameReturnsCorrectTableName() 0 9 1
A testGetReturnsAnObjectWhenIdIsFoundInDatabase() 0 10 1
A testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase() 0 7 1
A testSaveShouldCreateNewModelInDatabaseWhenIdNotPresent() 8 8 1
A testGetPropertiesReturnsExpectedArrayValues() 0 7 1
A testMagicMethodsReturnCorrectResult() 0 10 1
A testCreateModelWithNoParameter() 0 5 1
A testGetThrowsModelNotFoundExceptionWhenIdNotFoundInDatabase() 0 9 1
A testUpdateChangesTheValueOfObjectInDatabase() 9 9 1
A testCreateObjectInDatabase() 9 9 1
A testSaveShouldUpdateModelInDatabaseIfIdIsPresent() 8 8 1
A testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase() 0 8 1
A testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase() 0 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
4
use \Mockery as m;
5
use Pyjac\ORM\Model;
6
7
class ModelTest extends PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * Instance of DatabaseConnection used in test.
11
     */
12
    protected $databaseConnection;
13
14
    /**
15
     * Instance of Model used in test.
16
     */
17
    protected $model;
18
19
    /**
20
     * Instance of SqlStatement used in test.
21
     */
22
    protected $sqlStatement;
23
24
    public function setUp()
25
    {
26
        $databaseConnectionStringFactory =
27
                        m::mock('Pyjac\ORM\DatabaseConnectionStringFactoryInterface');
28
29
        $databaseConnectionStringFactory->shouldReceive('createDatabaseSourceString')
30
                                             ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:');
31
32
        $this->databaseConnection = m::mock('Pyjac\ORM\DatabaseConnection');
33
34
        $this->sqlStatement = m::mock('\PDOStatement');
35
36
        $this->model = $this->getMockForAbstractClass('Pyjac\ORM\Model', [$this->databaseConnection]);
37
    }
38
39
    public function testGetTableNameReturnsCorrectTableName()
40
    {
41
        $this->databaseConnection->shouldReceive('createConnection')->with('sqlite::memory:')->once()->andReturn('');
42
        $this->model->expects($this->any())
43
             ->method('getTableName')
44
             ->will($this->returnValue(strtolower(get_class($this->model).'s')));
45
46
        $this->assertEquals($this->model->getTableName(), strtolower(get_class($this->model).'s'));
47
    }
48
49
    public function testGetReturnsAnObjectWhenIdIsFoundInDatabase()
50
    {
51
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
52
        $this->sqlStatement->shouldReceive('setFetchMode');
53
        $this->sqlStatement->shouldReceive('execute');
54
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
55
        $this->sqlStatement->shouldReceive('fetch')->once()->andReturn(new stdClass());
56
57
        $this->assertInstanceOf('stdClass', $this->model->get(1));
58
    }
59
60
    /**
61
     * @expectedException Pyjac\ORM\Exception\ModelNotFoundException
62
     */
63
    public function testGetThrowsModelNotFoundExceptionWhenIdNotFoundInDatabase()
64
    {
65
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
66
        $this->sqlStatement->shouldReceive('setFetchMode');
67
        $this->sqlStatement->shouldReceive('execute');
68
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
69
70
        $this->assertInstanceOf('stdClass', $this->model->get(1));
71
    }
72
73
    public function testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase()
74
    {
75
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
76
        $this->sqlStatement->shouldReceive('execute');
77
        $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass(), new stdClass()]);
78
        $this->assertContainsOnlyInstancesOf('stdClass', $this->model->all());
79
    }
80
81 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...
82
    {
83
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
84
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
85
        $this->sqlStatement->shouldReceive('execute');
86
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
87
88
        $this->assertEquals(1, $this->model->update());
89
    }
90
91 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...
92
    {
93
        $this->model->setProperties(['name' => 'pyjac', 'age' => '419']);
94
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
95
        $this->sqlStatement->shouldReceive('execute');
96
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
97
98
        $this->assertEquals(1, $this->model->create());
99
    }
100
101 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...
102
    {
103
        $this->model->setProperties(['name' => 'pyjac', 'age' => '419']);
104
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
105
        $this->sqlStatement->shouldReceive('execute');
106
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
107
        $this->assertEquals(1, $this->model->save());
108
    }
109
110 View Code Duplication
    public function testSaveShouldUpdateModelInDatabaseIfIdIsPresent()
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...
111
    {
112
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
113
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
114
        $this->sqlStatement->shouldReceive('execute');
115
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
116
        $this->assertEquals(1, $this->model->save());
117
    }
118
119
    public function testGetPropertiesReturnsExpectedArrayValues()
120
    {
121
        $values = ['id' => 2, 'name' => 'pyjac', 'age' => '419'];
122
        $this->model->setProperties($values);
123
124
        $this->assertEquals($values, $this->model->getProperties());
125
    }
126
127
    public function testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase()
128
    {
129
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
130
        $this->sqlStatement->shouldReceive('execute');
131
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
132
133
        $this->assertEquals(true, $this->model->delete(1));
134
    }
135
136
    public function testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase()
137
    {
138
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
139
        $this->sqlStatement->shouldReceive('execute');
140
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
141
142
        $this->assertEquals(false, $this->model->delete(1));
143
    }
144
145
    public function testMagicMethodsReturnCorrectResult()
146
    {
147
        $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...
148
        $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->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...
150
151
        $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...
152
        $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...
153
        $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...
154
    }
155
156
    public function testCreateModelWithNoParameter()
157
    {
158
        $model = $this->getMockForAbstractClass('Pyjac\ORM\Model');
159
        $this->assertInstanceOf(\Pyjac\ORM\Model::class, $model);
160
    }
161
}
162