Issues (16)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

test/ModelTest.php (11 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 View Code Duplication
    public function testGetReturnNullWhenIdNotFoundInDatabase()
0 ignored issues
show
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...
61
    {
62
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
63
        $this->sqlStatement->shouldReceive('setFetchMode');
64
        $this->sqlStatement->shouldReceive('execute');
65
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
66
67
        $this->assertEquals(null, $this->model->get(1));
68
    }
69
70
    public function testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase()
71
    {
72
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
73
        $this->sqlStatement->shouldReceive('execute');
74
        $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass(), new stdClass()]);
75
        $this->assertContainsOnlyInstancesOf('stdClass', $this->model->all());
76
    }
77
78
    public function testUpdateChangesTheValueOfObjectInDatabase()
79
    {
80
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
81
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
82
        $this->sqlStatement->shouldReceive('execute');
83
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
84
85
        $this->assertEquals(1, $this->model->update());
86
    }
87
88 View Code Duplication
    public function testCreateObjectInDatabase()
0 ignored issues
show
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...
89
    {
90
        $this->model->setProperties(['name' => 'pyjac', 'age' => '419']);
91
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
92
        $this->sqlStatement->shouldReceive('execute');
93
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
94
95
        $this->assertEquals(1, $this->model->create());
96
    }
97
98
    public function testSaveShouldCreateNewModelInDatabaseWhenIdNotPresent()
99
    {
100
        $this->model->setProperties(['name' => 'pyjac', 'age' => '419']);
101
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
102
        $this->sqlStatement->shouldReceive('execute');
103
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
104
        $this->assertEquals(1, $this->model->save());
105
    }
106
107
    public function testSaveShouldUpdateModelInDatabaseIfIdIsPresent()
108
    {
109
        $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']);
110
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
111
        $this->sqlStatement->shouldReceive('execute');
112
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
113
        $this->assertEquals(1, $this->model->save());
114
    }
115
116
    public function testGetPropertiesReturnsExpectedArrayValues()
117
    {
118
        $values = ['id' => 2, 'name' => 'pyjac', 'age' => '419'];
119
        $this->model->setProperties($values);
120
121
        $this->assertEquals($values, $this->model->getProperties());
122
    }
123
124 View Code Duplication
    public function testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase()
0 ignored issues
show
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...
125
    {
126
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
127
        $this->sqlStatement->shouldReceive('execute');
128
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1);
129
130
        $this->assertEquals(true, $this->model->delete(1));
131
    }
132
133 View Code Duplication
    public function testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase()
0 ignored issues
show
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...
134
    {
135
        $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement);
136
        $this->sqlStatement->shouldReceive('execute');
137
        $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0);
138
139
        $this->assertEquals(false, $this->model->delete(1));
140
    }
141
142
    public function testMagicMethodsReturnCorrectResult()
143
    {
144
        $this->model->id = '10';
0 ignored issues
show
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
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
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
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
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
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
        $this->assertEquals($this->model->jac, null);
0 ignored issues
show
Accessing jac 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