| 1 |
<?php |
| 2 |
|
| 3 |
use PHPUnit\Framework\TestCase; |
| 4 |
|
| 5 |
class OptionRepositoryTest extends TestCase { |
| 6 |
|
| 7 |
public function setUp() { |
| 8 |
$this->db = $this->createMock('ResponsiveMenu\Database\WpDatabase'); |
| 9 |
$this->factory = $this->createMock('ResponsiveMenu\Factories\OptionFactory'); |
| 10 |
$this->defaults = ['a' => 1, 'b' => 2, 'c' => 3]; |
| 11 |
|
| 12 |
$this->repo = new ResponsiveMenu\Repositories\OptionRepository($this->db, $this->factory, $this->defaults); |
| 13 |
} |
| 14 |
|
| 15 |
public function testCollectionReturned() { |
| 16 |
$obj_1 = new \StdClass; |
| 17 |
$obj_1->name = 'a'; |
| 18 |
$obj_1->value = 1; |
| 19 |
|
| 20 |
$obj_2 = new \StdClass; |
| 21 |
$obj_2->name = 'b'; |
| 22 |
$obj_2->value = 2; |
| 23 |
|
| 24 |
$this->db->method('all')->willReturn([$obj_1, $obj_2]); |
| 25 |
|
| 26 |
$this->factory->method('build')->will($this->onConsecutiveCalls( |
| 27 |
new ResponsiveMenu\Models\Option('a', 1), |
| 28 |
new ResponsiveMenu\Models\Option('b', 2) |
| 29 |
)); |
| 30 |
|
| 31 |
$this->assertInstanceOf('ResponsiveMenu\Collections\OptionsCollection', $this->repo->all()); |
| 32 |
} |
| 33 |
|
| 34 |
public function testCollectionCountIsReturned() { |
| 35 |
$obj_1 = new \StdClass; |
| 36 |
$obj_1->name = 'a'; |
| 37 |
$obj_1->value = 1; |
| 38 |
|
| 39 |
$obj_2 = new \StdClass; |
| 40 |
$obj_2->name = 'b'; |
| 41 |
$obj_2->value = 2; |
| 42 |
|
| 43 |
$this->db->method('all')->willReturn([$obj_1, $obj_2]); |
| 44 |
|
| 45 |
$this->factory->method('build')->will($this->onConsecutiveCalls( |
| 46 |
new ResponsiveMenu\Models\Option('a', 1), |
| 47 |
new ResponsiveMenu\Models\Option('b', 2) |
| 48 |
)); |
| 49 |
|
| 50 |
$this->assertEquals(2, count($this->repo->all()->all())); |
| 51 |
} |
| 52 |
|
| 53 |
public function testDbUpdateIsCalled() { |
| 54 |
$option = $this->createMock('ResponsiveMenu\Models\Option'); |
| 55 |
$option->method('getFiltered')->willReturn(1); |
| 56 |
$option->method('getName')->willReturn('a'); |
| 57 |
$this->db->method('update')->willReturn(true); |
| 58 |
$this->assertTrue($this->repo->update($option)); |
| 59 |
} |
| 60 |
|
| 61 |
public function testDbCreateIsCalled() { |
| 62 |
$option = $this->createMock('ResponsiveMenu\Models\Option'); |
| 63 |
$option->method('getFiltered')->willReturn(1); |
| 64 |
$option->method('getName')->willReturn('a'); |
| 65 |
$this->db->method('insert')->willReturn(true); |
| 66 |
$this->db->method('mySqlTime')->willReturn('123'); |
| 67 |
$this->assertTrue($this->repo->create($option)); |
| 68 |
} |
| 69 |
|
| 70 |
public function testDbRemoveIsCalled() { |
| 71 |
$this->db->method('delete')->willReturn(true); |
| 72 |
$this->assertTrue($this->repo->remove('a')); |
| 73 |
} |
| 74 |
|
| 75 |
public function testBuildFromArray() { |
| 76 |
$opt_1 = new ResponsiveMenu\Models\Option('a', 4); |
| 77 |
$opt_1->setFilter($this->createMock('ResponsiveMenu\Filters\TextFilter')); |
| 78 |
|
| 79 |
$opt_2 = new ResponsiveMenu\Models\Option('d', 6); |
| 80 |
$opt_2->setFilter($this->createMock('ResponsiveMenu\Filters\TextFilter')); |
| 81 |
|
| 82 |
$this->factory->method('build')->will($this->onConsecutiveCalls($opt_1, $opt_2, $opt_1, $opt_2)); |
| 83 |
|
| 84 |
$this->assertInstanceOf('ResponsiveMenu\Collections\OptionsCollection', $this->repo->buildFromArray(['a' => 4, 'd' => 6])); |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
|