Skip to content

Commit

Permalink
added some looper and middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedevin committed Dec 1, 2015
1 parent 5c14ad2 commit 3c2580e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
52 changes: 47 additions & 5 deletions tests/LooperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public function testSet() {

public function testFilter() {
$loop = new \Tipsy\Looper([
(object)['a' => 1, 'b' => 1],
(object)['a' => 2, 'b' => 1],
(object)['a' => 3, 'b' => 3]
(object)['a' => 4, 'b' => 1],
(object)['a' => 5, 'b' => 1],
(object)['a' => 6, 'b' => 3]
]);
$loop = $loop->filter([
'b' => 1
Expand All @@ -89,7 +89,23 @@ public function testFilter() {
$loop->each(function() use (&$val) {
$val += $this->a;
});
$this->assertEquals(3, $val);
$this->assertEquals(9, $val);
}

public function testFilterNot() {
$loop = new \Tipsy\Looper([
(object)['a' => 4, 'b' => 1],
(object)['a' => 5, 'b' => 1],
(object)['a' => 6, 'b' => 3]
]);
$loop = $loop->not([
'b' => 1
]);
$val = 0;
$loop->each(function() use (&$val) {
$val += $this->a;
});
$this->assertEquals(6, $val);
}

public function testEq() {
Expand All @@ -98,7 +114,7 @@ public function testEq() {
$this->assertEquals(3, $val);
}

public function testGet() {
public function testEqGet() {
$loop = new \Tipsy\Looper([1,2,3]);
$val = $loop->eq(0);
$this->assertEquals(1, $val);
Expand Down Expand Up @@ -147,4 +163,30 @@ public function testToJson() {
$loop = new \Tipsy\Looper([1,"2"], new LoopItem);
$this->assertEquals('[1,"2",{"test":true}]', $loop->json());
}

public function testLoopInLoop() {
$loop = new \Tipsy\Looper([1,2], new \Tipsy\Looper([3,4], [5,6]));
$this->assertEquals('123456', "".$loop);
}

public function testGet() {
$loop = new \Tipsy\Looper([1,2]);
$this->assertEquals(1, $loop->get(0));
}

public function testSlice() {
$loop = new \Tipsy\Looper([
(object)['a' => 4],
(object)['a' => 5],
(object)['a' => 6],
(object)['a' => 7],
(object)['a' => 8]
]);
$slice = $loop->slice(2, 2);
$val = 0;
$slice->each(function() use (&$val) {
$val += $this->a;
});
$this->assertEquals(13, $val);
}
}
24 changes: 15 additions & 9 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function run($test) {
}
}

class MiddleWareTestFail {
function run() {
return false;
}
}


class MiddlewareTest extends Tipsy_Test {

Expand Down Expand Up @@ -54,16 +60,16 @@ public function testMiddlewareTipsy() {
$this->tip->middleware('LoginServiceTipsy');
$check = $this->ob(false);
$this->assertEquals('', $check);

$this->tip->router()->home(function() {});
$this->tip->start();
$check = $this->ob(false);
$this->assertEquals('HI', $check);

$check = $this->tip->service('LoginServiceTipsy')->test();
$this->assertEquals('HELLO', $check);
}

public function testMiddlewareTipsyDirect() {
$_REQUEST['__url'] = '';
$this->ob();
Expand All @@ -80,16 +86,16 @@ public function testMiddlewareTipsyDirect() {

$check = $this->ob(false);
$this->assertEquals('', $check);

$this->tip->router()->home(function() {});
$this->tip->start();
$check = $this->ob(false);
$this->assertEquals('HI', $check);

$check = $this->tip->service('LoginServiceTipsy')->test();
$this->assertEquals('HELLO', $check);
}

public function testMiddlewareFailure() {
$_REQUEST['__url'] = '';
$this->ob();
Expand All @@ -110,7 +116,7 @@ public function testMiddlewareFailure() {
}
$this->assertTrue($check);
}

/*
@todo: need to communicate with eachother
public function testMiddlewareToMiddlewareReference() {
Expand All @@ -122,7 +128,7 @@ public function testMiddlewareToMiddlewareReference() {
return 'HELLO';
}
]);
$this->tip->middleware('Tipsy\Service/SecondService', function($FirstService) {
return [
test => function() {
Expand All @@ -133,7 +139,7 @@ public function testMiddlewareToMiddlewareReference() {
$this->tip->router()->home(function() {});
$this->tip->start();
$check = $this->tip->middleware('SecondService')->test();
$this->assertEquals('HELLO', $check);
}
Expand Down

0 comments on commit 3c2580e

Please sign in to comment.