Skip to content

Instantly share code, notes, and snippets.

@MayMeow
Created June 16, 2019 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MayMeow/a163111262275ec1ff70e25dd29b08ab to your computer and use it in GitHub Desktop.
Save MayMeow/a163111262275ec1ff70e25dd29b08ab to your computer and use it in GitHub Desktop.
Model testing in PHP
<?php
namespace App\Test\TestCase\Model\Table;
use App\Model\Table\NotesTable;
use Cake\ORM\RulesChecker;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Cake\Validation\Validator;
/**
* App\Model\Table\NotesTable Test Case
*/
class NotesTableTest extends TestCase
{
/**
* Test subject
*
* @var \App\Model\Table\NotesTable
*/
public $Notes;
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'app.Notes',
'app.Profiles'
];
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$config = TableRegistry::getTableLocator()->exists('Notes') ? [] : ['className' => NotesTable::class];
$this->Notes = TableRegistry::getTableLocator()->get('Notes', $config);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
unset($this->Notes);
parent::tearDown();
}
/**
* Test initialize method
*
* @return void
*/
public function testInitialize()
{
$this->Notes->initialize([]);
$this->assertEquals('id', $this->Notes->getPrimaryKey(), 'The [Notes] default primary key is expected to be `id`');
$exepctedAssociations = [
'Profiles'
];
foreach ($exepctedAssociations as $exepctedAssociation) {
$this->assertTrue(
$this->Notes->associations()->has($exepctedAssociation),
"Cursory sanity check The [Notes] Table is expected to be associated with $exepctedAssociation"
);
}
$expectedBehaviors = [
'Timestamp',
//'CreatorModifier'
];
foreach ($expectedBehaviors as $expectedBehavior) {
$this->assertTrue(
$this->Notes->behaviors()->has($expectedBehavior),
"Cursory sanity check The [Notes] Table is expected to use $expectedBehavior behavior"
);
}
}
/**
* Test validationDefault method
*
* @return void
*/
public function testValidationDefault()
{
$validator = new Validator();
$validator = $this->Notes->validationDefault($validator);
$this->assertTrue($validator->hasField('id'));
}
/**
* Test buildRules method
*
* @return void
*/
public function testBuildRules()
{
$this->assertInstanceOf(
RulesChecker::class,
$this->Notes->buildRules(new RulesChecker()),
"Cursory sanity check buildRules() should return a ruleChecker."
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment