Quote from: sottwell at Nov 14, 2013, 12:40 AM
Probably the second-biggest one (after getting a firm grip on git and github) still remains, though... tests. Many years ago when I was a larval programmer, my library got in a new book, something about mathematically provable programming. At that time it went right over my head, and I've had a phobia ever since. So I have to get a grip on myself and figure out what these "unit test" thingies are all about.
You can peek at the MyComponent unit tests here:
https://github.com/BobRay/MyComponent/tree/master/_build/test
Here's an excerpt ($this, BTW, is the PHPUnit_Framework_TestCase class which the test class extends):
$this->mc->createResources();
$resources = $this->mc->props['resources'];
$this->assertNotEmpty($resources);
$id = null;
foreach ($resources as $resource => $fields) {
$r = $this->modx->getObject('modResource', array('pagetitle' => $resource));
$this->assertInstanceOf('modResource', $r);
$pagetitle = $r->get('pagetitle');
$this->assertNotEmpty($pagetitle);
/* see if parent, template, and TV Values are set properly */
if ($pagetitle == 'utResource1') {
$this->assertEquals($this->modx->getOption('default_template'), $r->get('template'));
$this->assertEquals(0, $r->get('parent'));
$id = $r->get('id');
} elseif ($pagetitle == 'utResource2') {
$templateObj = $this->modx->getObject('modTemplate',
array('templatename' => 'utTemplate1'));
$templateId = $templateObj->get('id');
$this->assertEquals($templateId, $r->get('template'));
$this->assertEquals($id, $r->get('parent'));
$this->assertEquals('SomeValue', $r->getTVValue('utTv1'));
$this->assertEquals('SomeOtherValue', $r->getTVValue('utTv2'));
} else {
/* make sure pagetitles are set properly */
assertTrue(false, 'BAD RESOURCE pagetitle');
}
}
It's mainly just checking to see that arrays and variables are not empty and values equal what they should.
It's a great comfort to know that MyComponent passes them all before a new release. When someone reports a bug, I try to add a new test (which fails) before fixing it.
Unit testing can be pretty addictive.
BTW, PhpStorm will generate a set of skeleton unit tests for you.