NeighborBehaviorTest.php 2.1 KB

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