So I have an XML "view" I want to test using mocked data in php unit. I basically have this:
public function setUp() {
parent::setUp();
//output is an array of mocked data, assume that it's populated
//"homepage" is a blade template that outputs xml
$this->xml = view("homepage", $this->output)->render();
}
then I have a suite of tests, for example this one, that verifies that the xml contains what it should:
/**
* A basic test example.
*
* @return void
*/
public function testXMLHasTemplate()
{
$this->assertRegexp('/<template>/', $this->xml);
}
However, when I run this very basic test I get the following errors:
Fatal error: Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: syntax error, unexpected 'version' (T_STRING) (View: /Users/171599/Documents/php_projects/vmdisks/dlarvarwww/v4api/html/resources/views/homepage.blade.php) in /Users/171599/Documents/php_projects/vmdisks/dlarvarwww/v4api/html/vendor/phpunit/phpunit/src/Framework/Assert.php on line 0
This same view method works just fine when used from a controller. But it seems to not work from phpunit. Is there a way to generate a view within phpunit like you would within a controller?
edit: here is my blade "view" for reference. It's all XML:
homepage.blade.php:
{!! "<?xml version=\"1.0\" ?>" !!}
<homepage template="{{ $template }}">
@foreach ($entities as $entity)
@if ($entity["type"] == 'Section')
@include('partials.section', ["section" => $entity])
@elseif ($entity["type"] == 'SectionGroup')
<sectiongroup>
@each('partials.section', $entity['sections'], 'section')
</sectiongroup>
@endif
@endforeach
<user id="{{ $user["user_id"] }}" email="{{ $user["email"] }}" listening_time="{{ $user["listening_time"] }}"/>
</homepage>
section.blade.php:
<section name="{{ $section['name'] }}" type="{{ $section["section_type"] }}">
@foreach ($section["items"] as $item)
@switch($item["type"])
@case("feed")
<feed id="{{$item["id"]}}" name="{{$item["description"]}}" imageURL="{{ $item["imageURL"] }}">
@include("partials.episode", ["episode" => $item["latest_episode"] ])
</feed>
@break
@case("episode")
@include("partials.episode", ["episode" => $item])
@break
@case("shortcut")
<shortcut id="{{ $item['id'] }}" name="{{ $item['name'] }}" />
@break
@endswitch
@endforeach
</section>