Inheritance: extends Model
Ejemplo n.º 1
0
 public function testDBFunctions()
 {
     $db = new DB_Interface();
     foreach ($models = $db->show('testmodel') as $model) {
         $model->delete();
     }
     $tests = $db->show('testmodel');
     $this->assertEquals(count($tests), 0);
     for ($i = 1; $i <= 10; $i++) {
         $model = new TestModel();
         $model->name = 'This is a test';
         $model->price = 1;
         $model->size = '5';
         $model->save();
         $tests = $db->show('testmodel');
         $this->assertEquals(count($tests), $i);
     }
     $models = $db->show('testmodel');
     for ($i = 0; $i < count($models); $i++) {
         $model = $models[$i];
         $model->delete();
         $tests = $db->show('testmodel');
         $this->assertEquals(count($tests), count($models) - $i - 1);
     }
 }
Ejemplo n.º 2
0
 public function appControllerLoad()
 {
     $TestModel = new TestModel();
     $list = $TestModel->getList();
     json_return($list);
     test();
 }
Ejemplo n.º 3
0
function test()
{
    $obj = new TestModel();
    var_dump($obj->getInfo());
    $param = func_get_args();
    var_dump($param);
}
Ejemplo n.º 4
0
 function getData()
 {
     $testModel = new TestModel();
     $data = $testModel->setData();
     $testView = new TestView();
     $testView->display($data);
 }
Ejemplo n.º 5
0
 function getItemsAction()
 {
     $model = new TestModel();
     $data = $model->getItems();
     $view = new TestView();
     $view->renderItemsPage($data);
 }
Ejemplo n.º 6
0
 public function testUpdatePointHasCorrectSql()
 {
     $this->model->exists = true;
     $this->model->point = new Point(2, 4);
     $this->model->save();
     $this->assertContains("ST_GeogFromText('POINT(4 2)')", $this->queries[0]);
 }
Ejemplo n.º 7
0
 /**
  * @test
  */
 public function shouldCreateModelWithEmptyPrimaryKeyAndSequence()
 {
     //when
     $testModel = new TestModel(array('primaryKey' => '', 'sequence' => '', 'fields' => array('field1')));
     //then
     $this->assertEquals('', $testModel->getIdName());
     $this->assertEquals('', $testModel->getSequenceName());
 }
Ejemplo n.º 8
0
 function indexAction()
 {
     $fc = FrontController::getInstance();
     $model = new TestModel();
     $model->name = $fc->getParams();
     $output = $model->render('../views/index.php');
     $fc->setBody($output);
 }
Ejemplo n.º 9
0
 public function testOrderByCanBeOverridden()
 {
     $model = new TestModel();
     $query1 = $model->newQuery()->orderBy('name')->orderBy('email', 'desc')->toSql();
     $query2 = $model->newQuery()->orderBy('sort_order')->orderBy('name')->toSql();
     $this->assertEquals('select * from "test" order by "name" asc, "email" desc', $query1);
     $this->assertEquals('select * from "test" order by "sort_order" asc, "name" asc', $query2);
 }
Ejemplo n.º 10
0
 public function testSingleAfterSaveCall()
 {
     \PHPModelDemo\ModelDemoConfig::init();
     $test_obj = new TestModel();
     $test_obj->setAfterSaveCounter(0);
     $test_obj->save();
     $this->assertEquals(1, $test_obj->getAfterSaveCounter());
 }
Ejemplo n.º 11
0
 public function testDecodeJson()
 {
     $model = new TestModel();
     $model->jsonProperty = 'foo';
     $model->foo = '{"bar":"baz"}';
     $this->assertEquals(json_decode('{"bar":"baz"}'), $model->foo()->all());
     return $model;
 }
Ejemplo n.º 12
0
 /** @test */
 public function cannotSetAttributeOnImmutableModel()
 {
     $model = new TestModel();
     $model->makeMutable();
     $model->foo = 'bar';
     $model->makeImmutable();
     $this->setExpectedException('RuntimeException');
     $model->foo = 'bar';
 }
Ejemplo n.º 13
0
 public function testArrayMethodCall()
 {
     $model = new TestModel();
     $model->jsonProperty = ['foo', 'bar'];
     $model->foo = null;
     $model->bar = null;
     $this->assertInstanceOf(JsonProperty::class, $model->foo());
     $this->assertInstanceOf(JsonProperty::class, $model->bar());
 }
Ejemplo n.º 14
0
 private function test()
 {
     $test = new TestModel();
     $res = (yield $test->MysqlMuticallTest());
     SysLog::info(__METHOD__ . " res == " . print_r($res, true), __CLASS__);
     if ($res['r'] == 0) {
         //yield success
         SysLog::info(__METHOD__ . " yield success data == " . print_r($res['data'], true), __CLASS__);
         (yield $res);
     } else {
         //yield failed
         SysLog::error(__METHOD__ . " yield failed res == " . print_r($res, true), __CLASS__);
         (yield array('r' => 1, 'error_msg' => 'yield failed'));
     }
 }
 protected function seed($attributes = [])
 {
     $item = ['id' => ['S' => str_random(36)], 'name' => ['S' => str_random(36)], 'description' => ['S' => str_random(256)], 'count' => ['N' => rand()]];
     $item = array_merge($item, $attributes);
     $this->dynamoDbClient->putItem(['TableName' => $this->testModel->getTable(), 'Item' => $item]);
     return $item;
 }
Ejemplo n.º 16
0
 public function testCriteria()
 {
     $this->specify("eager loading is applied when only one is given", function () {
         $this->assertCount(1, TestModelWithWithableMethod::withRelations('relation1')->getEagerLoads());
         $this->assertContains('relation1', array_keys(TestModelWithWithableMethod::withRelations('relation1')->getEagerLoads()));
         $this->assertNotContains('relation2', array_keys(TestModelWithWithableMethod::withRelations('relation1')->getEagerLoads()));
     });
     $this->specify("eager loading are applied when array is given", function () {
         $this->assertCount(2, TestModelWithWithableMethod::withRelations(['relation1', 'relation2'])->getEagerLoads());
         $this->assertContains('relation1', array_keys(TestModelWithWithableMethod::withRelations(['relation1', 'relation2'])->getEagerLoads()));
         $this->assertContains('relation2', array_keys(TestModelWithWithableMethod::withRelations(['relation1', 'relation2'])->getEagerLoads()));
         $this->assertNotContains('relation3', array_keys(TestModelWithWithableMethod::withRelations(['relation1', 'relation2'])->getEagerLoads()));
     });
     $this->specify("eager load is applied only to withable relations", function () {
         $this->assertCount(1, TestModelWithWithableMethod::withRelations('relation1')->getEagerLoads());
         $this->assertCount(2, TestModelWithWithableMethod::withRelations(['relation1', 'relation2'])->getEagerLoads());
         $this->assertCount(2, TestModelWithWithableMethod::withRelations(['relation1', 'relation2', 'relation3'])->getEagerLoads());
     });
     $this->specify('getWithableRelations is not required, if $withable property exists', function () {
         $this->assertCount(2, TestModelWithWithableProperty::withRelations(['relation1', 'relation2'])->getEagerLoads());
         $this->assertContains('relation1', array_keys(TestModelWithWithableProperty::withRelations(['relation1', 'relation2'])->getEagerLoads()));
         $this->assertContains('relation2', array_keys(TestModelWithWithableProperty::withRelations(['relation1', 'relation2'])->getEagerLoads()));
         $this->assertNotContains('relation3', array_keys(TestModelWithWithableProperty::withRelations(['relation1', 'relation2'])->getEagerLoads()));
     });
     $this->specify('model must implement getWithableAttributes() or have $withable property', function () {
         TestModel::withRelations('relation1');
     }, ['throws' => new RuntimeException()]);
     $this->specify('* in withable relations list makes all relations loadable', function () {
         $this->assertCount(1, TestModelWithAllRelationsWithable::withRelations('relation1')->getEagerLoads());
         $this->assertCount(2, TestModelWithAllRelationsWithable::withRelations(['relation1', 'relation2'])->getEagerLoads());
         $this->assertCount(3, TestModelWithAllRelationsWithable::withRelations(['relation1', 'relation2', 'relation3'])->getEagerLoads());
         $this->assertCount(4, TestModelWithAllRelationsWithable::withRelations(['relation1', 'relation2', 'relation3', 'relation4'])->getEagerLoads());
     });
 }
Ejemplo n.º 17
0
 public function testEventHandler()
 {
     $queue = Yii::$app->queue;
     /* @var $queue \UrbanIndo\Yii2\Queue\Queues\MemoryQueue */
     $this->assertEquals(0, $queue->getSize());
     $model = new TestModel();
     $model->recordId = 1;
     $model->createRecord();
     $model->triggerEvent();
     $this->assertEquals(1, $queue->getSize());
     $job = $queue->fetch();
     $this->assertEquals(0, $queue->getSize());
     $queue->run($job);
     $sameModel = DeferredEventBehaviorTestActiveRecord::findOne($model->recordId);
     $this->assertEquals('done', $sameModel->name);
 }
Ejemplo n.º 18
0
 public static function &getInstance()
 {
     if (!isset(self::$instance)) {
         self::$instance = new self();
     }
     return self::$instance;
 }
Ejemplo n.º 19
0
 public function testCriteria()
 {
     $this->specify("sort criterion is applied when only one is given", function () {
         $this->assertCount(1, (array) TestModelWithSortableMethod::sorted(['sort' => 'field1,asc'])->getQuery()->orders);
     });
     $this->specify("sort criteria are applied when array is given", function () {
         $this->assertCount(2, (array) TestModelWithSortableMethod::sorted(['field1,asc', 'field2,desc'])->getQuery()->orders);
     });
     $this->specify("sort criterion is applied when only one is given", function () {
         $this->assertCount(1, (array) TestModelWithSortableMethod::sorted(['sort' => 'field1,asc'])->getQuery()->orders);
     });
     $this->specify("sort criteria are applied when array is given", function () {
         $this->assertCount(2, (array) TestModelWithSortableMethod::sorted(['field1,asc', 'field2,desc'])->getQuery()->orders);
     });
     $this->specify("criteria are applied only to sortable parameters", function () {
         $this->assertCount(0, (array) TestModelWithSortableMethod::sorted('field0,asc')->getQuery()->orders);
         $this->assertCount(1, (array) TestModelWithSortableMethod::sorted(['field0,asc', 'field1,desc'])->getQuery()->orders);
         $this->assertCount(2, (array) TestModelWithSortableMethod::sorted(['sort' => ['field0,asc', 'field1,desc', 'field2,desc']])->getQuery()->orders);
         $this->assertCount(2, (array) TestModelWithSortableMethod::sorted(['field0,asc', 'field1,desc', 'field2,desc', 'field3,desc'])->getQuery()->orders);
     });
     $this->specify("criteria are applied to columns by name", function () {
         $criterion = (array) TestModelWithSortableMethod::sorted('field1,asc')->getQuery()->orders[0];
         $this->assertEquals('field1', $criterion['column']);
     });
     $this->specify("criteria are applied in the same order as specified", function () {
         $criteria = (array) TestModelWithSortableMethod::sorted(['field1,desc', 'field2,desc'])->getQuery()->orders;
         $this->assertEquals('field1', $criteria[0]['column']);
         $this->assertEquals('field2', $criteria[1]['column']);
         $criteria = (array) TestModelWithSortableMethod::sorted(['field2,desc', 'field1,desc'])->getQuery()->orders;
         $this->assertEquals('field2', $criteria[0]['column']);
         $this->assertEquals('field1', $criteria[1]['column']);
     });
     $this->specify('getSearchableAttribues is not required, if $searchable property exists', function () {
         $criteria = (array) TestModelWithSortableProperty::sorted(['field2,desc', 'field1,desc'])->getQuery()->orders;
         $this->assertEquals('field2', $criteria[0]['column']);
         $this->assertEquals('field1', $criteria[1]['column']);
     });
     $this->specify('model must implement getSortableAttributes() or have $sortable property', function () {
         TestModel::sorted(['field1,desc', 'field2,desc']);
     }, ['throws' => new RuntimeException()]);
     $this->specify('* in searchable field list makes all fields searchable', function () {
         $criteria = (array) TestModelWithAllFieldsSortable::sorted(['field2,desc', 'field42,desc'])->getQuery()->orders;
         $this->assertEquals('field2', $criteria[0]['column']);
         $this->assertEquals('field42', $criteria[1]['column']);
     });
     $this->specify('available callback method is used in lieu of standard sorting', function () {
         $criteria = (array) TestModelWithSortableCallbackMethod::sorted(['created_at,desc'])->getQuery()->orders;
         $this->assertEquals('created', $criteria[0]['column']);
         $this->assertEquals('desc', $criteria[0]['direction']);
     });
     $this->specify('default sorting criteria are applued', function () {
         Input::shouldReceive('input')->andReturn(null);
         $criteria = (array) TestModelWithDefaultSortingCriteria::sorted()->getQuery()->orders;
         $this->assertEquals('column1', $criteria[0]['column']);
         $this->assertEquals('desc', $criteria[0]['direction']);
         $this->assertEquals('column2', $criteria[1]['column']);
         $this->assertEquals('asc', $criteria[1]['direction']);
     });
 }
Ejemplo n.º 20
0
 public function indexAction()
 {
     require_once ZOODPP_APP . '/models/TestModel.php';
     $list = TestModel::getTests();
     $this->setData('list', $list);
     $this->addResult(self::RESULT_SUCCESS, 'php', 'test/test.php');
     return self::RESULT_SUCCESS;
 }
Ejemplo n.º 21
0
 public function testHydrate()
 {
     $test = new TestModel(['id' => 1, 'first_name' => 'john', 'last_name' => 'smith', 'age' => 29, 'secret' => 'foobar'], true);
     // test construct / hydration
     $this->assertEquals(1, $test->id);
     $this->assertEquals('john', $test->first_name);
     $this->assertEquals('smith', $test->last_name);
     $this->assertEquals(29, $test->age);
     $this->assertEquals('foobar', $test->secret);
     $this->assertFalse($test->isNew());
     // test revert
     $test->age = 34;
     $this->assertEquals(34, $test->age);
     $this->assertEquals(29, $test->revert()->age);
     // test save
     $test->age = 34;
     $this->assertEquals(34, $test->age);
     $test->save();
     $this->assertEquals(34, $test->revert()->age);
     // test sync
     $this->assertEquals(['id' => 1, 'first_name' => 'john', 'last_name' => 'smith', 'age' => 27, 'secret' => 'foobar2'], $test->sync()->toArray());
     // test not fillable column
     $test->id = 20;
     $this->assertEquals(1, $test->id);
     // test delete
     $this->assertEquals(['id' => null, 'first_name' => null, 'last_name' => null, 'age' => null, 'secret' => null], $test->delete()->toArray());
 }
Ejemplo n.º 22
0
 public function calculatePerTime($filters = [])
 {
     $this->queryFields = [\DB::raw('COUNT(*) AS `count`')];
     $query = $this->applyFilters(\TestModel::query(), $filters);
     $query = $this->applyTimeGroup($query);
     $results = $query->get($this->queryFields);
     $results = $this->fillDateGaps($results, $filters);
     return $this->dataTable;
 }
Ejemplo n.º 23
0
 public static function _model($table = '')
 {
     if (null == self::$_instance) {
         self::$_instance = new self();
     }
     if (!empty($table)) {
         self::$_instance->selectTable($table);
     }
     return self::$_instance;
 }
 public function testSerializableValues()
 {
     $testModel = TestModel::create(['title' => 'Title', 'values' => ['foo' => 'Something', 'bar' => 'Baz']]);
     $this->assertEquals('Something', $testModel->foo, 'Serialized value not properly set.');
     $this->assertEquals('Baz', $testModel->bar, 'Serialized value not properly set.');
     $testModel->foo = 'Bar';
     $this->assertEquals('Bar', $testModel->foo, 'Serialized value not properly set.');
     $testModel->baz = 'bar';
     $this->assertNull($testModel->getValue('baz'), 'Unallowed value was set.');
 }
Ejemplo n.º 25
0
 public function indexAction()
 {
     $data = array();
     $data['title'] = '列表页面';
     $condition = array('fields' => array('id', 'title', 'author', 'create_time'), 'condition' => 'id in (?, ?) or title=?', 'bind' => array(1, 2, 'c'), 'limit' => array('count' => 5));
     //        $data['story_list'] = Model_Test::_model('story')->fetchAll($condition);
     $data['story_list'] = TestModel::_model('story')->fetchAll($condition);
     $this->setInvokeArg('layout', 'mainLayout');
     $this->render($data, 'list');
 }
Ejemplo n.º 26
0
 public function indexAction()
 {
     /*$this->getRequest()->widget = function(){
           echo 'call widget';
       };*/
     #DebugTools::print_r($this->getRequest());
     $test_model = new TestModel();
     $test = $test_model->getList(array('cost' => 8));
     DebugTools::print_r($test);
     /*foreach ($test as $k=>$v){
           DebugTools::print_r($v);
       }*/
     #System_Memcache::set('test1','12346',60);
     #DebugTools::print_r(System_Memcache::get('test1'));
     #$version = System_Memcache::getInstance()->getVersion();
     #DebugTools::print_r($version);
     #$stats = System_Memcache::getInstance()->getStats();
     #DebugTools::print_r($stats);
     #$server_status = System_Memcache::getInstance()->getServerStatus('127.0.0.1',11211);
     #DebugTools::print_r($server_status);
     #DebugTools::print_r(System_Memcache::getExtendedStats());
     #DebugTools::print_r($test_model->dc_close());
     /*$test = System_Mongo::getInstance()->conn()
                 ->selectDB('gamedb')
                 ->selectCollection('entity_ff14_ClassJob')
                 ->findOne(array('Key'=>intval(1)));
     
             DebugTools::print_r($test);
             $a = System_Mongo::getInstance()->close();
             DebugTools::print_r($a.'a');
     
             $test = System_Mongo::getInstance()->conn()
                 ->selectDB('gamedb')
                 ->selectCollection('entity_ff14_ClassJob')
                 ->select(array(),array(),array(),3);
     
             foreach ($test as $doc){
                 DebugTools::print_r($doc);
             }*/
     $this->getView()->assign("content", "Hello World");
     return TRUE;
 }
 /**
  * @param $connection
  *
  * @dataProvider connectionsProvider
  */
 function testDba($connection)
 {
     $capsule = $this->setConnection($connection);
     $capsule->connection()->enableQueryLog();
     $table = new TestModel();
     $table->name = 'One';
     $table->save();
     /** @var \Fnp\Eloquent\Grammar $grammar */
     $grammar = $this->config[$connection]['grammar'];
     $quote = function ($name) use($connection) {
         $quote = $this->config[$connection]['quote'];
         return $quote . $name . $quote;
     };
     $timestamp = $grammar::timestamp();
     $generatedSql = $capsule->connection()->getQueryLog()[0]['query'];
     $pattern = '#insert into .test_table. \\(.name., .updated_at., .created_at.\\) values \\(\\?, (.*), (.*)\\)#';
     $match = [];
     $result = preg_match($pattern, $generatedSql, $match);
     $this->assertEquals($match[1], $timestamp);
     $this->assertEquals($match[2], $timestamp);
 }
Ejemplo n.º 28
0
 public function test_1Action()
 {
     $model = TestModel::model();
     $starttime = explode(' ', microtime());
     for ($i = 0; $i < 1000; $i++) {
         $data = $model->where('id', $i)->page(1, 4)->get();
     }
     debug($data);
     //程序运行时间
     $endtime = explode(' ', microtime());
     $thistime = $endtime[0] + $endtime[1] - ($starttime[0] + $starttime[1]);
     $thistime = round($thistime, 3);
     echo "本网页执行耗时:" . $thistime . " 秒。" . time();
     echo 'hello world';
 }
Ejemplo n.º 29
0
 public function testGenerateSchema2()
 {
     $model = new TestModel();
     $schema = Pluf::factory('Pluf_DB_Schema', $this->db);
     $schema->model = $model;
     $this->assertEquals(true, $schema->dropTables());
     $this->assertEquals(true, $schema->createTables());
     $model->title = 'my title';
     $model->description = 'A small desc.';
     $this->assertEquals(true, $model->create());
     $this->assertEquals(1, (int) $model->id);
     $this->assertEquals(true, $schema->dropTables());
 }
Ejemplo n.º 30
0
 function testInit()
 {
     $this->assertIsA($this->TestSource, 'TwitterSource');
     $this->assertIsA($this->TestModel->getDataSource(), 'TwitterSource');
 }