Skip to content

Commit

Permalink
Merge pull request #1943 from atishamte/model_test
Browse files Browse the repository at this point in the history
Model, Entity, Exception & Migration test cases
  • Loading branch information
lonnieezell authored Apr 19, 2019
2 parents e66c5ef + 70b0862 commit ebd203a
Show file tree
Hide file tree
Showing 15 changed files with 571 additions and 37 deletions.
1 change: 1 addition & 0 deletions system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

namespace CodeIgniter;

use CodeIgniter\Exceptions\EntityException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Exceptions\CastException;

Expand Down
26 changes: 13 additions & 13 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@ public function insert($data = null, bool $returnID = true)
$this->tempData = [];
}

if (empty($data))
{
throw DataException::forEmptyDataset('insert');
}

// If $data is using a custom class with public or protected
// properties representing the table elements, we need to grab
// them as an array.
Expand Down Expand Up @@ -668,11 +673,6 @@ public function insert($data = null, bool $returnID = true)

$data = $this->trigger('beforeInsert', ['data' => $data]);

if (empty($data))
{
throw DataException::forEmptyDataset('insert');
}

// Must use the set() method to ensure objects get converted to arrays
$result = $this->builder()
->set($data['data'], '', $escape)
Expand Down Expand Up @@ -749,6 +749,11 @@ public function update($id = null, $data = null): bool
$this->tempData = [];
}

if (empty($data))
{
throw DataException::forEmptyDataset('update');
}

// If $data is using a custom class with public or protected
// properties representing the table elements, we need to grab
// them as an array.
Expand Down Expand Up @@ -790,11 +795,6 @@ public function update($id = null, $data = null): bool

$data = $this->trigger('beforeUpdate', ['id' => $id, 'data' => $data]);

if (empty($data))
{
throw DataException::forEmptyDataset('update');
}

$builder = $this->builder();

if ($id)
Expand Down Expand Up @@ -956,9 +956,9 @@ public function onlyDeleted()
* @param null $data
* @param boolean $returnSQL
*
* @return boolean TRUE on success, FALSE on failure
* @return mixed
*/
public function replace($data = null, bool $returnSQL = false): bool
public function replace($data = null, bool $returnSQL = false)
{
// Validate data before saving.
if (! empty($data) && $this->skipValidation === false)
Expand Down Expand Up @@ -1317,7 +1317,7 @@ public function setValidationMessage(string $field, array $fieldMessages)
* Validate the data against the validation rules (or the validation group)
* specified in the class property, $validationRules.
*
* @param array $data
* @param array|object $data
*
* @return boolean
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public function up()
'constraint' => 1,
'default' => '0',
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
'updated_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('user', true);
Expand All @@ -50,9 +58,20 @@ public function up()
'type' => 'TEXT',
'null' => true,
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
'deleted' => [
'type' => 'TINYINT',
'constraint' => 1,
'default' => '0',
],
'created_at' => [
'type' => 'INTEGER',
'constraint' => 11,
'null' => true,
],
'updated_at' => [
'type' => 'INTEGER',
'constraint' => 11,
'null' => true,
],
]);
$this->forge->addKey('id', true);
Expand Down Expand Up @@ -85,6 +104,14 @@ public function up()
'type' => 'VARCHAR',
'constraint' => 40,
],
'created_at' => [
'type' => 'DATE',
'null' => true,
],
'updated_at' => [
'type' => 'DATE',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('empty', true);
Expand Down
4 changes: 3 additions & 1 deletion tests/_support/Models/EntityModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class EntityModel extends Model

protected $useSoftDeletes = false;

protected $dateFormat = 'datetime';
protected $dateFormat = 'int';

protected $deletedField = 'deleted';

protected $allowedFields = [
'name',
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Models/EventModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class EventModel extends Model

protected $useSoftDeletes = false;

protected $dateFormat = 'integer';
protected $dateFormat = 'datetime';

protected $allowedFields = [
'name',
Expand Down
6 changes: 5 additions & 1 deletion tests/_support/Models/JobModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ class JobModel extends Model

protected $useSoftDeletes = false;

protected $dateFormat = 'integer';
protected $dateFormat = 'int';

protected $allowedFields = [
'name',
'description',
];

public $name = '';

public $description = '';
}
2 changes: 1 addition & 1 deletion tests/_support/Models/SecondaryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SecondaryModel extends Model

protected $useSoftDeletes = false;

protected $dateFormat = 'integer';
protected $dateFormat = 'int';

protected $allowedFields = [
'key',
Expand Down
2 changes: 2 additions & 0 deletions tests/_support/Models/SimpleEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class SimpleEntity extends Entity
protected $id;
protected $name;
protected $description;
protected $deleted;
protected $created_at;
protected $updated_at;

protected $_options = [
'datamap' => [],
Expand Down
8 changes: 7 additions & 1 deletion tests/_support/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ class UserModel extends Model

protected $useSoftDeletes = true;

protected $dateFormat = 'integer';
protected $dateFormat = 'datetime';

public $name = '';

public $email = '';

public $country = '';
}
2 changes: 1 addition & 1 deletion tests/_support/Models/ValidErrorsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ValidErrorsModel extends Model

protected $useSoftDeletes = false;

protected $dateFormat = 'integer';
protected $dateFormat = 'int';

protected $allowedFields = [
'name',
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Models/ValidModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ValidModel extends Model

protected $useSoftDeletes = false;

protected $dateFormat = 'integer';
protected $dateFormat = 'int';

protected $allowedFields = [
'name',
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Database/Live/DbUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testUtilsCSVFromResult()

$data = array_filter(preg_split('/(\r\n|\n|\r)/', $data));

$this->assertEquals('"1","Developer","Awesome job, but sometimes makes you bored",""', $data[1]);
$this->assertEquals('"1","Developer","Awesome job, but sometimes makes you bored","0","",""', $data[1]);
}

//--------------------------------------------------------------------
Expand All @@ -203,7 +203,7 @@ public function testUtilsXMLFromResult()

$data = $util->getXMLFromResult($data);

$expected = '<root><element><id>4</id><name>Musician</name><description>Only Coldplay can actually called Musician</description><created_at></created_at></element></root>';
$expected = '<root><element><id>4</id><name>Musician</name><description>Only Coldplay can actually called Musician</description><deleted></deleted><created_at></created_at><updated_at></updated_at></element></root>';

$actual = preg_replace('#\R+#', '', $data);
$actual = preg_replace('/[ ]{2,}|[\t]/', '', $actual);
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Database/Live/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testGetFieldCount()
->get()
->getFieldCount();

$this->assertEquals(4, $jobs);
$this->assertEquals(6, $jobs);
}

//--------------------------------------------------------------------
Expand Down
Loading

0 comments on commit ebd203a

Please sign in to comment.