title: Symfony 5.2 - How to test a class with a dependency on the cache interface
body: <p>I'm using a Symfony 5.2 installation installed as a skeleton project and running on PHP 7.4:latest</p>
  <p>I am using <code>Symfony\Contracts\Cache\TagAwareCacheInterface</code> as an auto-wired constructor injected dependency in a class.</p>
  <p>I want to cache the open id response I fetch from the configuration endpoint:</p>
  <pre><code>$config = $this-&gt;cachePool-&gt;get(
      'openid-config',
      function (ItemInterface $item) {
          $item-&gt;expiresAfter(self::CACHE_TIME_SECONDS);
          $webContent = file_get_contents($this-&gt;openIdConfigurationUrl);
          if (false === $webContent) {
              $message = &quot;Failed to fetch openid config from web at [{$this-&gt;openIdConfigurationUrl}]&quot;;
              $this-&gt;logger-&gt;error($message);
              throw new CannotReadOpenIdConfigException($message);
          }
          return $webContent;
      }
  );
  </code></pre>
  <p>I want to unit test my class.</p>
  <p>The <a href="https://symfony.com/doc/current/cache.html" rel="nofollow noreferrer">Symfony documentation</a> does not provide an alternative to the callable syntax above.</p>
  <p>I have tried to create a mock of the cache using a mock builder:</p>
  <pre><code>return $this-&gt;getMockBuilder('Symfony\Contracts\Cache\TagAwareCacheInterface')
              -&gt;disableOriginalConstructor()
              -&gt;getMock();
  </code></pre>
  <p>I can't find a way to get this to execute the callable in my object under test.</p>
  <p>So, I tried using an actual liveCacheClient instead of mocking this dependency.  I could control its contents by adding or removing keys in my test code (icky, but I can't get mocking working)</p>
  <pre><code>$liveCacheClient = (self::$container)-&gt;get(TagAwareCacheInterface::class);
  $liveCacheClient-&gt;clear();
  </code></pre>
  <p>If I try to use the container to fetch a real instance of the cache then I get the error that Symfony has removed the class from the container:</p>
  <pre><code>Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The &quot;Symfony\Contracts\Cache\TagAwareCacheInterface&quot; service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.
  </code></pre>
  <p>So, what is the correct recipe to properly unit test code in Symfony cache?</p>