0
votes

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>
1
why do you have views in your unit test using a backend language, sound very wrong - meda
Because the "view" is just some xml output that I want to test. It's not really an html view, so I don't see the problem? - janedoe
then why not read the file directly, instead of using view method, its clearly meant for blade views - meda
because i'm using blade syntax in there to process the data in $output to generate the xml. i can't just "read" the file.... - janedoe
I feel like you should write a helper that can take some input and outputs the xml. I also recommend using a PHP xml library to build your xml. This way you can unit test that helper without messing with views. - meda

1 Answers

0
votes

The error tells you there is an error in views/homepage.blade.php - the string version is present when it is not expected to be. Look into what is happening in that file.