🔏
Knowledge
  • README
  • Business
    • Business
    • Starten
  • Dev
    • Apis
    • Blockchain
    • Composer
    • Css
    • Dev
    • Github
    • Hacking
    • Html
    • Javascript
      • Javascript
      • Vuejs
        • Dynamic-components
    • Linux
      • Bash
    • Markdown
    • Open-source
      • Open-source
    • Performance
    • Php
      • Array
      • Filesystem
      • Laravel
        • Create-model
        • Deploy
        • Laravel
        • Package-development
        • Queue
        • Tricks
        • Updates
          • 7
          • 8
      • Libraries
      • Php
      • Phpunit
      • Serialize
    • Programmieren
    • Regular-expressions
    • Security
    • Sql
    • Ssh
      • Git
        • Git
        • Reset-repository
    • Tools
      • Sublime-text
      • Tools
    • Web
      • Mockups
      • Web
      • Webspace
        • Namecheap
        • Uberspace
        • Uberspace
          • Domain
          • Laravel-deployment
          • Mail
    • Zip
  • Energy
    • Biogas
    • Energie
    • Solar
  • Garten
    • Anlegen
    • Boden
    • Garten
    • Gemeinschaftsgarten
    • Jahr
      • 1-januar
    • Pflanzen
  • Gesellschaft
    • Arbeit
    • Bildung
    • Finanzen
    • Gesellschaft
    • Landschaft
    • Nachbarschaft
    • Nachhaltigkeit
    • Nachrichten
    • Politik
    • Start
    • Steuern
    • Transport
    • Ueberfluss
    • Wirtschaft
  • Hof
    • Heizen
    • Trocknung
    • Ziel
  • Home-automation
    • 3d-printing
    • Architecture
    • Elektronik
    • Esp
    • Esphome
    • Home-assistant
    • Home-automation
    • Shelly
    • Wechselschaltung
  • Ideen
    • Dein-garten
    • Fitnessstudio
    • Kurse-fuer-fortgeschrittene
    • Lieferdienst
    • Life-as-a-service
    • Waldgarten-als-plattform
  • Landwirtschaft
    • Kompost
    • Landwirtschaft
    • Produktion
    • Umsetzung
  • Leben
    • Artikel
    • Beruf
    • Bestimmung
    • Beziehungen
    • Buecher
    • Entscheidungen
    • Erfolg
    • Erziehung
    • Finanzen
      • Etf
      • Finanzen
      • Finanzielle-unabhaengigkeit
      • Investments
      • Struktur
    • Firmen
    • Gamification
    • Gebaeude
    • Geist
      • Gedanken
      • Spiritualität
    • Gesundheit
      • Abnehmen
      • Beschwerden
      • Erholung
      • Ernaehrung
      • Ernaehrungsplan
      • Fitness
      • Gesundheit
      • Muskelaufbau
      • Psyche
      • Schlaf
      • Trainingsplan
    • Gewohnheiten
    • Kleidung
    • Kommunikation
    • Leben
    • Lebensweg
    • Lernen
    • Lesen
    • Mental-models
    • Minimalismus
    • Probleme_loesen
    • Quantified-self
    • Readme
    • Reisen
    • Schreiben
    • Start
    • Struktur
    • Tagebuch
    • Tagesablauf
    • Themes
    • Verbesserung
    • Wohnen
    • Ziele
    • Zitate
    • Zufriedenheit
  • Pc
    • Apps
    • Backup
    • Dateistruktur
      • Lokal
      • Onedrive
      • Apps
        • Macpass
    • MacOS
      • Dotfiles
      • Macos
      • Shortcuts
    • Netzwerk
    • Shortcuts
    • Windows
  • Produktivitaet
    • Aufgaben
    • Automatisierung
    • Ideen
    • Machen
    • Planung
      • Jahr
      • Monat
      • Planung
      • Tag
      • Woche
    • Produktivitaet
    • Prokrastination
    • 2021
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 49
      • 50
      • 51
      • 52
    • 2022
      • 01
      • 02
      • 03
      • 04
      • 05
      • 06
      • 08
      • 09
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 26
      • 28
      • 29
      • 32
      • 38
      • 39
      • 43
      • 45
      • 48
      • 51
    • 2023
      • 01
      • 03
      • 04
      • 05
      • 06
      • 07
      • 08
      • 09
      • 10
      • 11
      • 13
      • 16
      • 18
      • 52
  • Read
    • Reading
  • Sonstiges
    • Awesome
    • Diy
    • Geschichte
    • Haustiere
    • Rss
    • Schrauben
    • Sonstiges
    • Statistik
    • Wiki-workflow
    • Zeichnen
  • Zukunft
    • Serie
    • Zukunft
Powered by GitBook
On this page
  • XML Configuration File
  • Packages
  • Skipping, Incomplete
  • Mocking
  • Stubs
  • Mocking
  • Hard Dependencys

Was this helpful?

  1. Dev
  2. Php

Phpunit

PreviousPhpNextSerialize

Last updated 4 years ago

Was this helpful?

XML Configuration File

  • phpunit.xml -> nicht in git; persönliche Variablen speichern

  • phpunit.xml.dist -> in git

<php>
    <env name="foo" value="bar" force="true"/>
</php>

Packages

  • - für API tests

Skipping, Incomplete

$this->markTestSkipped('reason');
$this->markTestIncomplete('reason');

Mocking

Stubs

<?php
class SomeClass
{
    public function doSomething()
    {
        // Do something.
    }
}
<?php
use PHPUnit\Framework\TestCase;

class StubTest extends TestCase
{
    public function testStub()
    {
        // Create a stub for the SomeClass class.
        $stub = $this->createStub(SomeClass::class);

        // Configure the stub.
        $stub->method('doSomething')
             ->willReturn('foo');

        // Calling $stub->doSomething() will now return
        // 'foo'.
        $this->assertSame('foo', $stub->doSomething());
    }
}

Mocking

<?php
use PHPUnit\Framework\TestCase;

class SubjectTest extends TestCase
{
    public function testObserversAreUpdated()
    {
        // Create a mock for the Observer class,
        // only mock the update() method.
        $observer = $this->createMock(Observer::class);

        // Set up the expectation for the update() method
        // to be called only once and with the string 'something'
        // as its parameter.
        $observer->expects($this->once())
                 ->method('update')
                 ->with($this->equalTo('something'));

        // Create a Subject object and attach the mocked
        // Observer object to it.
        $subject = new Subject('My subject');
        $subject->attach($observer);

        // Call the doSomething() method on the $subject object
        // which we expect to call the mocked Observer object's
        // update() method with the string 'something'.
        $subject->doSomething();
    }
}

Hard Dependencys

<?php
namespace AppTest;
use Mockery as m;
class ServiceTest extends \PHPUnit_Framework_TestCase
{
    public function testCallingExternalService()
    {
        $param = 'Testing';

        $externalMock = m::mock('overload:App\Service\External');
        $externalMock->shouldReceive('sendSomething')
            ->once()
            ->with($param);
        $externalMock->shouldReceive('getSomething')
            ->once()
            ->andReturn('Tested!');

        $service = new \App\Service();

        $result = $service->callExternalService($param);

        $this->assertSame('Tested!', $result);
    }
}

Wie mache ich das mit PHPUnit?

The practice of replacing an object with a test double that (optionally) returns configured return values is referred to as stubbing. -

The practice of replacing an object with a test double that verifies expectations, for instance asserting that a method has been called, is referred to as mocking. -

Wenn Klasse in einer anderen Funktion erstellt wird ( erforderlich)

Documentation
Snapshot testing with PHPUnit
Test Doubles
Laracasts: Fake it Till You Make it
PHPUnit
Mock Pbjects
Mockery