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

testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
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
40
    public function testGetTableNameReturnsCorrectTableName()
41
    {
42
        $this->databaseConnection->shouldReceive('createConnection')->with('sqlite::memory:')->once()->andReturn('');
43
        $this->model->expects($this->any())
44
             ->method('getTableName')
45
             ->will($this->returnValue(strtolower(get_class($this->model).'s')));
46
47
        $this->assertEquals($this->model->getTableName(), strtolower(get_class($this->model).'s'));
48
    }
49
50
    public function testGetReturnsAnObjectWhenIdIsFoundInDatabase()
51
    {
52
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
53
        $this->sqlStatement->shouldReceive('setFetchMode');
54
        $this->sqlStatement->shouldReceive('execute');
55
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
56
        $this->sqlStatement->shouldReceive('fetch')->once()->andReturn(new stdClass());
57
58
        $this->assertInstanceOf('stdClass', $this->model->get(1));
59
    }
60
61
    /**
62
     * @expectedException Pyjac\ORM\Exception\ModelNotFoundException
63
     */
64
    public function testGetThrowsModelNotFoundExceptionWhenIdNotFoundInDatabase()
65
    {
66
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
67
        $this->sqlStatement->shouldReceive('setFetchMode');
68
        $this->sqlStatement->shouldReceive('execute');
69
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
70
71
        $this->assertInstanceOf('stdClass', $this->model->get(1));
72
    }
73
74
    public function testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase()
75
    {
76
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
77
        $this->sqlStatement->shouldReceive('execute');
78
        $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass(), new stdClass()]);
79
        $this->assertContainsOnlyInstancesOf('stdClass', $this->model->all());
80
    }
81
82 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...
83
    {
84
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
85
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
86
        $this->sqlStatement->shouldReceive('execute');
87
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
88
89
        $this->assertEquals(1, $this->model->update());
90
    }
91
92 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...
93
    {
94
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
95
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
96
        $this->sqlStatement->shouldReceive('execute');
97
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
98
99
        $this->assertEquals(1, $this->model->create());
100
    }
101
102 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...
103
    {
104
        $this->model->setProperties(['name' => 'pyjac', 'age' => '419']);
105
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
106
        $this->sqlStatement->shouldReceive('execute');
107
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
108
        $this->assertEquals(1, $this->model->save());
109
    }
110
111 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...
112
    {
113
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
114
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
115
        $this->sqlStatement->shouldReceive('execute');
116
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
117
        $this->assertEquals(1, $this->model->save());
118
    }
119
120
    public function testGetPropertiesReturnsExpectedArrayValues()
121
    {
122
        $values = ['id' => 2, 'name' => 'pyjac', 'age' => '419'];
123
        $this->model->setProperties($values);
124
125
        $this->assertEquals($values, $this->model->getProperties());
126
    }
127
128
    public function testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase()
129
    {
130
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
131
        $this->sqlStatement->shouldReceive('execute');
132
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
133
134
        $this->assertEquals(true, $this->model->delete(1));
135
    }
136
137
    public function testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase()
138
    {
139
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
140
        $this->sqlStatement->shouldReceive('execute');
141
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
142
143
        $this->assertEquals(false, $this->model->delete(1));
144
    }
145
146
    public function testMagicMethodsReturnCorrectResult()
147
    {
148
        $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->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->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
        $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...
153
        $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...
154
        $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...
155
    }
156
157
    public function testCreateModelWithNoParameter()
158
    {
159
        $model = $this->getMockForAbstractClass('Pyjac\ORM\Model');
160
        $this->assertInstanceOf(\Pyjac\ORM\Model::class, $model);
161
    }
162
}
163