FAQ Database Discussion Community

php,phpunit

PHPUnit RabbitMQ: write test for create connection function

php,phpunit,rabbitmq
I'm facing the following problem. I've wrote a function that create a connection object (AMQPConnection) given the required parameters. Now I want to write the corresponding unit test. I just don't know how to do it without having the RabbitMQ broker running. Here is the function in question: public function...

Symfony2: changing the request class and updating the test environment

php,symfony2,request,phpunit

Stubbing a method in php

php,mocking,phpunit,stubbing
I have an issue where I don't know how to say, when x is called, it expects y to be called at least once and I don't care what y does, so long as its called. So if we look at this example: public static function createAction($params) {$postParams =...

Laravel 5.1 PHPUnit with “use DatabaseMigrations;” Produces Error when Accessing Test DB

php,laravel,phpunit,laravel-5.1
I'm just learning PHPUnit with Laravel 5.1. I'm using "use DatabaseMigrations" to migrate the test database for each test, which I set in my phpunit.xml: <php> ... <env name="DB_DATABASE" value="project_test"/> ... </php> I've setup a bunch of basic tests as I go along to check instantiation, factories, etc, but I...

How to setup a mocked WP_Query in a WP_UnitTestCase

php,wordpress,phpunit
How do I setup a mock global $wp_query for a unit test? I basically want to setup the query so that is_single returns true. Stubbing over the function with namespaces is not an option since it clobbers the function for all remaining tests.... PHPUnit: Calling a child method from abstract class constructor phpunit I'm seeing an unexpected (to me) behaviour with PHPUnit, is this a bug, or am I doing something wrong? Simplified test case: abstract class abstractSpeaker { public function __construct($param) { $this->setSpeaker($param); $this->getSpeaker()->speak(); //bad line, causes error$this->tellSpeakerToSpeak(); //this lines ok } abstract function setSpeaker($value); abstract function getSpeaker(); function tellSpeakerToSpeak() {... Why is PHPUnit still failing on SQL error, even though I have caught the exception php,database,phpunit I am trying to build a wrapper class that simplifies SQL access within my application (amongst other things). When testing my class with PHPUnit, a SQL error kills the test and spits out the error message to the console despite the fact that I have caught and handled the exception.... PHPUnit Mysteriously Fails On Object Constructor with require_once and$SERVER variable

php,netbeans,phpunit
I'm trying to wrap a test class around a pre-existing PHP class file that does not adhere to any PSR standard. The PHP object I'm trying to test has a constructor that accepts 1 argument. When prepping my object in setUp, my test correctly fails because there is a constructor...

PHPunit expectation on Mock object is not fulfilled

php,mocking,phpunit,expectations
Here is my class: public function __construct(Manager $moduleManager, Source\Yesno$yesNo) { $this->moduleManager =$moduleManager; $this->yesNo =$yesNo; } public function my1() { $this->moduleManager->isOutputEnabled('');$this->yesNo->toOptionArray(); } public function my2() { $this->moduleManager->isOutputEnabled('');$this->yesNo->toOptionArray(); } Here is my test: ... $this->observerMock =$this->getMock( 'path\to\Observer', null, [$this->moduleManagerMock,$this->yesNoMock], '', true ); ......

Symfony Functional Testing: how to understand why the test fails (with a 500 Error)

php,symfony2,phpunit,functional-testing

Unit Testing a PHP Script

php,unit-testing,phpunit
I have some php scripts that are run as cronjobs. No classes and only a few functions. Id like to test the script with PHPUnit to make sure everything is working but it appears I need to rewrite the php as a class, which I dont want to do (or...

PHPunit call magic methods

methods,mocking,phpunit,magic-methods
I have magic method getData. Is it possible to call following inside unit test: $this->attributeMock ->method('getData') ->with('additional_data') ->willReturn('some value');$this->attributeMock ->method('getData') ->with('is_default') ->willReturn('something'); When I do this, I got: Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'is_default' +'additional_data' Is there some another way?...

Mockery object argument validation issue

php,phpunit,mockery
Consider the example classes (apologies for it being so convoluted, but it's as slim as possible): class RecordLookup { private $records = [ 13 => 'foo', 42 => 'bar', ]; function __construct($id) { $this->record =$this->records[$id]; } public function getRecord() { return$this->record; } } class RecordPage { public function...

How to use group in phpunit?

zend-framework,phpunit
How to invoke phpunit for zendframework module group wise. I mean when you invoke phpunit then it invoke module1 first then module2 and so on.

Phpunit coverage: what is the difference between 'addUncoveredFilesFromWhitelist' and 'processUncoveredFilesFromWhitelist' options?

php,phpunit
I'm trying to set up code coverage for phpunit for a particular directory. Can someone tell me what is the difference between: <filter> <whitelist> <directory suffix=".php">lib/</directory> </whitelist> </filter> and <filter> <whitelist addUncoveredFilesFromWhitelist="true"> <directory suffix=".php">lib/</directory> </whitelist> </filter> and <filter> <whitelist processUncoveredFilesFromWhitelist="true"> <directory...

php,unit-testing,symfony2,phpunit
Just recently I got into testing pieces of a symfony application... For starters, I thought I would try and test a login form that should grant access to a private area. The users for this private area are correctly persisted to the database and have been tried on the development...

Can't run phpunit tests from command line

php,phpunit,laravel-5
I am trying to run unit tests in a new laravel 5 application, using the phpunit framework. In the root path of my laravel application I ru the following command: ./vendor/bin/phpunit /tests/ExampleTest.php And then I get the following message: You need to set up the project dependencies using the following...

assertEqual not works when testing equals of command output

php,unit-testing,symfony2,phpunit,command-line-interface
Here is what I output in my command: $output->writeln("\033[37;42m Translations from " .$input->getArgument('bundle') . " imported successfully! \033[0m"); Here is what I get in command line with trim func to delete spaces: trim($commandTester->getDisplay()) ..string(69) " Translations from AcmeDemoBundle imported successfully! " But when I run this:$this->assertEquals(" Translations from...

submit a form using ajax in functional test

php,ajax,symfony2,phpunit,functional-testing
I'm creating a functional test for the inscription part of my project and I need to know how to test it if the form needs to go in an ajax request, otherwise the server will always return an empty inscription form. Looks like the submit method doesn't take an argument...

Laravel 5 package exceptions not firing Hander's render method

php,phpunit,laravel-5

PHPunit no tests execute on Travis CI

php,phpunit,composer-php,travis-ci
I'm been following a PHPUnit tutorial for the first time and my tests run fine locally. However, when running my tests on Travis CI, no tests are executed and my build exits with 0. My directory structure and full code can be seen on the repo. Build log from Travis...

Testing PHPUnit to test a “__construct() must be an instance of ..” does not recognize exception

php,phpunit
I have the following code to test if the class constructor will fire the exception but PHPUnit test fail on it. I am trying to figure out what I am doing wrong. /** @test */ public function should_require_instance_of_uuid() { $this->setExpectedException('Exception');$id = new BusinessPartnerId; } PHPunit gives the following error:...

Argument 1 passed to Foo::__construct() must be an instance of Psr\Log\LoggerTrait, instance of Mock_Trait_LoggerTrait_65811a25_bdddd884 given

php,unit-testing,mocking,phpunit,trait
I'm trying to mock a simple trait with PHPUnit but it is resulting on a FatalError, it works if I remove the type hinting: Argument 1 passed to Foo::__construct() must be an instance of Psr\Log\LoggerTrait, instance of Mock_Trait_LoggerTrait_65811a25_bdddd884 given The class: use Psr\Log\LoggerTrait; class Foo { /** * @var LoggerTrait...

Laravel 5.1 Unable to Run Test on User Password Mutator

php,unit-testing,laravel,phpunit,laravel-5.1
I have a password mutator: /** * Mutator for setting the encryption on the user password. * * @param $password */ public function getPasswordAttribute($password) { $this->attributes[ 'password' ] = bcrypt($password); } That I'm trying to test: /** * A basic check of password mutator. * * @return void */ public...

mocking findOneBy“field” in doctrine2 with phpunit

php,unit-testing,doctrine2,mocking,phpunit

phpunit testing expectedException not working

php,exception,phpunit
I am trying to test my class for InvalidArgumentException but I get Tests\BarTest::should_receive_parameter Missing argument 1 for Itdc\Foo\Bar::__construct(), called in /mypath/foo/tests/BarTest.php on line 10 and defined This is the test (BarTest.php) file I use: <?php namespace Tests; use Itdc\Foo\Bar; class BarTest extends \PHPUnit_Framework_TestCase { /** @test */ public function should_receive_parameter()...

Behat and Symfony data fixtures

php,symfony2,phpunit,fixtures,behat
I'm wondering is it possible to use separate database for the Behat tests? is there any way to drop and truncate database and load data fixtures with it like you can do with PHPUnit?

Auth::attempt fails with PHPUnit TESTING

php,laravel,laravel-4,phpunit
I have a really strange behavior and I do not see where it could come from. I built an authentication system with Laravel documentation. When I save a User and try to login from the application, it works fine. However when I do the same thing in my AuthControllerTest, the...

Define type for PHPUnit's mock objects

php,phpunit,phpdoc
I was wondering if its possible to use phpdoc to define some object in specific scope (inside a method only) as PHPUni's Mock, so in that method i can take advantage of type-hints, such as ->expected, ->methods and so on, just like when you just create the mock without addressing...

Can't run tests provided in Slim Framework

php,xampp,phpunit,netbeans-8,slimframework
I'm trying to make a simple rest client for my android app. I would like to do than in TDD way, but for that I need simple working configuration for all actions (GET, POST, and so on). After some struggling I was able to make test work with get requests....

Laravel 5 Model mocking

php,phpunit,laravel-5,mockery
I have seen a few posts in various places about this and they all seem to have a similar answer. However for the life of me I cannot get the Mockery object working properly. The Attribute model looks like this namespace App\Models; use Illuminate\Database\Eloquent\Model; class Attribute extends Model { public...

Should I create extra functions to make my code testable?

php,unit-testing,testing,phpunit
I am creating an application which depending on the day that it runs, it creates different reports. So if it is Monday @ 1am, It will create a weekly report, if it is the 1st of the month @ 1.30am, it will create a monthly report. My initial approach uses...

Validating HTTP Response Codes in PHPUnit

php,unit-testing,phpunit
I am writing unit tests for several methods which return HTTP response codes. I cannot find a way to assert an HTTP response code. Perhaps I am missing something obvious, or I am misunderstanding something about PHPUnit. I am using PHPUnit 4.5 stable. Relevant part of class Message: public function...

Mocking Laravel Model::increment() with Mockery

php,laravel,phpunit,codeception,mockery

Any tool to help unit testing?

phpunit
Im using phpunit. But you know its difficulties, no possibility to mock private functions, to access private variables, etc. Is there a tool which help me? Something to turn private functions to public, turn static method to mockable, etc

Phpunit can't find abstract class

php,unit-testing,phpunit
For some reason when I try to test abstract class I get PHPUnit_Framework_MockObject_RuntimeException: Class "AbstractClass" does not exist. Here's the code AbstractClass.php <?php namespace SD\Project; abstract class AbstractClass { public function handle() { } } AbstractClassTest.php <?php require_once 'AbstractClass.php'; use SD\Project\AbstractClass; class AbstractClassTest extends PHPUnit_Framework_TestCase { public function testHandle() {...

PHPUnit mock using mock

php,dependency-injection,mocking,phpunit,unit-testing
I have two class, i want to test it via PHPUnit. But something i do wrongly at mocking the stuffs. I want to altering a method what called by the first class. class One { private $someVar = null; private$abc = null; public function Start() { if ( null...

Symfony2 : PHPUnit How to use 'OR' condition in an assertion?

php,unit-testing,symfony2,phpunit
Using Symfony2 with PHPUnit, how can I use a OR condition in an assertion? In my case a client request can return codes 200 OR 302, but the assertEquals expect only one possibility. Is there any way to throw an exception is the code is not 200 AND 302? private...

PHPUnit symfony2 faking parameters.yml

symfony2,phpunit
I need to test some functionality with PHPUnit. I got isolated database for running tests but still some functionality depends on the parameters.yml where I add user IDs or IP addresses. Is there any way of creating fake parameters.yml for tests?

phpunit and http content-type

rest,laravel,phpunit,dingo-api
I have an API built in Laravel (Dingo) and it works perfectly. However I have a problem with implementing phpunit to unit test my API class ProductControllerTest extends TestCase { public function testInsertProductCase() { $data = array( , "description" => "Expensive Pen" , "price" => 100 );$server = array();...

WP_UnitTestCase - how to configure the include_path correctly with phpunit PHAR

wordpress,phpunit,composer-php

Mocking or not registry-like object in phpunit tests

unit-testing,mocking,phpunit

phpUnit: Pass argument into tearDownAfterClass from a test

php,phpunit
I'm using phpUnit. Is it possible to pass a result from another test to the tearDownAfterClass. I know it's possible to use @depends to make other tests depend on a test for data. Is it possible to pass that data to tearDownAfterClass afterwards somehow. @depends doesn't seem to work for...

PHPUnit: Testing RestFul API with die response

php,json,api,zend-framework2,phpunit

PHPUnit: dataProvider not working in the sub classes of PHPUnit_Framework_TestCase

php,unit-testing,phpunit

Phpunit permission test

php,testing,phpunit
I have function that take a csv file and generate file content in array. I need to write a phpunit tests. Goal is represend correctness of behavior when input is not present or unreadable (whether because the file is malformed or due to the lack of sufficient access rights). I...

Unable to find wrapper when testing Guzzle call with PHPUnit

php,codeigniter,phpunit,guzzle
I am writing a unit test for an API that I am developing. The API is written in the Codeigniter framework, that calls another API using Guzzle. The test I am writing verifies that the API call returns the correct response. The Test.php file contains the following code require '/application/libraries/apiWrappers/Breathehr.php';...

Vagrant: executable files are not executable

laravel,phpunit,puppet,laravel-5

php,phpunit

How can I test a function that uses DateTime to get the current time?

unit-testing,symfony2,datetime,phpunit,mockery
Most of the answers I have seen on StackOverflow are without using the DateTime object, and are instead using the date() function. This makes them very dirty solutions (overriding date(), mocking a protected function of the subject under test, etc). Is there a way to mock DateTime, effectively mocking the...

Cakephp 3 - MissingDatasourceConfigException when running phpunit test

php,unit-testing,cakephp,phpunit,cakephp-3.0
I am trying to run some unit tests in CakePHP 3 with PHPUnit 4.7.3, but I`m getting the following error: PHPUnit 4.7.3 by Sebastian Bergmann and contributors. There was 1 error: 1) App\Test\TestCase\Model\Table\MoviesTableTest::testFindMoviesByGenre Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource configuration "default" was not found. C:\xampp\htdocs\movie-pal\vendor\cakephp\cakephp\src\Datasource\ConnectionManager.php:188 C:\xampp\htdocs\movie-pal\vendor\cakephp\cakephp\src\ORM\TableRegistry.php:191...

How to setup phpUnit in Atlassian Bamboo?

php,phpunit,bamboo
I am new to Atlassian Bamboo. I have my code on Atlassian Stash Repository, and I want to run PhpUnit test on my code using Bamboo plan. I have go through documentation but I am not able to create one. Does anyone knows about it?

Configuring PhPUnit.phar with Atlassian Bamboo

php,unit-testing,phpunit,bamboo,atlassian
I am trying to configure PHPUnit with bamboo. I have followed all the instruction available on Bamboo Documentation about Getting started with PHP and Bamboo accordingly but I am not able to get a successful build. Here are the screen shots of my configuration panel and Error log. My Task...

Cannot connect to DB when setting up Codeception

php,mysql,pdo,phpunit,codeception

Getting More Context Around PHPUnit Test Failures

php,phpunit,automated-tests
In PHPUnit, let's say I do this: $this->assertTrue($some_variable == "foo" ); When I run my tests with my code in a failing state, PHPUnit tells me: Failed asserting that false is true. Which is all fine and good, and I can usually track down WHY the test is failing....