예제 #1
0
 public function setUp()
 {
     $this->deps = Test\Factory::managerNoteDefault();
     if ($this->deps->connector->engine != 'sqlite') {
         return $this->markTestSkipped();
     }
 }
예제 #2
0
 function setUp()
 {
     $this->deps = $deps = Test\Factory::managerArraysModelCustom(['Pants' => ['table' => 'yep', 'class' => 'stdClass', 'fields' => ['foo' => true, 'bar' => true, 'baz' => true]]]);
     $deps->manager->insertTable('Pants', ['foo' => 'abc', 'bar' => '123', 'baz' => '!!!']);
     $deps->manager->insertTable('Pants', ['foo' => 'bcd', 'bar' => '234', 'baz' => '@@@']);
     $deps->manager->insertTable('Pants', ['foo' => 'cde', 'bar' => '345', 'baz' => '###']);
 }
 public function testCustomType()
 {
     $mapper = \Amiss\Sql\Factory::createMapper();
     $mapper->addTypeHandler(new TestCustomFieldTypeHandler(), 'foo');
     $deps = \Amiss\Test\Factory::managerNoteModelCustom('
         /** :amiss = true; */
         class TestCustomFieldTypeModel
         {
             /** :amiss = {"field": {"primary": true, "type": "autoinc" }}; */
             public $testCustomFieldTypeRecordId;
             
             /**
              * :amiss = {"field": {"type": "foo bar"}};
              */
             public $yep1;
         }
     ', (object) ['mapper' => $mapper]);
     $class = $deps->classes['TestCustomFieldTypeModel'];
     $r = new $class();
     $r->yep1 = 'foo';
     $deps->manager->save($r);
     $r = $deps->manager->getById($class, 1);
     // this will have passed through the prepareValueForDb first, then
     // through the handleValueFromDb method
     $this->assertEquals('value-db-foo', $r->yep1);
 }
예제 #4
0
 public function setUp()
 {
     $this->deps = Test\Factory::managerArraysModelCustom(['Test' => ['class' => 'stdClass', 'fields' => ['id' => ['primary' => true], 'linkedId' => ['index' => true]], 'relations' => ['link' => ['one', 'of' => 'Test', 'from' => 'linkedId', 'mode' => 'auto']]]]);
     $this->deps->manager->insertTable('Test', ['id' => 1, 'linkedId' => 2]);
     $this->deps->manager->insertTable('Test', ['id' => 2, 'linkedId' => 2]);
     $this->deps->manager->connector->queries = 0;
 }
예제 #5
0
 public function setUp()
 {
     $this->deps = Test\Factory::managerModelDemo();
     $this->manager = $this->deps->manager;
     $this->artist = $this->manager->get(Demo\Artist::class, 'artistId=?', array(1));
     $this->assertEquals('Limozeen', $this->artist->name);
 }
예제 #6
0
 /**
  * @covers Amiss\Sql\Manager::groupBy
  */
 public function testGroupBy()
 {
     $deps = Test\Factory::managerModelDemo();
     $indexed = $deps->manager->groupBy($this->objects, 'name');
     $expected = ['a' => [$this->objects[0]], 'b' => [$this->objects[1], $this->objects[2]]];
     $this->assertEquals($expected, $indexed);
 }
예제 #7
0
 public function setUp()
 {
     parent::setUp();
     $this->deps = Test\Factory::managerNoteDefault();
     $this->manager = $this->deps->manager;
     TableBuilder::create($this->deps->connector, $this->deps->mapper, [MappedFieldNameLeft::class, MappedFieldNameAssoc::class, MappedFieldNameRight::class]);
 }
예제 #8
0
    function setUp()
    {
        $d = $this->deps = Test\Factory::managerNoteModelCustom('
            /** :amiss = true; */
            class TestParent {
                /** :amiss = {"field": {"primary": true}}; */ public $id;
                
                private $children = "nope";

                /** :amiss = {"has": {"type": "many", "of": "{{ns}}TestChild", "to": "parentId"}}; */
                public function getChildren()   { return $this->children; }
                public function setChildren($v) { $this->children = $v; }
            }

            /** :amiss = true; */
            class TestChild {
                /** :amiss = {"field": {"primary": true}}; */ public $id;
                /** :amiss = {"field": {"index": true}};   */ public $parentId;

                private $parent = "nope";

                /** :amiss = {"has": {"type": "one", "of": "{{ns}}TestParent", "from": "parentId"}}; */
                public function getParent()   { return $this->parent; }
                public function setParent($v) { $this->parent = $v; }
            }
        ');
        $this->deps->manager->insertTable($d->classes['TestParent'], ['id' => 1]);
        $this->deps->manager->insertTable($d->classes['TestChild'], ['id' => 1, 'parentId' => 1]);
        $this->deps->manager->insertTable($d->classes['TestChild'], ['id' => 2, 'parentId' => 1]);
    }
예제 #9
0
    public function testInsertGeneratesGuid()
    {
        $d = Test\Factory::managerNoteModelCustom('
            /**
             * :amiss = {
             *     "on": {
             *         "beforeInsert": ["generateGuid"]
             *     },
             *     "primary": "id"
             * };
             */
            class AutoGuid {
                /** :amiss = {"field": {"type": "autoinc"}}; */
                public $id;
                
                /** :amiss = {"field": true}; */
                public $guid;

                public function generateGuid() {
                    $this->guid = \\Amiss\\Functions::guid();
                }
            }
        ');
        $cls = "{$d->ns}\\AutoGuid";
        $o = new $cls();
        $d->manager->insert($o);
        $this->assertNotEmpty($o->guid);
        $o = $d->manager->get($cls);
        $this->assertNotEmpty($o->guid);
    }
예제 #10
0
 /**
  * Ensures the signature for table insertion works
  *   Amiss\Sql\Manager->insert( string $table , array $values )
  * 
  * @group acceptance
  * @group manager
  */
 public function testInsertTable()
 {
     $deps = Test\Factory::managerModelDemo();
     $this->assertEquals(0, $deps->manager->count(Demo\Artist::class, 'slug="insert-table-test"'));
     $id = $deps->manager->insertTable(Demo\Artist::class, array('name' => 'Insert Table Test', 'slug' => 'insert-table-test', 'artistTypeId' => 1));
     $this->assertGreaterThan(0, $id);
     $this->assertEquals(1, $deps->manager->count(Demo\Artist::class, 'slug="insert-table-test"'));
 }
예제 #11
0
 public function setUp()
 {
     $this->deps = Test\Factory::managerModelDemo();
     $this->manager = $this->deps->manager;
     $this->artist = $this->deps->manager->get(Demo\Artist::class, 'artistId=?', array(1));
     if (!$this->artist) {
         throw new \UnexpectedValueException("Unexpected test data");
     }
 }
예제 #12
0
 public function testDeleteEvents()
 {
     $events = ['beforeDelete', 'afterDelete'];
     $deps = Test\Factory::managerNoteModelCustom($this->makeClass($events));
     $class = $deps->classes['Test'];
     $class::setManager($deps->manager);
     $deps->connector->exec("INSERT INTO test(id) VALUES(1);");
     $record = $class::getById(1);
     $record->delete();
     $this->assertEquals($events, $record->events);
 }
예제 #13
0
 /**
  * @covers Amiss\Sql\Manager::indexBy
  */
 public function testIndexByAllowedNulls()
 {
     $deps = Test\Factory::managerModelDemo();
     $obj1 = new \Amiss\Demo\Artist();
     $obj1->name = null;
     $obj2 = new \Amiss\Demo\Artist();
     $obj2->name = 'a';
     $indexed = $deps->manager->indexBy([$obj1, $obj2], 'name', null, null, !'ignoreNulls');
     $expected = ['' => $obj1, 'a' => $obj2];
     $this->assertEquals($expected, $indexed);
 }
예제 #14
0
 function setUp()
 {
     $map = ['Pants' => ['class' => 'stdClass', 'table' => 'pants', 'fields' => ['id' => ['primary' => true, 'type' => 'autoinc']], 'relations' => ['other' => ['one', 'of' => 'Other']]], 'Other' => ['class' => 'stdClass', 'table' => 'other', 'fields' => ['id' => ['primary' => true, 'type' => 'autoinc']]]];
     $map['Trou'] = $map['Pants'];
     $map['Trou']['table'] = 'trou';
     unset($map['Trou']['relations']);
     $this->deps = Test\Factory::managerArraysModelCustom($map);
     $manager = $this->deps->manager;
     $manager->insertTable('Other', ['id' => 1]);
     $manager->insertTable('Trou', ['id' => 1]);
 }
예제 #15
0
 function setUp()
 {
     parent::setUp();
     $mappings = ['foo' => ['table' => 'foo', 'class' => 'stdClass', 'fields' => ['fooId' => true, 'foo' => true]], 'bar' => ['table' => 'bar', 'class' => 'stdClass', 'fields' => ['barId' => true, 'fooId' => true, 'bar' => true]]];
     $this->deps = Factory::managerArraysModelCustom($mappings);
     $this->deps->mapper->mappings['baz'] = ['table' => 'baz', 'class' => 'stdClass', 'fields' => ['fooId' => true, 'barId' => true, 'foo' => true, 'bar' => true]];
     $this->deps->connector->query("\n            CREATE VIEW baz AS\n                SELECT bar.fooId, bar.barId, foo.foo, bar.bar FROM bar\n                INNER JOIN foo ON foo.fooId = bar.fooId\n        ");
     $this->deps->manager->insertTable('foo', ['fooId' => 1, 'foo' => 'a']);
     $this->deps->manager->insertTable('foo', ['fooId' => 2, 'foo' => 'b']);
     $this->deps->manager->insertTable('bar', ['fooId' => 1, 'barId' => 1, 'bar' => 'a']);
     $this->deps->manager->insertTable('bar', ['fooId' => 1, 'barId' => 2, 'bar' => 'b']);
 }
예제 #16
0
 public function setUp()
 {
     parent::setUp();
     $this->deps = Test\Factory::managerArraysModelCustom(['Parent' => ['class' => 'stdClass', 'table' => 't1', 'fields' => ['id' => ['primary' => true], 'orderedId' => ['index' => true]], 'relations' => ['ordered' => ['one', 'of' => 'Ordered', 'from' => 'orderedId']]], 'Ordered' => ['class' => 'stdClass', 'table' => 't2', 'primary' => 'id', 'fields' => ['id' => true, 'yep' => true, 'sort' => true], 'defaultOrder' => 'sort']]);
     $this->manager = $this->deps->manager;
     $this->manager->insertTable('Ordered', ['id' => 1, 'yep' => 2, 'sort' => 3]);
     $this->manager->insertTable('Ordered', ['id' => 2, 'yep' => 2, 'sort' => 2]);
     $this->manager->insertTable('Ordered', ['id' => 3, 'yep' => 2, 'sort' => 1]);
     $this->manager->insertTable('Parent', ['id' => 1, 'orderedId' => 2]);
     $this->manager->insertTable('Parent', ['id' => 2, 'orderedId' => 1]);
     $this->manager->insertTable('Parent', ['id' => 3, 'orderedId' => 3]);
 }
예제 #17
0
 public function setUp()
 {
     $this->deps = Test\Factory::managerNoteDefault();
     if ($this->deps->connector->engine == 'sqlite') {
         $this->deps->connector->execAll(["CREATE TABLE test_embed_one_parent(id INTEGER PRIMARY KEY AUTOINCREMENT, child TEXT)", "CREATE TABLE test_embed_many_parent(id INTEGER PRIMARY KEY AUTOINCREMENT, children TEXT)"]);
     } elseif ($this->deps->connector->engine == 'mysql') {
         $this->deps->connector->execAll(["CREATE TABLE test_embed_one_parent(id INT PRIMARY KEY AUTO_INCREMENT, child TEXT)", "CREATE TABLE test_embed_many_parent(id INT PRIMARY KEY AUTO_INCREMENT, children TEXT)"]);
     }
     // To test against either mysql or sqlite, we need to encode as well.
     $embed = new Type\Embed($this->deps->mapper);
     $encoder = new Type\Encoder('serialize', 'unserialize', $embed);
     $this->deps->mapper->addTypeHandler($encoder, 'embed');
 }
예제 #18
0
 public function testObjectToPropertiesWithGetters()
 {
     $deps = Test\Factory::managerNoteModelCustom('
         /** :amiss = true; */
         class Pants {
             /** :amiss = {"field": true}; */
             function getId() { return $this->id; }
             function setId($v) { $this->id = $v; }
         }
     ');
     $c = $deps->ns . "\\Pants";
     $pants = new $c();
     $pants->setId(1);
     $props = $deps->mapper->mapObjectToProperties($pants);
     $this->assertEquals(['id' => 1], $props);
 }
예제 #19
0
    function testSelectDateForcedTime()
    {
        $d = Test\Factory::managerNoteModelCustom('
            /** :amiss = true; */
            class Pants {
                /** :amiss = {"field": {"primary": true, "type": "autoinc"}}; */
                public $id;

                /** :amiss = {"field": {"type": "date"}}; */
                public $date;
            }
        ');
        $d->connector->exec("INSERT INTO pants(`date`) VALUES('2014-01-01');");
        $date = new \DateTime('2014-01-01T12:00:00Z');
        $result = $d->manager->getList($d->classes['Pants'], '{date}>=:date', ['date' => $date]);
        $this->assertCount(1, $result);
    }
예제 #20
0
    /**
     * @group acceptance
     * @group manager 
     * @group exists
     */
    public function testExistsKeySingle()
    {
        $deps = Test\Factory::managerNoteModelCustom('
            /** :amiss = true; */
            class Pants {
                /** :amiss = {"field": {"index": {"key": true}}}; */
                public $slug;

                /** :amiss = {"field": true}; */
                public $name;
            }
        ');
        $class = $deps->classes['Pants'];
        $deps->manager->insertTable($class, ['slug' => 'yes', 'name' => 'Yep!']);
        $this->assertTrue($deps->manager->exists($class, 'yes', 'slug'));
        $this->assertFalse($deps->manager->exists($class, 'nup', 'slug'));
    }
 public function setUp()
 {
     $this->deps = Test\Factory::managerNoteDefault();
     $this->manager = $this->deps->manager;
     $this->ns = 'Amiss\\Demo\\AssocDifferentFieldColumn';
     $tb = TableBuilder::create($this->manager->connector, $this->manager->mapper, [$this->ns . '\\Event', $this->ns . '\\Artist', $this->ns . '\\EventArtist']);
     $manager = $this->manager;
     $manager->insertTable($this->ns . '\\Event', ['id' => 1, 'name' => 'event1']);
     $manager->insertTable($this->ns . '\\Event', ['id' => 2, 'name' => 'event2']);
     $manager->insertTable($this->ns . '\\Event', ['id' => 3, 'name' => 'event3']);
     $manager->insertTable($this->ns . '\\Artist', ['id' => 1, 'name' => 'artist1']);
     $manager->insertTable($this->ns . '\\Artist', ['id' => 2, 'name' => 'artist2']);
     $manager->insertTable($this->ns . '\\Artist', ['id' => 3, 'name' => 'artist3']);
     $manager->insertTable($this->ns . '\\EventArtist', ['eventId' => 1, 'artistId' => 1]);
     $manager->insertTable($this->ns . '\\EventArtist', ['eventId' => 1, 'artistId' => 2]);
     $manager->insertTable($this->ns . '\\EventArtist', ['eventId' => 2, 'artistId' => 2]);
     $manager->insertTable($this->ns . '\\EventArtist', ['eventId' => 2, 'artistId' => 3]);
 }
예제 #22
0
    function testDecimalToDb()
    {
        $d = Test\Factory::managerNoteModelCustom('
            /** :amiss = true; */
            class Pants {
                /** :amiss = {"field": {"primary": true, "type": "autoinc"}}; */
                public $id;

                /** :amiss = {"field": {"type": "decimal"}}; */
                public $num;
            }
        ');
        $c = $d->ns . '\\Pants';
        $obj = new $c();
        $obj->num = Decimal::fromString("1.23456");
        $d->manager->insert($obj);
        $out = $d->connector->query("SELECT num FROM pants")->fetchColumn(0);
        $this->assertEquals("1.23456", $out);
    }
예제 #23
0
 public function testCreateTable()
 {
     $deps = Test\Factory::managerNoteDefault();
     $deps->mapper->addTypeHandler(new \Amiss\Sql\Type\Autoinc(), 'autoinc');
     $deps->mapper->defaultTableNameTranslator = function ($name) {
         return 'test_' . $name;
     };
     try {
         \Amiss\Demo\Active\DemoRecord::_reset();
         \Amiss\Demo\Active\DemoRecord::setManager($deps->manager);
         TableBuilder::create($deps->connector, $deps->mapper, Demo\Active\EventRecord::class);
         $er = new Demo\Active\EventRecord();
         $er->name = 'foo bar';
         $er->slug = 'foobar';
         $er->save();
         $this->assertTrue(true);
     } finally {
         \Amiss\Demo\Active\DemoRecord::_reset();
     }
 }
예제 #24
0
    function testMetaAfterDelete()
    {
        $d = Test\Factory::managerNoteModelCustom('
            /**
             * :amiss = {"on": {"afterDelete": ["a"]}};
             */
            class Pants {
                /** :amiss = {"field": {"primary": true, "type": "autoinc"}}; */
                public $id;

                function a() { $this->id = 99; }
            }
        ');
        $cls = $d->classes['Pants'];
        $d->manager->insertTable($cls, ['id' => 94]);
        $d->manager->on['afterDelete'][] = function ($object) {
            $object->id -= 5;
        };
        $o = new $cls();
        $o->id = 1;
        $d->manager->insert($o, $d->manager->getMeta($cls));
        // delete
        $o = $d->manager->getById($cls, 1);
        $this->assertEquals(1, $o->id);
        $d->manager->delete($o);
        // It will change in the instance
        $this->assertEquals(94, $o->id);
        // It should not exist in the DB
        $o = $d->manager->getById($cls, 1);
        $this->assertNull($o);
        // but the other one should
        $this->assertTrue($d->manager->exists($cls, 94));
    }
예제 #25
0
 /**
  * @group acceptance
  * @group manager 
  * @group getById
  */
 public function testGetByIdMultiNamed()
 {
     $d = Test\Factory::managerArraysModelCustom(['Pants' => ['class' => 'stdClass', 'primary' => ['foo', 'bar'], 'fields' => ['foo' => true, 'bar' => true]]]);
     $d->manager->insertTable('Pants', ['foo' => 2, 'bar' => 1]);
     $result = $d->manager->getById('Pants', ['bar' => 1, 'foo' => 2]);
     $this->assertEquals(2, $result->foo);
     $this->assertEquals(1, $result->bar);
 }
예제 #26
0
 protected static function managerNestedSetNote($classes)
 {
     $deps = Test\Factory::managerNoteModelCustom($classes);
     $deps->nsManager = \Amiss\Ext\NestedSet\Manager::createWithDefaults($deps->manager);
     return $deps;
 }
예제 #27
0
 public function setUp()
 {
     $this->deps = Test\Factory::managerModelDemo();
     $this->manager = $this->deps->manager;
 }
 public function setUp()
 {
     $this->deps = \Amiss\Test\Factory::managerActiveDemo();
 }
예제 #29
0
 public function setUp()
 {
     parent::setUp();
     $this->deps = Test\Factory::managerActiveDemo();
     $this->manager = $this->deps->manager;
 }
예제 #30
0
 function testCreate()
 {
     $d = Test\Factory::managerArraysModelCustom(['Pants' => ['table' => 'ORDER BY', 'class' => 'stdClass', 'fields' => ['foo' => true]]]);
     return $d;
 }