[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/tests/ProxyManagerTest/GeneratorStrategy/ -> FileWriterGeneratorStrategyTest.php (source)

   1  <?php
   2  /*
   3   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   4   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   5   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   6   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   7   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   8   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   9   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14   *
  15   * This software consists of voluntary contributions made by many individuals
  16   * and is licensed under the MIT license.
  17   */
  18  
  19  namespace ProxyManagerTest\GeneratorStrategy;
  20  
  21  use PHPUnit_Framework_TestCase;
  22  use ProxyManager\Exception\FileNotWritableException;
  23  use ProxyManager\Generator\ClassGenerator;
  24  use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
  25  use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
  26  
  27  /**
  28   * Tests for {@see \ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy}
  29   *
  30   * @author Marco Pivetta <ocramius@gmail.com>
  31   * @license MIT
  32   *
  33   * @group Coverage
  34   *
  35   * Note: this test generates temporary files that are not deleted
  36   */
  37  class FileWriterGeneratorStrategyTest extends PHPUnit_Framework_TestCase
  38  {
  39      /**
  40       * @covers \ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy::__construct
  41       * @covers \ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy::generate
  42       */
  43      public function testGenerate()
  44      {
  45          /* @var $locator \ProxyManager\FileLocator\FileLocatorInterface|\PHPUnit_Framework_MockObject_MockObject */
  46          $locator   = $this->getMock('ProxyManager\\FileLocator\\FileLocatorInterface');
  47          $generator = new FileWriterGeneratorStrategy($locator);
  48          $tmpFile   = sys_get_temp_dir() . '/' . uniqid('FileWriterGeneratorStrategyTest', true) . '.php';
  49          $namespace = 'Foo';
  50          $className = UniqueIdentifierGenerator::getIdentifier('Bar');
  51          $fqcn      = $namespace . '\\' . $className;
  52  
  53          $locator
  54              ->expects($this->any())
  55              ->method('getProxyFileName')
  56              ->with($fqcn)
  57              ->will($this->returnValue($tmpFile));
  58  
  59          $body = $generator->generate(new ClassGenerator($fqcn));
  60  
  61          $this->assertGreaterThan(0, strpos($body, $className));
  62          $this->assertFalse(class_exists($fqcn, false));
  63          $this->assertTrue(file_exists($tmpFile));
  64  
  65          require $tmpFile;
  66  
  67          $this->assertTrue(class_exists($fqcn, false));
  68      }
  69  
  70      public function testGenerateWillFailIfTmpFileCannotBeWrittenToDisk()
  71      {
  72          $tmpDirPath = sys_get_temp_dir() . '/' . uniqid('nonWritable', true);
  73  
  74          mkdir($tmpDirPath, 0555, true);
  75  
  76          /* @var $locator \ProxyManager\FileLocator\FileLocatorInterface|\PHPUnit_Framework_MockObject_MockObject */
  77          $locator   = $this->getMock('ProxyManager\\FileLocator\\FileLocatorInterface');
  78          $generator = new FileWriterGeneratorStrategy($locator);
  79          $tmpFile   = $tmpDirPath . '/' . uniqid('FileWriterGeneratorStrategyFailedFileWriteTest', true) . '.php';
  80          $namespace = 'Foo';
  81          $className = UniqueIdentifierGenerator::getIdentifier('Bar');
  82          $fqcn      = $namespace . '\\' . $className;
  83  
  84          $locator
  85              ->expects($this->any())
  86              ->method('getProxyFileName')
  87              ->with($fqcn)
  88              ->will($this->returnValue($tmpFile));
  89  
  90          $this->setExpectedException('ProxyManager\\Exception\\FileNotWritableException');
  91          $generator->generate(new ClassGenerator($fqcn));
  92      }
  93  
  94      public function testGenerateWillFailIfTmpFileCannotBeMovedToFinalDestination()
  95      {
  96          /* @var $locator \ProxyManager\FileLocator\FileLocatorInterface|\PHPUnit_Framework_MockObject_MockObject */
  97          $locator   = $this->getMock('ProxyManager\\FileLocator\\FileLocatorInterface');
  98          $generator = new FileWriterGeneratorStrategy($locator);
  99          $tmpFile   = sys_get_temp_dir() . '/' . uniqid('FileWriterGeneratorStrategyFailedFileMoveTest', true) . '.php';
 100          $namespace = 'Foo';
 101          $className = UniqueIdentifierGenerator::getIdentifier('Bar');
 102          $fqcn      = $namespace . '\\' . $className;
 103  
 104          $locator
 105              ->expects($this->any())
 106              ->method('getProxyFileName')
 107              ->with($fqcn)
 108              ->will($this->returnValue($tmpFile));
 109  
 110          mkdir($tmpFile);
 111  
 112          $this->setExpectedException('ProxyManager\\Exception\\FileNotWritableException');
 113          $generator->generate(new ClassGenerator($fqcn));
 114      }
 115  
 116      public function testWhenFailingAllTemporaryFilesAreRemoved()
 117      {
 118          $tmpDirPath = sys_get_temp_dir() . '/' . uniqid('noTempFilesLeftBehind', true);
 119  
 120          mkdir($tmpDirPath);
 121  
 122          /* @var $locator \ProxyManager\FileLocator\FileLocatorInterface|\PHPUnit_Framework_MockObject_MockObject */
 123          $locator   = $this->getMock('ProxyManager\\FileLocator\\FileLocatorInterface');
 124          $generator = new FileWriterGeneratorStrategy($locator);
 125          $tmpFile   = $tmpDirPath . '/' . uniqid('FileWriterGeneratorStrategyFailedFileMoveTest', true) . '.php';
 126          $namespace = 'Foo';
 127          $className = UniqueIdentifierGenerator::getIdentifier('Bar');
 128          $fqcn      = $namespace . '\\' . $className;
 129  
 130          $locator
 131              ->expects($this->any())
 132              ->method('getProxyFileName')
 133              ->with($fqcn)
 134              ->will($this->returnValue($tmpFile));
 135  
 136          mkdir($tmpFile);
 137  
 138          try {
 139              $generator->generate(new ClassGenerator($fqcn));
 140  
 141              $this->fail('An exception was supposed to be thrown');
 142          } catch (FileNotWritableException $exception) {
 143              rmdir($tmpFile);
 144  
 145              $this->assertEquals(array('.', '..'), scandir($tmpDirPath));
 146          }
 147      }
 148  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1