CurrentRouteTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace yiiunit\debug\router;
  3. use PHPUnit\Framework\TestCase;
  4. use yii\debug\models\router\CurrentRoute;
  5. use yii\log\Logger;
  6. class CurrentRouteTest extends TestCase
  7. {
  8. /**
  9. * @test
  10. */
  11. public function shouldDoNothingForNoMessages()
  12. {
  13. $router = new CurrentRoute();
  14. $this->assertSame([], $router->messages);
  15. $this->assertSame('', $router->route);
  16. $this->assertSame('', $router->action);
  17. $this->assertNull($router->message);
  18. $this->assertSame([], $router->logs);
  19. $this->assertSame(0, $router->count);
  20. $this->assertFalse($router->hasMatch);
  21. }
  22. /**
  23. * @test
  24. */
  25. public function shouldStoreMessageForProperOne()
  26. {
  27. $router = new CurrentRoute(['messages' => [['test', Logger::LEVEL_TRACE]]]);
  28. $this->assertSame('', $router->route);
  29. $this->assertSame('', $router->action);
  30. $this->assertSame('test', $router->message);
  31. $this->assertSame([], $router->logs);
  32. $this->assertSame(0, $router->count);
  33. $this->assertFalse($router->hasMatch);
  34. }
  35. /**
  36. * @test
  37. */
  38. public function shouldStoreLogForProperOneAndTrueMatch()
  39. {
  40. $router = new CurrentRoute(
  41. [
  42. 'messages' => [
  43. [
  44. ['rule' => 'test rule', 'match' => true],
  45. 999
  46. ]
  47. ]
  48. ]
  49. );
  50. $this->assertSame('', $router->route);
  51. $this->assertSame('', $router->action);
  52. $this->assertNull($router->message);
  53. $this->assertSame([['rule' => 'test rule', 'match' => true]], $router->logs);
  54. $this->assertSame(1, $router->count);
  55. $this->assertTrue($router->hasMatch);
  56. }
  57. /**
  58. * @test
  59. */
  60. public function shouldStoreLogForProperOneAndFalseMatch()
  61. {
  62. $router = new CurrentRoute(
  63. [
  64. 'messages' => [
  65. [
  66. ['rule' => 'test rule', 'match' => false],
  67. 999
  68. ]
  69. ]
  70. ]
  71. );
  72. $this->assertSame('', $router->route);
  73. $this->assertSame('', $router->action);
  74. $this->assertNull($router->message);
  75. $this->assertSame([['rule' => 'test rule', 'match' => false]], $router->logs);
  76. $this->assertSame(1, $router->count);
  77. $this->assertFalse($router->hasMatch);
  78. }
  79. /**
  80. * @test
  81. */
  82. public function shouldSkipParent()
  83. {
  84. $router = new CurrentRoute(
  85. [
  86. 'messages' => [
  87. [
  88. ['rule' => 'test rule', 'match' => false, 'parent' => 'test parent'],
  89. 999
  90. ],
  91. [
  92. ['rule' => 'test parent', 'match' => false],
  93. 999
  94. ]
  95. ]
  96. ]
  97. );
  98. $this->assertSame('', $router->route);
  99. $this->assertSame('', $router->action);
  100. $this->assertNull($router->message);
  101. $this->assertSame([['rule' => 'test rule', 'match' => false, 'parent' => 'test parent']], $router->logs);
  102. $this->assertSame(1, $router->count);
  103. $this->assertFalse($router->hasMatch);
  104. }
  105. /**
  106. * @test
  107. */
  108. public function shouldIncreaseCounter()
  109. {
  110. $router = new CurrentRoute(
  111. [
  112. 'messages' => [
  113. [
  114. ['rule' => 'test rule 1', 'match' => false],
  115. 999
  116. ],
  117. [
  118. ['rule' => 'test rule 2', 'match' => false],
  119. 999
  120. ]
  121. ]
  122. ]
  123. );
  124. $this->assertSame('', $router->route);
  125. $this->assertSame('', $router->action);
  126. $this->assertNull($router->message);
  127. $this->assertSame(
  128. [
  129. ['rule' => 'test rule 1', 'match' => false],
  130. ['rule' => 'test rule 2', 'match' => false]
  131. ],
  132. $router->logs
  133. );
  134. $this->assertSame(2, $router->count);
  135. $this->assertFalse($router->hasMatch);
  136. }
  137. }