NeighborBehaviorTest.php 2.2 KB

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