-
-
Save tmli3b3rm4n/0e69da02565bccbfe3fb835ba3d3f7d8 to your computer and use it in GitHub Desktop.
A few examples how to use PHPUnit.
Covering Mock Objects, setUp Method, MockBuilder, @ExpectedException, @dataProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Silpion\ExampleBundle\Tests\Listener\Serialization; | |
use Silpion\ExampleBundle\Entity\TextBlock; | |
use Silpion\ExampleBundle\Listener\Serialization\SerializationListener; | |
class SerializationListenerTest extends \PHPUnit_Framework_TestCase | |
{ | |
/** @var SerializationListener */ | |
protected $listener; | |
/** @var \PHPUnit_Framework_MockObject_MockObject */ | |
protected $visitorMock; | |
protected function setUp() { | |
$this->visitorMock = $this->getMockBuilder('\JMS\Serializer\JsonSerializationVisitor') | |
->disableOriginalConstructor() | |
->setMethods(array('addData')) | |
->getMock(); | |
$this->listener = new SerializationListener($this->solverMock); | |
} | |
public function testGetSubscribedEvents() { | |
$this->assertEquals( | |
array( | |
array( | |
'event' => 'serializer.post_serialize', | |
'class' => 'Silpion\ExampleBundle\Entity\TextBlock', | |
'method' => 'onPostSerialize' | |
), | |
), | |
$this->listener->getSubscribedEvents() | |
); | |
} | |
public function testOnPostSerialize() { | |
$textBlock = new TextBlock(); | |
$textBlock->setLanguage('de', 'Foo'); | |
$this->visitorMock->expects($this->once()) | |
->method('addData') | |
->with('translations', array(42)); | |
$this->listener->onPostSerialize($this->visitorMock); | |
} | |
/** | |
* @expectedException \InvalidArgumentException | |
* @expectedExceptionMessage Invalid Foo | |
*/ | |
public function testInvalidFoo() { | |
new SerializationListener(null); | |
} | |
/** | |
* This method will use translationsData() to get the Arguments for this test Method. | |
* | |
* @dataProvider translationsData | |
*/ | |
public function testSolveTranslation($langs, $input, $expected) { | |
$solver = new TranslationsSolver($langs); | |
$translations = $solver->calculateTranslations($input); | |
$this->assertEquals($expected, $translations); | |
} | |
public function translationsData() { | |
return array( | |
// One language, no fallback. | |
array('arg1', 'arg2', 'arg3'), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment