title: Unit testing - Parsing a yaml config file with file_get_contents and symfony yaml parser
body: <p>I want to load the content of a yaml config file. Using Symfony Yaml parser, and according to <a href="http://symfony.com/doc/current/components/yaml/introduction.html#reading-yaml-files" rel="nofollow">their documentation</a>, I have a <code>parse()</code> method working like this:</p>
  
  <pre><code>    public function parse($filePath)
      {
          $fileAsString = file_get_contents($filePath);
  
          if (false === $fileAsString) {
              throw new \Exception('Failed to get content of config file.');
          }
  
          return $this-&gt;parser-&gt;parse($fileAsString);
      }
  </code></pre>
  
  <p>When testing this method with a virtual file system <a href="https://github.com/mikey179/vfsStream" rel="nofollow">vfsStream</a> I noticed that <code>file_get_contents()</code> does not only returns false in case of failure but also throws what looks like an error:</p>
  
  <pre><code>    public function parse_throws_Exception_for_invalid_filePath()
      {
          // Setup a root directory
          $root = vfsStream::setup();
  
          // Set a path of a non existing file
          $filePath = $root-&gt;url() . '/file';
  
          // Create a config mock
          $configMock = $this-&gt;getMockBuilder('Microblog\Config')
              -&gt;disableOriginalConstructor()
              -&gt;setMethods(null)
              -&gt;getMock();
  
          $configMock-&gt;parse($filePath);
      }
  </code></pre>
  
  <p>I also noticed that to be more solid, the <code>parse</code> method needed also to take into account the case of an invalide yaml file, which got me to this change in the <code>parse</code> method:</p>
  
  <pre><code>    public function parse($filePath)
      {
          try {
              return $this-&gt;parser-&gt;parse(file_get_contents($filePath));
          } 
  
          catch (\Exception $e) {
              // Catch exception from file_get_contents and yaml parser
              throw new \Exception('Failed to get config file ("'. $e-&gt;getMessage() . '").');
          }
  
          catch (Symfony\Component\Yaml\Exception\ParseException $e) {
              // Catch exception from file_get_contents and yaml parser
              throw new \Exception('Failed to parse config file ("'. $e-&gt;getMessage() . '").');
          }
      }
  </code></pre>
  
  <p>But this does not seem to catch any of the exceptions... :(</p>
  
  <p>I've also read that a good alternative to <code>file_get_contents()</code> is <code>cURL</code> but the config file being a file on the same filesystem, <code>cURL</code> does not really seem to be what I need. Any ideas?</p>