Skip to content

Commit 35d00b9

Browse files
committed
Get Model basics working with CI4-beta2
1 parent 97186b6 commit 35d00b9

6 files changed

Lines changed: 141 additions & 0 deletions

File tree

app/Config/Autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function __construct()
5353
'Config' => APPPATH . 'Config',
5454
APP_NAMESPACE => APPPATH, // For custom namespace
5555
'App' => APPPATH, // To ensure filters, etc still found,
56+
'Vulcan' => ROOTPATH . '../vulcan/',
5657
];
5758

5859
/**

app/Controllers/Home.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?php namespace App\Controllers;
22

33
use CodeIgniter\Controller;
4+
use App\Models\UserModel;
45

56
class Home extends BaseController
67
{
78
public function index()
89
{
10+
$model = new UserModel();
11+
dd($model->findAll());
12+
913
return view('welcome_message');
1014
}
1115

app/Models/UserModel.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php namespace App\Models;
2+
3+
use CodeIgniter\Model;
4+
5+
/**
6+
* UserModel Model
7+
*/
8+
class UserModel extends Model
9+
{
10+
protected $table = 'users';
11+
protected $primaryKey = 'id';
12+
13+
protected $allowedFields = [
14+
'name',
15+
'email',
16+
];
17+
18+
protected $returnType = 'array';
19+
protected $useSoftDeletes = false;
20+
21+
protected $useTimestamps = true;
22+
protected $createdField = 'created_at';
23+
protected $updatedField = 'updated_at';
24+
protected $dateFormat = 'datetime';
25+
26+
protected $validationRules = [
27+
'id' => 'integer|max_length[11]',
28+
'name' => 'alpha_numeric_spaces|max_length[255]',
29+
'email' => 'alpha_numeric_spaces|max_length[255]',
30+
31+
];
32+
protected $validationMessages = [];
33+
protected $skipValidation = false;
34+
}

tests/ControllerTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php namespace Test;
2+
3+
use App\Controllers\Home;
4+
use CodeIgniter\HTTP\IncomingRequest;
5+
use CodeIgniter\HTTP\Response;
6+
use CodeIgniter\Test\CIUnitTestCase;
7+
use CodeIgniter\Test\ControllerTester;
8+
9+
class ControllerTest extends CIUnitTestCase
10+
{
11+
use ControllerTester;
12+
13+
public function testHomePage()
14+
{
15+
$result = $this->withUri('http://localhost:8080')
16+
->controller(Home::class)
17+
->execute('index');
18+
19+
// Success Status
20+
$this->assertTrue($result->isOK());
21+
22+
// Not a Redirect
23+
$this->assertFalse($result->isRedirect());
24+
25+
$request = $result->request();
26+
$this->assertInstanceOf(IncomingRequest::class, $request);
27+
28+
$response = $result->response();
29+
$this->assertInstanceOf(Response::class, $response);
30+
31+
$body = $result->getBody();
32+
$this->assertNotEmpty($body);
33+
34+
// Check the content of the page
35+
$this->assertTrue($result->see('CodeIgniter'));
36+
37+
$this->assertTrue($result->dontSee('Laravel'));
38+
39+
$this->assertTrue($result->seeElement('.logo'));
40+
41+
$this->assertTrue($result->seeLink('User Guide'));
42+
}
43+
}

tests/DatabaseTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php namespace Test;
2+
3+
use CodeIgniter\Test\CIDatabaseTestCase;
4+
5+
class DatabaseTest extends CIDatabaseTestCase
6+
{
7+
public function testSomeThings()
8+
{
9+
$this->hasInDatabase('user', [
10+
'name' => 'Foo Bar',
11+
'email' => 'foobar@example.com',
12+
'country' => 'US',
13+
]);
14+
15+
$this->seeInDatabase('user', [
16+
'name' => 'Foo Bar',
17+
]);
18+
19+
$this->dontSeeInDatabase('user', [
20+
'name' => 'Fannie Farkle',
21+
]);
22+
23+
$name = $this->grabFromDatabase('user', 'name', ['email' => 'foobar@example.com']);
24+
$this->assertEquals('Foo Bar', $name);
25+
26+
$this->seeNumRecords(1, 'user', ['name' => 'Foo Bar']);
27+
}
28+
}

tests/HomeTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php namespace Test;
2+
3+
use CodeIgniter\Test\FeatureTestCase;
4+
5+
class HomeTest extends FeatureTestCase
6+
{
7+
public function testHomePage()
8+
{
9+
$result = $this->skipEvents()->call('get', '/');
10+
11+
$result->assertOk();
12+
13+
$result->assertStatus(200);
14+
15+
$result->assertSee('CodeIgniter');
16+
$result->assertDontSee('Symfony');
17+
}
18+
19+
public function testJson()
20+
{
21+
$result = $this->get('api');
22+
23+
$json = $result->getJSON();
24+
25+
$result->assertJSONFragment(['foo' => 'bar']);
26+
$result->assertJSONExact([
27+
'foo' => 'bar',
28+
'bar' => 'none',
29+
]);
30+
}
31+
}

0 commit comments

Comments
 (0)