NeighborBehaviorTest.php 2.5 KB

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