AttributeValueSpec.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace spec\Sylius\Component\Attribute\Model;
  11. use PhpSpec\ObjectBehavior;
  12. use Sylius\Component\Attribute\Model\AttributeInterface;
  13. use Sylius\Component\Attribute\Model\AttributeSubjectInterface;
  14. use Sylius\Component\Attribute\Model\AttributeTypes;
  15. /**
  16. * @author Paweł Jędrzejewski <pawel@sylius.org>
  17. */
  18. class AttributeValueSpec extends ObjectBehavior
  19. {
  20. function it_is_initializable()
  21. {
  22. $this->shouldHaveType('Sylius\Component\Attribute\Model\AttributeValue');
  23. }
  24. function it_implements_Sylius_subject_attribute_interface()
  25. {
  26. $this->shouldImplement('Sylius\Component\Attribute\Model\AttributeValueInterface');
  27. }
  28. function it_has_no_id_by_default()
  29. {
  30. $this->getId()->shouldReturn(null);
  31. }
  32. function it_does_not_belong_to_a_subject_by_default()
  33. {
  34. $this->getSubject()->shouldReturn(null);
  35. }
  36. function it_allows_assigning_itself_to_a_subject(AttributeSubjectInterface $subject)
  37. {
  38. $this->setSubject($subject);
  39. $this->getSubject()->shouldReturn($subject);
  40. }
  41. function it_allows_detaching_itself_from_a_subject(AttributeSubjectInterface $subject)
  42. {
  43. $this->setSubject($subject);
  44. $this->getSubject()->shouldReturn($subject);
  45. $this->setSubject(null);
  46. $this->getSubject()->shouldReturn(null);
  47. }
  48. function it_has_no_attribute_defined_by_default()
  49. {
  50. $this->getAttribute()->shouldReturn(null);
  51. }
  52. function its_attribute_is_definable(AttributeInterface $attribute)
  53. {
  54. $this->setAttribute($attribute);
  55. $this->getAttribute()->shouldReturn($attribute);
  56. }
  57. function it_has_no_value_by_default()
  58. {
  59. $this->getValue()->shouldReturn(null);
  60. }
  61. function its_value_is_mutable()
  62. {
  63. $this->setValue('XXL');
  64. $this->getValue()->shouldReturn('XXL');
  65. }
  66. function it_converts_value_to_Boolean_if_attribute_has_checkbox_type(AttributeInterface $attribute)
  67. {
  68. $attribute->getType()->willReturn(AttributeTypes::CHECKBOX);
  69. $this->setAttribute($attribute);
  70. $this->setValue('1');
  71. $this->getValue()->shouldReturn(true);
  72. $this->setValue(0);
  73. $this->getValue()->shouldReturn(false);
  74. }
  75. function it_returns_its_value_when_converted_to_string()
  76. {
  77. $this->setValue('S');
  78. $this->__toString()->shouldReturn('S');
  79. }
  80. function it_throws_exception_when_trying_to_get_name_without_attribute_defined()
  81. {
  82. $this
  83. ->shouldThrow('BadMethodCallException')
  84. ->duringGetName()
  85. ;
  86. }
  87. function it_returns_its_attribute_name(AttributeInterface $attribute)
  88. {
  89. $attribute->getName()->willReturn('T-Shirt material');
  90. $this->setAttribute($attribute);
  91. $this->getName()->shouldReturn('T-Shirt material');
  92. }
  93. function it_throws_exception_when_trying_to_get_presentation_without_attribute_defined()
  94. {
  95. $this
  96. ->shouldThrow('BadMethodCallException')
  97. ->duringGetPresentation()
  98. ;
  99. }
  100. function it_returns_its_attribute_presentation(AttributeInterface $attribute)
  101. {
  102. $attribute->getPresentation()->willReturn('Material');
  103. $this->setAttribute($attribute);
  104. $this->getPresentation()->shouldReturn('Material');
  105. }
  106. function it_throws_exception_when_trying_to_get_type_without_attribute_defined()
  107. {
  108. $this
  109. ->shouldThrow('BadMethodCallException')
  110. ->duringGetType()
  111. ;
  112. }
  113. function it_returns_its_attribute_type(AttributeInterface $attribute)
  114. {
  115. $attribute->getType()->willReturn('choice');
  116. $this->setAttribute($attribute);
  117. $this->getType()->shouldReturn('choice');
  118. }
  119. function it_throws_exception_when_trying_to_get_configuration_without_attribute_defined()
  120. {
  121. $this
  122. ->shouldThrow('BadMethodCallException')
  123. ->duringGetConfiguration()
  124. ;
  125. }
  126. function it_returns_its_attribute_configuration(AttributeInterface $attribute)
  127. {
  128. $attribute->getConfiguration()->willReturn(array('choices' => array('Red')));
  129. $this->setAttribute($attribute);
  130. $this->getConfiguration()->shouldReturn(array('choices' => array('Red')));
  131. }
  132. }