Skip to content

Commit 0d0e492

Browse files
committed
mock and test model class
Signed-off-by: Derek Smart <derek@grindaga.com>
1 parent 959174c commit 0d0e492

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

tests/mocks/PipeUser.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace PHPRed\Models;
3+
4+
class PipeUser extends Model
5+
{
6+
public function __construct(\MysqliDb $mysql)
7+
{
8+
$this->model = 'PipeUser';
9+
$this->table = 'pipes_users';
10+
$this->primaryKey = 'id';
11+
$this->belongsTo = ['Pipe','User'];
12+
13+
parent::__construct($mysql);
14+
}
15+
}

tests/mocks/Service.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace PHPRed\Models;
3+
4+
class Service extends Model
5+
{
6+
public function __construct(\MysqliDb $mysql)
7+
{
8+
$this->model = 'Service';
9+
$this->table = 'services';
10+
$this->primaryKey = 'id';
11+
$this->foreignKey = 'service_id';
12+
$this->fields = ['id','display_name','class_name'];
13+
$this->hasAndBelongsToMany = ['Pipe', 'User'];
14+
15+
parent::__construct($mysql);
16+
}
17+
}

tests/mocks/User.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace PHPRed\Models;
3+
4+
class User extends Model
5+
{
6+
public function __construct(\MysqliDb $mysql)
7+
{
8+
$this->model = 'User';
9+
$this->table = 'users';
10+
$this->primaryKey = 'id';
11+
$this->foreignKey = 'user_id';
12+
$this->fields = ['id','email'];
13+
$this->requiredFields = ['email'];
14+
$this->uniqueFields = ['email'];
15+
$this->hasMany = ['PipeUser'];
16+
$this->hasAndBelongsToMany = ['Service'];
17+
18+
parent::__construct($mysql);
19+
}
20+
}

tests/unit/PHPRed/ModelTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ModelTest extends \PHPUnit\Framework\TestCase
1616
private $services;
1717
private $pipesServices;
1818
private $pipesUsers;
19+
private $users;
1920

2021
public function setup()
2122
{
@@ -41,18 +42,23 @@ public function setup()
4142
'pipe_id' => 1,
4243
'service_id' => 1
4344
];
44-
$this->pipesUsers = [
45+
$this->pipesUsers = [[
4546
'id' => 1,
4647
'pipe_id' => 1,
4748
'user_id' => 1,
4849
'last_run' => '2017-07-04 10:20:24',
4950
'run_count' => 3,
5051
'tracking_condition' => '',
5152
'enabled' => 1
52-
];
53+
]];
54+
$this->users = [[
55+
'id' => 1,
56+
'email' => 'derek@grindaga.com'
57+
]];
5358
$this->prophet = new Prophecy\Prophet;
5459
$this->mysql = $this->prophet->prophesize("\MysqliDb");
5560
$this->pipe = new Pipe($this->mysql->reveal());
61+
$this->pipeUser = new PipeUser($this->mysql->reveal());
5662
}
5763

5864
public function testCanInstantiate()
@@ -219,4 +225,17 @@ public function testFieldsAreNotUnique()
219225
$this->expectExceptionMessage('name must be unique for Pipe.');
220226
$this->pipe->insert($pipe);
221227
}
228+
229+
public function testBelongsTo()
230+
{
231+
$this->mysql->where(\Prophecy\Argument::type('string'), \Prophecy\Argument::type('int'))->willReturn(true);
232+
$this->mysql->where(\Prophecy\Argument::type('string'), \Prophecy\Argument::type('string'))->willReturn(true);
233+
$this->mysql->join(\Prophecy\Argument::type('string'), \Prophecy\Argument::type('string'), \Prophecy\Argument::type('string'))->willReturn(true);
234+
$this->mysql->get(\Prophecy\Argument::exact('pipes Pipe'))->willReturn($this->pipes);
235+
$this->mysql->get(\Prophecy\Argument::exact('users User'))->willReturn($this->users);
236+
$this->mysql->get(\Prophecy\Argument::exact('pipes_users PipeUser'))->willReturn($this->pipesUsers);
237+
238+
$pipeUsers = $this->pipeUser->getAll();
239+
$this->assertEquals(1, $pipeUsers[0]['pipe_id']);
240+
}
222241
}

0 commit comments

Comments
 (0)