| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace Tools\Test\TestCase\Model\Behavior;
- use Cake\ORM\TableRegistry;
- use Shim\TestSuite\TestCase;
- class NeighborBehaviorTest extends TestCase {
- /**
- * @var array
- */
- protected $fixtures = [
- 'plugin.Tools.Stories',
- ];
- /**
- * @var \Tools\Model\Table\Table
- */
- protected $Table;
- /**
- * @return void
- */
- public function setUp(): void {
- parent::setUp();
- $this->Table = TableRegistry::getTableLocator()->get('Stories');
- $this->Table->addBehavior('Tools.Neighbor');
- }
- /**
- * @return void
- */
- public function tearDown(): void {
- TableRegistry::clear();
- parent::tearDown();
- }
- /**
- * @return void
- */
- public function testNeighbors() {
- $id = 2;
- $result = $this->Table->neighbors($id);
- $this->assertEquals('Second', $result['prev']['title']);
- $this->assertEquals('Forth', $result['next']['title']);
- }
- /**
- * @return void
- */
- public function testNeighborsReverse() {
- $id = 2;
- $result = $this->Table->neighbors($id, ['reverse' => true]);
- $this->assertEquals('Forth', $result['prev']['title']);
- $this->assertEquals('Second', $result['next']['title']);
- }
- /**
- * @return void
- */
- public function testNeighborsCustomSortField() {
- $id = 2;
- $result = $this->Table->neighbors($id, ['sortField' => 'sort']);
- $this->assertEquals('Second', $result['prev']['title']);
- $this->assertEquals('First', $result['next']['title']);
- }
- /**
- * @return void
- */
- public function testNeighborsCustomFields() {
- $id = 2;
- $result = $this->Table->neighbors($id, ['sortField' => 'sort', 'fields' => ['title']]);
- $this->assertEquals(['title' => 'Second'], $result['prev']->toArray());
- $this->assertEquals(['title' => 'First'], $result['next']->toArray());
- }
- /**
- * @return void
- */
- public function testNeighborsStart() {
- $id = 1;
- $result = $this->Table->neighbors($id, ['sortField' => 'id']);
- $this->assertNull($result['prev']);
- $this->assertEquals('Third', $result['next']['title']);
- }
- /**
- * @return void
- */
- public function testNeighborsEnd() {
- $id = 4;
- $result = $this->Table->neighbors($id);
- $this->assertEquals('Third', $result['prev']['title']);
- $this->assertNull($result['next']);
- }
- }
|