-
-
Save MayMeow/a163111262275ec1ff70e25dd29b08ab to your computer and use it in GitHub Desktop.
Model testing in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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