Completed
Push — develop ( 90d6e0...88778e )
by Oyebanji Jacob
04:34 queued 02:21
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
    protected $model;
15
16
    protected $sqlStatement;
17
18
    public function setUp()
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
    /**
57
     * @expectedException Pyjac\ORM\Exception\ModelNotFoundException
58
     */
59 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...
60
    {
61
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
62
        $this->sqlStatement->shouldReceive('setFetchMode');
63
        $this->sqlStatement->shouldReceive('execute');
64
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
65
66
        $this->assertInstanceOf('stdClass', $this->model->get(1));
67
    }
68
69
    public function testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase()
70
    {
71
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
72
        $this->sqlStatement->shouldReceive('execute');
73
        $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass(), new stdClass()]);
74
        $this->assertContainsOnlyInstancesOf('stdClass', $this->model->all());
75
    }
76
77
    public function testUpdateChangesTheValueOfObjectInDatabase()
78
    {
79
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
80
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
81
        $this->sqlStatement->shouldReceive('execute');
82
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
83
84
        $this->assertEquals(1, $this->model->update());
85
    }
86
87
    public function testCreateObjectInDatabase()
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('rowCount')->once()->andReturn(1);
93
94
        $this->assertEquals(1, $this->model->create());
95
    }
96
97 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...
98
    {
99
        $this->model->setProperties(['name' => 'pyjac', 'age' => '419']);
100
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
101
        $this->sqlStatement->shouldReceive('execute');
102
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
103
        $this->assertEquals(1, $this->model->save());
104
    }
105
106
    public function testSaveShouldUpdateModelInDatabaseIfIdIsPresent()
107
    {
108
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
109
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
110
        $this->sqlStatement->shouldReceive('execute');
111
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
112
        $this->assertEquals(1, $this->model->save());
113
    }
114
115
    public function testGetPropertiesReturnsExpectedArrayValues()
116
    {
117
        $values = ['id' => 2, 'name' => 'pyjac', 'age' => '419'];
118
        $this->model->setProperties($values);
119
120
        $this->assertEquals($values, $this->model->getProperties());
121
    }
122
123 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...
124
    {
125
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
126
        $this->sqlStatement->shouldReceive('execute');
127
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
128
129
        $this->assertEquals(true, $this->model->delete(1));
130
    }
131
132 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...
133
    {
134
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
135
        $this->sqlStatement->shouldReceive('execute');
136
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
137
138
        $this->assertEquals(false, $this->model->delete(1));
139
    }
140
141
    public function testMagicMethodsReturnCorrectResult()
142
    {
143
        $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...
144
        $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...
145
        $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...
146
147
        $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...
148
        $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...
149
        $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...
150
    }
151
152
    public function testCreateModelWithNoParameter()
153
    {
154
        $model = $this->getMockForAbstractClass('Pyjac\ORM\Model');
155
        $this->assertInstanceOf(\Pyjac\ORM\Model::class, $model);
156
    }
157
}
158