Completed
Push — develop ( 3f7eb4...5c5c48 )
by Oyebanji Jacob
04:34 queued 02:18
created

testUpdateChangesTheValueOfObjectInDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
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 View Code Duplication
    public function testGetReturnNullWhenIdNotFoundInDatabase()
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...
62
    {
63
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
64
        $this->sqlStatement->shouldReceive('setFetchMode');
65
        $this->sqlStatement->shouldReceive('execute');
66
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
67
68
        $this->assertEquals(null, $this->model->get(1));
69
    }
70
71
    public function testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase()
72
    {
73
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
74
        $this->sqlStatement->shouldReceive('execute');
75
        $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass(), new stdClass()]);
76
        $this->assertContainsOnlyInstancesOf('stdClass', $this->model->all());
77
    }
78
79
    public function testUpdateChangesTheValueOfObjectInDatabase()
80
    {
81
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
82
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
83
        $this->sqlStatement->shouldReceive('execute');
84
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
85
86
        $this->assertEquals(1, $this->model->update());
87
    }
88
89 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...
90
    {
91
        $this->model->setProperties(['name' => 'pyjac', 'age' => '419']);
92
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
93
        $this->sqlStatement->shouldReceive('execute');
94
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
95
96
        $this->assertEquals(1, $this->model->create());
97
    }
98
99
    public function testSaveShouldCreateNewModelInDatabaseWhenIdNotPresent()
100
    {
101
        $this->model->setProperties(['name' => 'pyjac', 'age' => '419']);
102
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
103
        $this->sqlStatement->shouldReceive('execute');
104
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
105
        $this->assertEquals(1, $this->model->save());
106
    }
107
108
    public function testSaveShouldUpdateModelInDatabaseIfIdIsPresent()
109
    {
110
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
111
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
112
        $this->sqlStatement->shouldReceive('execute');
113
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
114
        $this->assertEquals(1, $this->model->save());
115
    }
116
117
    public function testGetPropertiesReturnsExpectedArrayValues()
118
    {
119
        $values = ['id' => 2, 'name' => 'pyjac', 'age' => '419'];
120
        $this->model->setProperties($values);
121
122
        $this->assertEquals($values, $this->model->getProperties());
123
    }
124
125 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...
126
    {
127
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
128
        $this->sqlStatement->shouldReceive('execute');
129
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
130
131
        $this->assertEquals(true, $this->model->delete(1));
132
    }
133
134 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...
135
    {
136
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
137
        $this->sqlStatement->shouldReceive('execute');
138
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
139
140
        $this->assertEquals(false, $this->model->delete(1));
141
    }
142
143
    public function testMagicMethodsReturnCorrectResult()
144
    {
145
        $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...
146
        $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...
147
        $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...
148
149
        $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...
150
        $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...
151
        $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...
152
    }
153
154
    public function testCreateModelWithNoParameter()
155
    {
156
        $model = $this->getMockForAbstractClass('Pyjac\ORM\Model');
157
        $this->assertInstanceOf(\Pyjac\ORM\Model::class, $model);
158
    }
159
}
160