NeighborBehaviorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. public $Table;
  10. public $fixtures = ['plugin.tools.stories'];
  11. public function setUp() {
  12. parent::setUp();
  13. //Configure::write('App.namespace', 'TestApp');
  14. $this->Table = TableRegistry::get('Stories');
  15. $this->Table->addBehavior('Tools.Neighbor');
  16. }
  17. public function tearDown() {
  18. TableRegistry::clear();
  19. parent::tearDown();
  20. }
  21. /**
  22. * NeighborBehaviorTest::testNeighborRecords()
  23. *
  24. * @return void
  25. */
  26. public function testNeighbors() {
  27. $id = 2;
  28. $result = $this->Table->neighbors($id);
  29. $this->assertEquals('Second', $result['prev']['title']);
  30. $this->assertEquals('Forth', $result['next']['title']);
  31. }
  32. /**
  33. * NeighborBehaviorTest::testNeighborRecords()
  34. *
  35. * @return void
  36. */
  37. public function testNeighborsReverse() {
  38. $id = 2;
  39. $result = $this->Table->neighbors($id, ['reverse' => true]);
  40. $this->assertEquals('Forth', $result['prev']['title']);
  41. $this->assertEquals('Second', $result['next']['title']);
  42. }
  43. /**
  44. * NeighborBehaviorTest::testNeighborRecords()
  45. *
  46. * @return void
  47. */
  48. public function testNeighborsCustomSortField() {
  49. $id = 2;
  50. $result = $this->Table->neighbors($id, ['sortField' => 'sort']);
  51. $this->assertEquals('Second', $result['prev']['title']);
  52. $this->assertEquals('First', $result['next']['title']);
  53. }
  54. /**
  55. * NeighborBehaviorTest::testNeighborRecords()
  56. *
  57. * @return void
  58. */
  59. public function testNeighborsCustomFields() {
  60. $id = 2;
  61. $result = $this->Table->neighbors($id, ['sortField' => 'sort', 'fields' => ['title']]);
  62. //debug($result);
  63. $this->assertEquals(['title' => 'Second'], $result['prev']->toArray());
  64. $this->assertEquals(['title' => 'First'], $result['next']->toArray());
  65. }
  66. /**
  67. * NeighborBehaviorTest::testNeighborRecords()
  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. * NeighborBehaviorTest::testNeighborRecords()
  79. *
  80. * @return void
  81. */
  82. public function testNeighborsEnd() {
  83. $id = 4;
  84. $result = $this->Table->neighbors($id);
  85. //debug($result);
  86. $this->assertEquals('Third', $result['prev']['title']);
  87. $this->assertNull($result['next']);
  88. }
  89. }