NeighborBehaviorTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Tools\Model\Behavior;
  3. use Cake\ORM\TableRegistry;
  4. use Tools\Model\Table\Table;
  5. use Tools\TestSuite\TestCase;
  6. class NeighborBehaviorTest extends TestCase {
  7. /**
  8. * @var \Tools\Model\Table\Table
  9. */
  10. public $Table;
  11. /**
  12. * @var array
  13. */
  14. public $fixtures = ['plugin.tools.stories'];
  15. /**
  16. * @return void
  17. */
  18. public function setUp() {
  19. parent::setUp();
  20. $this->Table = TableRegistry::get('Stories');
  21. $this->Table->addBehavior('Tools.Neighbor');
  22. }
  23. /**
  24. * @return void
  25. */
  26. public function tearDown() {
  27. TableRegistry::clear();
  28. parent::tearDown();
  29. }
  30. /**
  31. * @return void
  32. */
  33. public function testNeighbors() {
  34. $id = 2;
  35. $result = $this->Table->neighbors($id);
  36. $this->assertEquals('Second', $result['prev']['title']);
  37. $this->assertEquals('Forth', $result['next']['title']);
  38. }
  39. /**
  40. * @return void
  41. */
  42. public function testNeighborsReverse() {
  43. $id = 2;
  44. $result = $this->Table->neighbors($id, ['reverse' => true]);
  45. $this->assertEquals('Forth', $result['prev']['title']);
  46. $this->assertEquals('Second', $result['next']['title']);
  47. }
  48. /**
  49. * @return void
  50. */
  51. public function testNeighborsCustomSortField() {
  52. $id = 2;
  53. $result = $this->Table->neighbors($id, ['sortField' => 'sort']);
  54. $this->assertEquals('Second', $result['prev']['title']);
  55. $this->assertEquals('First', $result['next']['title']);
  56. }
  57. /**
  58. * @return void
  59. */
  60. public function testNeighborsCustomFields() {
  61. $id = 2;
  62. $result = $this->Table->neighbors($id, ['sortField' => 'sort', 'fields' => ['title']]);
  63. $this->assertEquals(['title' => 'Second'], $result['prev']->toArray());
  64. $this->assertEquals(['title' => 'First'], $result['next']->toArray());
  65. }
  66. /**
  67. * @return void
  68. */
  69. public function testNeighborsStart() {
  70. $id = 1;
  71. $result = $this->Table->neighbors($id, ['sortField' => 'id']);
  72. $this->assertNull($result['prev']);
  73. $this->assertEquals('Third', $result['next']['title']);
  74. }
  75. /**
  76. * @return void
  77. */
  78. public function testNeighborsEnd() {
  79. $id = 4;
  80. $result = $this->Table->neighbors($id);
  81. $this->assertEquals('Third', $result['prev']['title']);
  82. $this->assertNull($result['next']);
  83. }
  84. }