TitleTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\Framework\View\Page\Config
  8. */
  9. namespace Magento\Framework\View\Test\Unit\Page;
  10. use Magento\Store\Model\ScopeInterface;
  11. class TitleTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\View\Page\Title
  15. */
  16. protected $title;
  17. /**
  18. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $scopeConfigMock;
  21. /**
  22. * @return void
  23. */
  24. protected function setUp()
  25. {
  26. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  30. $this->title = $objectManagerHelper->getObject(
  31. \Magento\Framework\View\Page\Title::class,
  32. ['scopeConfig' => $this->scopeConfigMock]
  33. );
  34. }
  35. /**
  36. * @return void
  37. */
  38. public function testSet()
  39. {
  40. $value = 'test_value';
  41. $this->title->set($value);
  42. $this->assertEquals($value, $this->title->get());
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function testUnset()
  48. {
  49. $value = 'test';
  50. $this->title->set($value);
  51. $this->assertEquals($value, $this->title->get());
  52. $this->title->unsetValue();
  53. $this->assertEmpty($this->title->get());
  54. }
  55. /**
  56. * @return void
  57. */
  58. public function testGet()
  59. {
  60. $value = 'test';
  61. $prefix = 'prefix';
  62. $suffix = 'suffix';
  63. $expected = 'prefix test suffix';
  64. $this->scopeConfigMock->expects($this->any())
  65. ->method('getValue')
  66. ->willReturnMap(
  67. [
  68. ['design/head/title_prefix', ScopeInterface::SCOPE_STORE, null, $prefix],
  69. ['design/head/title_suffix', ScopeInterface::SCOPE_STORE, null, $suffix],
  70. ]
  71. );
  72. $this->title->set($value);
  73. $this->assertEquals($expected, $this->title->get());
  74. }
  75. /**
  76. * @return void
  77. */
  78. public function testGetShort()
  79. {
  80. $value = 'some_title';
  81. $this->title->set($value);
  82. $this->title->prepend($value);
  83. $this->title->append($value);
  84. $this->assertEquals($value, $this->title->getShort());
  85. }
  86. /**
  87. * @return void
  88. */
  89. public function testGetShortWithSuffixAndPrefix()
  90. {
  91. $value = 'some_title';
  92. $prefix = 'prefix';
  93. $suffix = 'suffix';
  94. $expected = $prefix . ' ' . $value . ' ' . $suffix;
  95. $this->title->set($value);
  96. $this->scopeConfigMock->expects($this->any())
  97. ->method('getValue')
  98. ->willReturnMap(
  99. [
  100. ['design/head/title_prefix', ScopeInterface::SCOPE_STORE, null, $prefix],
  101. ['design/head/title_suffix', ScopeInterface::SCOPE_STORE, null, $suffix],
  102. ]
  103. );
  104. $this->assertEquals($expected, $this->title->getShort());
  105. }
  106. /**
  107. * @return void
  108. */
  109. public function testGetShortHeading()
  110. {
  111. $value = 'some_title';
  112. $this->title->set($value);
  113. $this->scopeConfigMock->expects($this->never())
  114. ->method('getValue');
  115. $this->assertEquals($value, $this->title->getShortHeading());
  116. }
  117. /**
  118. * @return void
  119. */
  120. public function testGetDefault()
  121. {
  122. $defaultTitle = 'default title';
  123. $prefix = 'prefix';
  124. $suffix = 'suffix';
  125. $expected = 'prefix default title suffix';
  126. $this->scopeConfigMock->expects($this->any())
  127. ->method('getValue')
  128. ->willReturnMap(
  129. [
  130. ['design/head/title_prefix', ScopeInterface::SCOPE_STORE, null, $prefix],
  131. ['design/head/title_suffix', ScopeInterface::SCOPE_STORE, null, $suffix],
  132. ['design/head/default_title', ScopeInterface::SCOPE_STORE, null, $defaultTitle],
  133. ]
  134. );
  135. $this->assertEquals($expected, $this->title->getDefault());
  136. }
  137. /**
  138. * @return void
  139. */
  140. public function testAppendPrepend()
  141. {
  142. $value = 'title';
  143. $prepend = 'prepend_title';
  144. $append = 'append_title';
  145. $expected = 'prepend_title / title / append_title';
  146. $this->title->set($value);
  147. $this->title->prepend($prepend);
  148. $this->title->append($append);
  149. $this->assertEquals($expected, $this->title->get());
  150. }
  151. }