Skip to content

Commit 7029f98

Browse files
committed
updated some tests and factory functions
1 parent 430a66e commit 7029f98

4 files changed

Lines changed: 185 additions & 19 deletions

File tree

src/Resource.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,30 @@ public function table($table = null) {
457457
}
458458

459459
public function __o($args) {
460-
$object = clone $this;
461-
$object->tipsy($this->tipsy());
462-
$object->load($args);
463-
return $object;
460+
//return call_user_func_array([self, '__o_static'], func_get_args());
461+
462+
$classname = get_called_class();
463+
464+
foreach (func_get_args() as $arg) {
465+
if (is_array($arg)) {
466+
foreach ($arg as $item) {
467+
$items[] = $this->tipsy()->factory($classname,$item);
468+
}
469+
} else {
470+
$items[] = $this->tipsy()->factory($classname,$arg);
471+
}
472+
}
473+
foreach ($items as $item) {
474+
$item->tipsy($this->tipsy());
475+
echo '123'.get_called_class($item);
476+
}
477+
478+
479+
if (count($items) == 1) {
480+
return array_pop($items);
481+
} else {
482+
return new Looper($items);
483+
}
464484
}
465485

466486
public static function __o_static() {
@@ -475,6 +495,9 @@ public static function __o_static() {
475495
$items[] = Tipsy::factory($classname,$arg);
476496
}
477497
}
498+
foreach ($items as $item) {
499+
$item->tipsy(Tipsy::app());
500+
}
478501

479502
if (count($items) == 1) {
480503
return array_pop($items);

tests/FactoryTest.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
class kindOfResource extends Tipsy\Model {
4+
public function idVar() {
5+
return 'id';
6+
}
7+
public function dbId() {
8+
return $this->{$this->idVar()};
9+
}
10+
}
11+
12+
class factoryResource extends \Tipsy\Resource {
13+
public function __construct($id = null) {
14+
$this->idVar('id')->table('test_user')->load($id);
15+
parent::__construct($id);
16+
}
17+
}
18+
19+
class FactoryTest extends Tipsy_Test {
20+
public function setUp() {
21+
$this->tip = new Tipsy\Tipsy;
22+
$this->tip->config('tests/config.ini');
23+
$this->setupDb($this->tip);
24+
}
25+
26+
public function testBasicStorage() {
27+
$m = new Tipsy\Model;
28+
$m->a = 1;
29+
$this->tip->factory($m, 1);
30+
31+
$m = new Tipsy\Model;
32+
$m->a = 2;
33+
$this->tip->factory($m, 2);
34+
35+
$this->assertEquals(1, $this->tip->factory('Tipsy\\Model', 1)->a);
36+
$this->assertEquals(2, $this->tip->factory('Tipsy\\Model', 2)->a);
37+
}
38+
39+
public function testResource() {
40+
$m = new factoryResource(1);
41+
$this->tip->factory($m);
42+
43+
$m = new factoryResource(2);
44+
$this->tip->factory($m);
45+
46+
$this->assertEquals(1, $this->tip->factory('factoryResource', 1)->dbId());
47+
$this->assertEquals(2, $this->tip->factory('factoryResource', 2)->dbId());
48+
}
49+
/*
50+
public function testStaticAuto() {
51+
$m = factoryResource::o(1);
52+
$m->first = true;
53+
$m = factoryResource::o(2);
54+
$m->second = true;
55+
$this->assertEquals(1, $this->tip->factory('factoryResource')->dbId());
56+
$this->assertEquals(2, $this->tip->factory('factoryResource')->dbId());
57+
}
58+
59+
public function testIdvarAuto() {
60+
$m = new kindOfResource;
61+
$m->id = 1;
62+
$this->tip->factory($m);
63+
64+
$m = new kindOfResource;
65+
$m->id = 2;
66+
$this->tip->factory($m);
67+
68+
$this->assertEquals(1, $this->tip->factory('kindOfResource')->dbId());
69+
$this->assertEquals(2, $this->tip->factory('kindOfResource')->dbId());
70+
}
71+
*/
72+
public function testIdvarUser() {
73+
$m = new kindOfResource;
74+
$m->id = 1;
75+
$this->tip->factory($m);
76+
77+
$m = new kindOfResource;
78+
$m->id = 2;
79+
$this->tip->factory($m);
80+
81+
$this->assertEquals(1, $this->tip->factory('kindOfResource', 1)->dbId());
82+
$this->assertEquals(2, $this->tip->factory('kindOfResource', 2)->dbId());
83+
}
84+
85+
public function testStaticRef() {
86+
$m = factoryResource::o(1);
87+
$m->first = true;
88+
$m = factoryResource::o(2);
89+
$m->second = true;
90+
91+
$m = factoryResource::o(1);
92+
$this->assertEquals(1, $m->dbId());
93+
$this->assertEquals(true, $m->first);
94+
95+
$m = factoryResource::o(2);
96+
$this->assertEquals(2, $m->dbId());
97+
$this->assertEquals(true, $m->second);
98+
}
99+
100+
public function testStaticRefMulti() {
101+
$this->markTestSkipped('Test incomplete');
102+
$l = factoryResource::o(1,2,[id => 3, name => 'new']);
103+
104+
$this->assertEquals(1, $l->dbId());
105+
$this->assertEquals(2, $l->dbId());
106+
$this->assertEquals(3, $l->dbId());
107+
}
108+
109+
public function testObjRefMulti() {
110+
$this->markTestSkipped('Test incomplete');
111+
$m = new factoryResource;
112+
$l = $m->o(1,2,[id => 3, name => 'new']);
113+
114+
$this->assertEquals(1, $l->dbId());
115+
$this->assertEquals(2, $l->dbId());
116+
$this->assertEquals(3, $l->dbId());
117+
}
118+
119+
public function testObjAuto() {
120+
return;
121+
$m = new factoryResource;
122+
$a = $m->o(1);
123+
$a->first = true;
124+
$b = $m->o(2);
125+
$b->second = true;
126+
127+
$a = $m->o(1);
128+
$this->assertEquals(1, $a->dbId());
129+
$this->assertEquals(true, $a->first);
130+
131+
$b = $m->o(2);
132+
$this->assertEquals(2, $b->dbId());
133+
$this->assertEquals(true, $b->second);
134+
}
135+
}

tests/HttpTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ public function testJsonGetJson() {
4343
$this->assertEquals($res, (object)[id => 1, key => 'value', method => 'get']);
4444
}
4545

46-
/*
4746
public function testJsonPostJson() {
47+
$this->markTestSkipped('Test incomplete');
4848
$http = (new Tipsy\Http())->post($this->host.'item/1/json', [key => 'value'], [type => 'json'])->complete(function($data) use (&$res) {
4949
$res = $data;
5050
});
5151
$this->assertEquals($res, (object)[id => 1, key => 'value', method => 'post']);
5252
}
53-
*/
5453

5554
public function testJsonGetPlain() {
5655
$http = (new Tipsy\Http())->get($this->host.'item/1/plain', [key => 'value'], [type => 'json'])->complete(function($data) use (&$res) {
@@ -59,15 +58,13 @@ public function testJsonGetPlain() {
5958
$this->assertEquals($res, '1.value.get');
6059
}
6160

62-
/*
6361
public function testJsonPostPlain() {
62+
$this->markTestSkipped('Test incomplete');
6463
$http = (new Tipsy\Http())->post($this->host.'item/1/plain', [key => 'value'], [type => 'json'])->complete(function($data) use (&$res) {
6564
$res = $data;
6665
});
6766
$this->assertEquals($res, '1.value.post');
6867
}
69-
*/
70-
7168

7269
public function testError() {
7370
$http = (new Tipsy\Http())->post($this->host.'404')

tests/ServiceTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ function stuff($stuff) {
1111

1212

1313
class ServiceTest extends Tipsy_Test {
14-
14+
1515
public function setUp() {
1616
$this->tip = new Tipsy\Tipsy;
1717
$this->useOb = true; // for debug use
1818
}
19-
19+
2020
public function testServiceClass() {
2121
$_REQUEST['__url'] = '';
2222
$class = $this->tip->service('LoginService');
@@ -25,6 +25,17 @@ public function testServiceClass() {
2525

2626
$this->assertEquals('LoginService', $class);
2727
}
28+
29+
public function testServiceExtend() {
30+
$_REQUEST['__url'] = '';
31+
$this->tip->service('LoginService/Second', [
32+
test => function() {
33+
return 'SECOND';
34+
}
35+
]);
36+
37+
$this->assertEquals('SECONDYESA', $this->tip->service('Second')->test().$this->tip->service('Second')->stuff('A'));
38+
}
2839
/*
2940
public function testServiceClassController() {
3041
$_REQUEST['__url'] = '';
@@ -35,14 +46,14 @@ public function testServiceClassController() {
3546
->when('', function($LoginService) {
3647
echo get_class($LoginService);
3748
});
38-
49+
3950
$this->ob();
4051
$this->tip->start();
4152
$check = $this->ob(false);
4253
4354
$this->assertEquals('LoginService', $check);
4455
}
45-
56+
4657
public function testServiceFunc() {
4758
$_REQUEST['__url'] = '';
4859
$this->tip->service('LoginService');
@@ -52,14 +63,14 @@ public function testServiceFunc() {
5263
die(get_class($LoginService));
5364
echo $LoginService->stuff('MAM');
5465
});
55-
66+
5667
$this->ob();
5768
$this->tip->start();
5869
$check = $this->ob(false);
5970
6071
$this->assertEquals('YESMAM', $check);
6172
}
62-
73+
6374
public function testServiceConstruct() {
6475
$_REQUEST['__url'] = '';
6576
$this->tip->service('LoginService');
@@ -68,16 +79,16 @@ public function testServiceConstruct() {
6879
->when('', function($LoginService) {
6980
echo $LoginService->loggedin;
7081
});
71-
82+
7283
$this->ob();
7384
$this->tip->start();
7485
$check = $this->ob(false);
75-
86+
7687
die($this->tip->service('LoginService')->loggedin);
7788
7889
$this->assertEquals('YES', $check);
7990
}
80-
91+
8192
public function testServiceAccess() {
8293
$_REQUEST['__url'] = '';
8394
$this->tip->service('LoginService');
@@ -87,7 +98,7 @@ public function testServiceAccess() {
8798
->when('', function($LoginService) {
8899
echo $LoginService->testVar;
89100
});
90-
101+
91102
$this->ob();
92103
$this->tip->start();
93104
$check = $this->ob(false);

0 commit comments

Comments
 (0)