|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Coduo\ToString; |
| 4 | + |
| 5 | +use PhpSpec\ObjectBehavior; |
| 6 | + |
| 7 | +class Foo |
| 8 | +{ |
| 9 | + public function __toString() |
| 10 | + { |
| 11 | + return 'This is Foo'; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class StringSpec extends ObjectBehavior |
| 16 | +{ |
| 17 | + function it_convert_double_to_string() |
| 18 | + { |
| 19 | + $this->beConstructedWith(1.1); |
| 20 | + $this->__toString()->shouldReturn('1.1'); |
| 21 | + } |
| 22 | + |
| 23 | + function it_convert_double_to_string_for_specific_locale() |
| 24 | + { |
| 25 | + $this->beConstructedWith(1.1, 'pl'); |
| 26 | + $this->__toString()->shouldReturn('1,1'); |
| 27 | + } |
| 28 | + |
| 29 | + function it_convert_integer_to_string() |
| 30 | + { |
| 31 | + $this->beConstructedWith(20); |
| 32 | + $this->__toString()->shouldReturn('20'); |
| 33 | + } |
| 34 | + |
| 35 | + function it_convert_boolean_to_string() |
| 36 | + { |
| 37 | + $this->beConstructedWith(true); |
| 38 | + $this->__toString()->shouldReturn('true'); |
| 39 | + } |
| 40 | + |
| 41 | + function it_convert_object_to_string() |
| 42 | + { |
| 43 | + $this->beConstructedWith(new \stdClass()); |
| 44 | + $this->__toString()->shouldReturn('\stdClass'); |
| 45 | + } |
| 46 | + |
| 47 | + function it_convert_object_with_toString_method_to_string() |
| 48 | + { |
| 49 | + $this->beConstructedWith(new Foo()); |
| 50 | + $this->__toString()->shouldReturn('This is Foo'); |
| 51 | + } |
| 52 | + |
| 53 | + function it_convert_array_to_string() |
| 54 | + { |
| 55 | + $this->beConstructedWith(array('foo', 'bar')); |
| 56 | + $this->__toString()->shouldReturn('Array(2)'); |
| 57 | + } |
| 58 | + |
| 59 | + function it_convert_resource_to_string() |
| 60 | + { |
| 61 | + $resource = fopen(sys_get_temp_dir() . "/foo", "w"); |
| 62 | + $this->beConstructedWith($resource); |
| 63 | + $this->__toString()->shouldReturn('Resource(stream)'); |
| 64 | + fclose($resource); |
| 65 | + unlink(sys_get_temp_dir() . "/foo"); |
| 66 | + } |
| 67 | + |
| 68 | + function it_convert_callback_string() |
| 69 | + { |
| 70 | + $func = function() {return 'test';}; |
| 71 | + $this->beConstructedWith($func); |
| 72 | + $this->__toString()->shouldReturn('\Closure'); |
| 73 | + } |
| 74 | +} |
0 commit comments