allantatter's avatar

phpspec: How to test if native function was called?

How to test if file_get_content() was called with right arguments if we had this example class:

<?php namespace Some\Dir;

class Example {

    public function method1($path)
    {
        return $this->method2($path);
    }

    private function method2($path)
    {
        return file_get_contents($path);
    }

}

My actual code is a lot more complicated but this is the general idea, that I call a public method that calls a private method that calls file_get_contents().

Thanks in advance.

0 likes
4 replies
davorminchorov's avatar

Test public methods only. You don't really have to test private methods.

allantatter's avatar

The thing is that I don't want my test to know the exact implementation of the class and what happens in the middle. The only thing I want to test is when I call (new Example)-> method1() that in the end, my Example class will try to make a request to remote API with correct arguments.

Maybe I'm on the wrong track.

Any advice?

davorminchorov's avatar

Write the test like you said, "when I call [method name], I expect it to return [data type] or whatever I expect it to return".

    $this->shouldBeCalled($this->example->method1('path/to/file')->shouldReturn(true); // I don't know if method2 will return true if everything goes as planned but it should give you an idea.  
allantatter's avatar

I'm not that sure that it will help to solve my problem. To be more concrete with my code, I just published a composer package for Laravel, https://github.com/allantatter/laravel-react.

I want to test that when {!! React::component('ComponentName', $props) !!} gets called from Blade templates, it always ends up with the same GET request.

Please or to participate in this conversation.