NeighborBehaviorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public function setUp() {
  16. parent::setUp();
  17. //Configure::write('App.namespace', 'TestApp');
  18. $this->Table = TableRegistry::get('Stories');
  19. $this->Table->addBehavior('Tools.Neighbor');
  20. }
  21. public function tearDown() {
  22. TableRegistry::clear();
  23. parent::tearDown();
  24. }
  25. /**
  26. * NeighborBehaviorTest::testNeighborRecords()
  27. *
  28. * @return void
  29. */
  30. public function testNeighbors() {
  31. $id = 2;
  32. $result = $this->Table->neighbors($id);
  33. $this->assertEquals('Second', $result['prev']['title']);
  34. $this->assertEquals('Forth', $result['next']['title']);
  35. }
  36. /**
  37. * NeighborBehaviorTest::testNeighborRecords()
  38. *
  39. * @return void
  40. */
  41. public function testNeighborsReverse() {
  42. $id = 2;
  43. $result = $this->Table->neighbors($id, ['reverse' => true]);
  44. $this->assertEquals('Forth', $result['prev']['title']);
  45. $this->assertEquals('Second', $result['next']['title']);
  46. }
  47. /**
  48. * NeighborBehaviorTest::testNeighborRecords()
  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. * NeighborBehaviorTest::testNeighborRecords()
  60. *
  61. * @return void
  62. */
  63. public function testNeighborsCustomFields() {
  64. $id = 2;
  65. $result = $this->Table->neighbors($id, ['sortField' => 'sort', 'fields' => ['title']]);
  66. //debug($result);
  67. $this->assertEquals(['title' => 'Second'], $result['prev']->toArray());
  68. $this->assertEquals(['title' => 'First'], $result['next']->toArray());
  69. }
  70. /**
  71. * NeighborBehaviorTest::testNeighborRecords()
  72. *
  73. * @return void
  74. */
  75. public function testNeighborsStart() {
  76. $id = 1;
  77. $result = $this->Table->neighbors($id, ['sortField' => 'id']);
  78. $this->assertNull($result['prev']);
  79. $this->assertEquals('Third', $result['next']['title']);
  80. }
  81. /**
  82. * NeighborBehaviorTest::testNeighborRecords()
  83. *
  84. * @return void
  85. */
  86. public function testNeighborsEnd() {
  87. $id = 4;
  88. $result = $this->Table->neighbors($id);
  89. //debug($result);
  90. $this->assertEquals('Third', $result['prev']['title']);
  91. $this->assertNull($result['next']);
  92. }
  93. }