NeighborBehaviorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Tools\Model\Behavior;
  3. use Cake\Core\Configure;
  4. use Cake\ORM\TableRegistry;
  5. use Tools\Model\Behavior\NeighborBehavior;
  6. use Tools\Model\Table\Table;
  7. use Tools\TestSuite\TestCase;
  8. class NeighborBehaviorTest extends TestCase {
  9. /**
  10. * @var \Tools\Model\Table\Table
  11. */
  12. public $Table;
  13. /**
  14. * @var array
  15. */
  16. public $fixtures = ['plugin.tools.stories'];
  17. public function setUp() {
  18. parent::setUp();
  19. //Configure::write('App.namespace', 'TestApp');
  20. $this->Table = TableRegistry::get('Stories');
  21. $this->Table->addBehavior('Tools.Neighbor');
  22. }
  23. public function tearDown() {
  24. TableRegistry::clear();
  25. parent::tearDown();
  26. }
  27. /**
  28. * NeighborBehaviorTest::testNeighborRecords()
  29. *
  30. * @return void
  31. */
  32. public function testNeighbors() {
  33. $id = 2;
  34. $result = $this->Table->neighbors($id);
  35. $this->assertEquals('Second', $result['prev']['title']);
  36. $this->assertEquals('Forth', $result['next']['title']);
  37. }
  38. /**
  39. * NeighborBehaviorTest::testNeighborRecords()
  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. * NeighborBehaviorTest::testNeighborRecords()
  51. *
  52. * @return void
  53. */
  54. public function testNeighborsCustomSortField() {
  55. $id = 2;
  56. $result = $this->Table->neighbors($id, ['sortField' => 'sort']);
  57. $this->assertEquals('Second', $result['prev']['title']);
  58. $this->assertEquals('First', $result['next']['title']);
  59. }
  60. /**
  61. * NeighborBehaviorTest::testNeighborRecords()
  62. *
  63. * @return void
  64. */
  65. public function testNeighborsCustomFields() {
  66. $id = 2;
  67. $result = $this->Table->neighbors($id, ['sortField' => 'sort', 'fields' => ['title']]);
  68. //debug($result);
  69. $this->assertEquals(['title' => 'Second'], $result['prev']->toArray());
  70. $this->assertEquals(['title' => 'First'], $result['next']->toArray());
  71. }
  72. /**
  73. * NeighborBehaviorTest::testNeighborRecords()
  74. *
  75. * @return void
  76. */
  77. public function testNeighborsStart() {
  78. $id = 1;
  79. $result = $this->Table->neighbors($id, ['sortField' => 'id']);
  80. $this->assertNull($result['prev']);
  81. $this->assertEquals('Third', $result['next']['title']);
  82. }
  83. /**
  84. * NeighborBehaviorTest::testNeighborRecords()
  85. *
  86. * @return void
  87. */
  88. public function testNeighborsEnd() {
  89. $id = 4;
  90. $result = $this->Table->neighbors($id);
  91. //debug($result);
  92. $this->assertEquals('Third', $result['prev']['title']);
  93. $this->assertNull($result['next']);
  94. }
  95. }