Repository navigation
Expand file tree
/
Copy pathGeneralModelTest.php
More file actions
142 lines (118 loc) · 3.46 KB
/
Copy pathGeneralModelTest.php
File metadata and controls
142 lines (118 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace CodeIgniter\Models;
use BadMethodCallException;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Model;
use CodeIgniter\Test\CIUnitTestCase;
use Tests\Support\Models\JobModel;
use Tests\Support\Models\UserModel;
/**
* GeneralModelTest should be used when only testing Model's
* features without requiring a database connection.
*/
final class GeneralModelTest extends CIUnitTestCase
{
/**
* Current instance of the Model.
*
* @var Model
*/
private $model;
protected function setUp(): void
{
parent::setUp();
}
protected function tearDown(): void
{
parent::tearDown();
$this->resetServices();
}
/**
* Create an instance of Model for use in testing.
*
* @param string $modelName
* @param BaseConnection|null $db
*
* @return Model
*/
private function createModel(string $modelName, BaseConnection $db = null): Model
{
$this->model = new $modelName($db);
return $this->model;
}
public function testGetModelDetails(): void
{
$this->createModel(JobModel::class);
$this->assertSame('job', $this->model->table);
$this->assertSame('id', $this->model->primaryKey);
$this->assertSame('object', $this->model->returnType);
$this->assertNull($this->model->DBGroup);
}
public function testMagicGetters(): void
{
$this->createModel(UserModel::class);
$this->assertTrue(isset($this->model->table));
$this->assertSame('user', $this->model->table);
$this->assertFalse(isset($this->model->foobar));
$this->assertNull($this->model->foobar);
$this->model->flavor = 'chocolate';
$this->assertTrue(isset($this->model->flavor));
$this->assertSame('chocolate', $this->model->flavor);
// from DB
$this->assertTrue(isset($this->model->DBPrefix));
$this->assertSame('utf8', $this->model->charset);
// from Builder
$this->assertTrue(isset($this->model->QBNoEscape));
$this->assertIsArray($this->model->QBNoEscape);
}
public function testUndefinedModelMethod(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Call to undefined method Tests\Support\Models\UserModel::undefinedMethodCall');
$this->createModel(UserModel::class)->undefinedMethodCall();
}
public function testSetAllowedFields(): void
{
$allowed1 = [
'id',
'created_at',
];
$allowed2 = [
'id',
'updated_at',
];
$model = new class extends Model
{
protected $allowedFields = [
'id',
'created_at',
];
};
$this->assertSame($allowed1, $this->getPrivateProperty($model, 'allowedFields'));
$model->setAllowedFields($allowed2);
$this->assertSame($allowed2, $this->getPrivateProperty($model, 'allowedFields'));
}
public function testBuilderUsesModelTable(): void
{
$builder = $this->createModel(UserModel::class)->builder();
$this->assertSame('user', $builder->getTable());
}
public function testBuilderRespectsTableParameter(): void
{
$this->createModel(UserModel::class);
$builder1 = $this->model->builder('jobs');
$builder2 = $this->model->builder();
$this->assertSame('jobs', $builder1->getTable());
$this->assertSame('user', $builder2->getTable());
}
public function testBuilderWithParameterIgnoresShared(): void
{
$this->createModel(UserModel::class);
$builder1 = $this->model->builder();
$builder2 = $this->model->builder('jobs');
$builder3 = $this->model->builder();
$this->assertSame('user', $builder1->getTable());
$this->assertSame('jobs', $builder2->getTable());
$this->assertSame('user', $builder3->getTable());
}
}