1<?php 2 3namespace Icinga\Module\Director\Test; 4 5use Icinga\Exception\IcingaException; 6use Icinga\Module\Director\Data\Db\DbObject; 7use Icinga\Module\Director\Db\Cache\PrefetchCache; 8use Icinga\Module\Director\Import\Sync; 9use Icinga\Module\Director\Objects\IcingaObject; 10use Icinga\Module\Director\Objects\ImportSource; 11use Icinga\Module\Director\Objects\SyncProperty; 12use Icinga\Module\Director\Objects\SyncRule; 13 14abstract class SyncTest extends BaseTestCase 15{ 16 protected $objectType; 17 18 protected $keyColumn; 19 20 /** @var ImportSource */ 21 protected $source; 22 23 /** @var SyncRule */ 24 protected $rule; 25 26 /** @var SyncProperty[] */ 27 protected $properties = array(); 28 29 /** @var Sync */ 30 protected $sync; 31 32 public function setUp() 33 { 34 $this->source = ImportSource::create(array( 35 'source_name' => 'testimport', 36 'provider_class' => 'Icinga\\Module\\Director\\Test\\ImportSourceDummy', 37 'key_column' => $this->keyColumn, 38 )); 39 $this->source->store($this->getDb()); 40 41 $this->rule = SyncRule::create(array( 42 'rule_name' => 'testrule', 43 'object_type' => $this->objectType, 44 'update_policy' => 'merge', 45 'purge_existing' => 'n' 46 )); 47 $this->rule->store($this->getDb()); 48 49 $this->sync = new Sync($this->rule); 50 } 51 52 public function tearDown() 53 { 54 // properties should be deleted automatically 55 if ($this->rule !== null && $this->rule->hasBeenLoadedFromDb()) { 56 $this->rule->delete(); 57 } 58 59 if ($this->source !== null && $this->source->hasBeenLoadedFromDb()) { 60 $this->source->delete(); 61 } 62 63 // find objects created by this class and delete them 64 $db = $this->getDb(); 65 $dummy = IcingaObject::createByType($this->objectType, array(), $db); 66 $query = $db->getDbAdapter()->select() 67 ->from($dummy->getTableName()) 68 ->where('object_name LIKE ?', 'SYNCTEST_%'); 69 70 /** @var IcingaObject $object */ 71 foreach (IcingaObject::loadAllByType($this->objectType, $db, $query) as $object) { 72 $object->delete(); 73 } 74 75 // make sure cache is clean for other tests 76 PrefetchCache::forget(); 77 DbObject::clearAllPrefetchCaches(); 78 } 79 80 /** 81 * @param array $rows 82 * 83 * @throws IcingaException 84 */ 85 protected function runImport($rows) 86 { 87 ImportSourceDummy::setRows($rows); 88 $this->source->runImport(); 89 if ($this->source->get('import_state') !== 'in-sync') { 90 throw new IcingaException('Import failed: %s', $this->source->get('last_error_message')); 91 } 92 } 93 94 protected function setUpProperty($properties = array()) 95 { 96 $properties = array_merge(array( 97 'rule_id' => $this->rule->id, 98 'source_id' => $this->source->id, 99 'merge_policy' => 'override', 100 ), $properties); 101 102 $this->properties[] = $property = SyncProperty::create($properties); 103 $property->store($this->getDb()); 104 } 105} 106