working on it ...

Filters

Explore Public Snippets

Sort by

Found 324 snippets matching: phpunit

    public by maholtz  3398  1  6  1

    Windows Batch Script zum ausführen von PHPUnit

    Script zum erzeugen von PHPUnit. Die phpunit.phar muss im gleichen Verzeichnis liegen. Man kann festlegen ob nur eine bestimmte Datei oder ggf. ein Filter für bestimmte Testmethoden ausgeführt wird. Am Ende kann man den gleichen Test mit den selben Parametern erneut durchführen.
    @echo off
    REM Diese Datei führt die Unittests im lokalen Verzeichnis durch.
    REM
    REM Der Befehl "%~dp0" wechselt in den Ordner des Batch-Skripts. (sinnvoll bei Windows-Doppelklicks)
    
    rem # %~dp0 -> DirectoryPath ~dp0..
    @echo Unittest starten....
    
    SET testfile=%~dp0
    
    CHOICE /C NJ /M "Code Coverage Analyse durchfuehren (dauert laenger)?"
    IF ERRORLEVEL 2 (
       SET coverage= -v --testdox-html agile.html --testdox-text agile.txt --coverage-html coverage/
       GOTO start
    )
    
    SET /P testfile=Nur Bestimmte Datei testen? (Default = leer):
    IF NOT "%test%" == "" (
    	SET testfile=%testfile%
    )
    
    SET /p filter=Nur bestimmte Methoden testen? (Wildcard: *):
    IF NOT "%filter%" == "" (
       SET filter= --filter %filter%
    ) else (
       SET filter=
    )
    
    
    :start
    @echo phpunit-4.1.1.phar %filter% %coverage% --static-backup  --strict %testfile%
    php phpunit-4.1.1.phar %filter% %coverage% --static-backup  --strict %testfile%
    pause
    
    CHOICE /C NJ /M "Den selben Unittest noch einmal durchfuehren?"
    IF ERRORLEVEL 2 (
       goto start
    )
    
    :end
    @echo "Ende"
    
    CHOICE /C NJ /M "PHP Unit self-update durchfuehren?"
    IF ERRORLEVEL 2 (
       php phpunit-4.1.1.phar --self-update
       @echo Selbstupdate durchgefuehrt
       pause
    )
    

    public by jlappano  3241  1  5  2

    Check Content and Status Code in PHP Unit

            $client->request('GET', ‘/create/adviseeSet’);
    
            $content = $client->getResponse()->getContent();
            $status = $client->getResponse()->getStatusCode();
    
            $this->assertEquals('shmee', $content);
            $this->assertEquals(200, $status);
    
    
    Gives a nice failure message which prints out content and status code 

    public by arsnie  1181  0  4  0

    Composer for Wordpress

    wordpress (4.5.1), phpunit, goutte
    {
      "repositories": [
        {
          "type": "git",
          "url": "git@bitbucket.org:digitalwert/phpunit-webtest.git"
        }
      ],
      "config": {
        "vendor-dir": "vendor"
      },
      "require":{
        "php": ">=5.5",
        "johnpbloch/wordpress": "4.5.1"
      },
      "extra":{
        "wordpress-install-dir": "htdocs"
      },
      "require-dev": {
        "phpunit/phpunit": "3.7.*",
        "fabpot/goutte": "2.*"
      }
    }            

    external by Dennis Meckel  525  0  3  0

    Basic FatFreeFramework/Base->mock() + PHPUnit example

    Basic FatFreeFramework/Base-mock() + PHPUnit example: F3PHPUnitTest.php
    <?php
    
    class F3PHPUnitTest extends PHPUnit_Framework_TestCase {
    	public function test() {
    		$f3 = Base::instance();
    
    		// Don't write to STDOUT
    		$f3->set('QUIET', true);
    
    		$f3->route('GET /path', function(){ echo 'TEXT'; });
    
    		$this->assertNull($f3->mock('GET /path'));
    		$this->assertSame('TEXT', $f3->get('RESPONSE'));
    	}
    }
    
    

    external by Jason Bradley  11  0  1  0

    Run PHP Unit tests bash script. Looks for phpunit command availability or phpunit.phar in the same location. It will download the phar file if neither is found.

    Run PHP Unit tests bash script. Looks for phpunit command availability or phpunit.phar in the same location. It will download the phar file if neither is found.: run-tests
    #!/bin/bash
    
    PHPUNIT_COMMAND=""
    MEMORY_LIMIT=512M
    PHPUNIT_VERSION="4.1.3"
    
    if hash phpunit 2>/dev/null;
    then
        PHPUNIT_COMMAND="phpunit"
    elif [ -e phpunit.phar ]
    then
        PHPUNIT_COMMAND="phpunit.phar"
    else
        wget -qO phpunit.phar https://phar.phpunit.de/phpunit-"$PHPUNIT_VERSION".phar --no-check-certificate
        PHPUNIT_COMMAND="phpunit.phar"
    fi
    
    
    if [ ! -z "$PHPUNIT_COMMAND" ]
    then
        if [ "phpunit.phar" = "$PHPUNIT_COMMAND" ]
        then
            php "$PHPUNIT_COMMAND" -d memory_limit="$MEMORY_LIMIT" -c app $*
        else
            "$PHPUNIT_COMMAND" -d memory_limit="$MEMORY_LIMIT" -c app $*
        fi
    else
    	echo "PHPUNIT/PHPUNIT.PHAR NOT FOUND."
    fi
    
    

    external by mwr  951  0  3  0

    PHPUnit Parameter Validation using Callback (phpunit 4.1.0)

    PHPUnit Parameter Validation using Callback (phpunit 4.1.0): phpunit_parameter_validation_callback.php
    <?php
    // Validation Callback
    $validator1 = new \PHPUnit_Framework_Constraint_Callback(
        function (/** @todo add parameter **/) {
            // @todo add assertions here
            return true;
        }
    );
    
    $validator2 = new \PHPUnit_Framework_Constraint_Callback(
        function (/** @todo add parameter **/) {
            // @todo add assertions here
            return true;
        }
    );
    
    // 
    $this->mock->expects($this->once())->method('$METHODNAME')->with($validator1, $validator2);
    
    

    external by Sebastian Knuell  87  1  1  0

    Mocking native PHP functions (date(), time() and shell_exec) for PHPUnit using php-mock/php-mock and php-mock/php-mock-phpunit

    Mocking native PHP functions (date(), time() and shell_exec) for PHPUnit using php-mock/php-mock and php-mock/php-mock-phpunit: PHPNativeFunctionMock.php
    <?php
    namespace Foo\Bar;
    
    use phpmock\MockBuilder;
    use phpmock\phpunit\PHPMock;
    use PHPUnit_Framework_TestCase;
    
    class Baz
    {
        /**
         * @return string
         */
        public function getDate(): string
        {
            return date('Y-m-d H:i:s');
        }
    
        /**
         * @return int
         */
        public function getTime(): int
        {
            return time();
        }
    
        /**
         * @return string
         */
        public function exec(): string
        {
            return shell_exec('cmd.sh');
        }
    }
    
    class BazTest extends PHPUnit_Framework_TestCase
    {
        use PHPMock;
    
        /**
         * @var Baz
         */
        private $baz;
    
        /**
         * Mocking date() and time() methods for _all_ test cases
         */
        public static function setUpBeforeClass()
        {
            parent::setUpBeforeClass();
    
            $builder = new MockBuilder();
            $builder->setNamespace(__NAMESPACE__);
    
            $builder->setName('date')
                ->setFunction(
                    function() {
                        return '2016-12-30 01:00:00';
                    }
                );
            $dateMock = $builder->build();
            $dateMock->enable();
    
            $builder->setName('time')
                ->setFunction(
                    function() {
                        return strtotime('2016-12-30 01:00:00');
                    }
                );
            $timeMock = $builder->build();
            $timeMock->enable();
        }
    
        protected function setUp()
        {
            parent::setUp();
    
            $this->baz = new Baz();
        }
    
        public function testGetDate()
        {
            $this->assertSame('2016-12-30 01:00:00', $this->baz->getDate());
        }
    
        public function testGetTime()
        {
            $this->assertSame(strtotime('2016-12-30 01:00:00'), $this->baz->getTime());
        }
    
        public function testExec()
        {
            // mocking shell_exec for selected test case using getFunctionsMock()
            $mock = $this->getFunctionMock(__NAMESPACE__, 'shell_exec');
            $mock->expects($this->once())->with('cmd.sh')->willReturn('output');
    
            $this->assertSame('output', $this->baz->exec());
        }
    }
    
    
    

    external by Digi92  186  0  2  0

    PHPUnit Test Fail with "PHPUnit_Framework_Exception: PHP Fatal error: Call to undefined method Composer\Autoload\ClassLoader::setPsr4()"

    PHPUnit Test Fail with "PHPUnit_Framework_Exception: PHP Fatal error: Call to undefined method Composer\Autoload\ClassLoader::setPsr4()": PHPUnit_Exception_setPsr4
    Wenn die Funktionalen Test fehlschlagen liegt das im TYPO3 an der Extension "phpunit".
    1. Gehe in den Ordner "/htdocs/typo3conf/ext/phpunit/Composer/"
    2. Führe den Befehl "composer dump-autoload" aus.
    Jetzt sollte es funktionieren!
    
    

    external by albertpark  139  0  2  0

    Parse JUnit xml log generated by PHPUnit (phpunit ---log-junit build/logs/junit.xml) and print elapsed time each test case. If you want to color time at certain threshold time, get ColorCLI class (see https://gist.github.com/albertpark/985d39b759f356cd...

    Parse JUnit xml log generated by PHPUnit (phpunit ---log-junit build/logs/junit.xml) and print elapsed time each test case. If you want to color time at certain threshold time, get ColorCLI class (see https://gist.github.com/albertpark/985d39b759f356cdbbeb).: junit.php
    <?php
    
    function run($path) {
        $xml = simplexml_load_file($path);
        $project = $xml->testsuite;
        
        echo sprintf("total:    %s msec", formatMsec($project['time'])) . PHP_EOL;
        
        foreach ($project->testsuite as $testsuite) {
            echo sprintf("  suite:  %s msec : %s", formatMsec($testsuite['time']), $testsuite['name']) . PHP_EOL;
            
            foreach ($testsuite->testcase as $testcase) {
                echo sprintf("    case: %s msec :   %s", printMsec($testcase['time']), $testcase['name']) . PHP_EOL;
            }
        }
        
        return 0;
    }
    
    function msec($str) {
        return floatval((string) $str) * 1000;
    }
    
    function formatMsec($time) {
        return sprintf('%9.3f', msec($time));
    }
    
    function printMsec($time, $warn = 5, $error = 10) {
        $str = formatMsec($time);
        
        if (!class_exists('ColorCLI')) {
            return $str;
        }
        
        $msec = msec($time);
        
        if ($msec < $warn) {
            return ColorCLI::lightGreen($str);
        } elseif ($msec < $error) {
            return ColorCLI::yellow($str);
        }
        
        return ColorCLI::red($str);
    }
    
    $colorCli = realpath(__DIR__ . '/ColorCLI.php');
    
    if (file_exists($colorCli)) {
        include_once $colorCli;
    }
    
    $xmlFileName = "junit.xml";
    $root = realpath(__DIR__ . "/..");
    $path = realpath("$root/build/logs/$xmlFileName");
    
    if ($path === false || !file_exists($path)) {
        die("Not found $xmlFileName");
    }
    
    return run($path);
    
    

    external by eviljay  56  0  1  0

    This is an PHPUnit code snippet that we used for our blog post https://rubygarage.org/blog/ruby-on-rails-and-symfony-comparison at RubyGarage. It demonstrates how to create a namespace with PHPUnit. This code snippet was taken from the Symfony main web...

    This is an PHPUnit code snippet that we used for our blog post https://rubygarage.org/blog/ruby-on-rails-and-symfony-comparison at RubyGarage. It demonstrates how to create a namespace with PHPUnit. This code snippet was taken from the Symfony main website.: how_to_create_a_namespace_with_phpunit.php
    <?php
    // tests/AppBundle/Util/CalculatorTest.php
    namespace Tests\AppBundle\Util;
    
    use AppBundle\Util\Calculator;
    
    class CalculatorTest extends \PHPUnit_Framework_TestCase
    {
      public function testAdd()
      {
        $calc = new Calculator();
        $result = $calc->add(30, 12);
        //assert that your calculator added the numbers correctly!
        $this->assertEquals(42, $result);
      }
    }
    ?>
    
    
    • Public Snippets
    • Channels Snippets