Browse Source

Use Phake to create mock objects in tests

microsub
J. King 7 years ago
parent
commit
6b2be0a877
  1. 11
      composer.json
  2. 296
      composer.lock
  3. 11
      tests/Exception/TestException.php
  4. 14
      tests/lib/Lang/Setup.php

11
composer.json

@ -22,13 +22,14 @@
"ext-intl": "*",
"ext-iconv": "*",
"fguillot/picofeed": ">=0.1.31",
"jkingweb/druuid": "^3.0",
"phpseclib/phpseclib": "^2.0",
"hosteurope/password-generator": "^1.0",
"webmozart/glob": "^4.1"
"jkingweb/druuid": "^3.0.0",
"phpseclib/phpseclib": "^2.0.4",
"hosteurope/password-generator": "^1.0.1",
"webmozart/glob": "^4.1.0"
},
"require-dev": {
"mikey179/vfsStream": "^1.6.4"
"mikey179/vfsStream": "^1.6.4",
"phake/phake": "^2.3.2"
},
"autoload": {
"psr-4": {

296
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "4afb7b406ec21a2c18f46fd176fc242f",
"content-hash": "58d51aeeabc7ce9d69626938805bb61f",
"packages": [
{
"name": "fguillot/picofeed",
@ -471,6 +471,300 @@
"description": "Virtual file system to mock the real file system in unit tests.",
"homepage": "http://vfs.bovigo.org/",
"time": "2016-07-18T14:02:57+00:00"
},
{
"name": "phake/phake",
"version": "v2.3.2",
"source": {
"type": "git",
"url": "https://github.com/mlively/Phake.git",
"reference": "d5832f1a0dd2370e14d38bcbaeb6770e8546cff2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/mlively/Phake/zipball/d5832f1a0dd2370e14d38bcbaeb6770e8546cff2",
"reference": "d5832f1a0dd2370e14d38bcbaeb6770e8546cff2",
"shasum": ""
},
"require": {
"php": ">=5.3.3",
"sebastian/comparator": "~1.1"
},
"require-dev": {
"codeclimate/php-test-reporter": "dev-master",
"doctrine/common": "2.3.*",
"ext-soap": "*",
"hamcrest/hamcrest-php": "1.1.*",
"phpunit/phpunit": "3.7.*"
},
"suggest": {
"doctrine/common": "Allows mock annotations to use import statements for classes.",
"hamcrest/hamcrest-php": "Use Hamcrest matchers."
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0.0-dev"
}
},
"autoload": {
"psr-0": {
"Phake": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Mike Lively",
"email": "m@digitalsandwich.com"
}
],
"description": "The Phake mock testing library",
"homepage": "https://github.com/mlively/Phake",
"keywords": [
"mock",
"testing"
],
"time": "2017-03-20T05:16:34+00:00"
},
{
"name": "sebastian/comparator",
"version": "1.2.4",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/comparator.git",
"reference": "2b7424b55f5047b47ac6e5ccb20b2aea4011d9be"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/2b7424b55f5047b47ac6e5ccb20b2aea4011d9be",
"reference": "2b7424b55f5047b47ac6e5ccb20b2aea4011d9be",
"shasum": ""
},
"require": {
"php": ">=5.3.3",
"sebastian/diff": "~1.2",
"sebastian/exporter": "~1.2 || ~2.0"
},
"require-dev": {
"phpunit/phpunit": "~4.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.2.x-dev"
}
},
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Jeff Welch",
"email": "whatthejeff@gmail.com"
},
{
"name": "Volker Dusch",
"email": "github@wallbash.com"
},
{
"name": "Bernhard Schussek",
"email": "bschussek@2bepublished.at"
},
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de"
}
],
"description": "Provides the functionality to compare PHP values for equality",
"homepage": "http://www.github.com/sebastianbergmann/comparator",
"keywords": [
"comparator",
"compare",
"equality"
],
"time": "2017-01-29T09:50:25+00:00"
},
{
"name": "sebastian/diff",
"version": "1.4.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/diff.git",
"reference": "13edfd8706462032c2f52b4b862974dd46b71c9e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/13edfd8706462032c2f52b4b862974dd46b71c9e",
"reference": "13edfd8706462032c2f52b4b862974dd46b71c9e",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Kore Nordmann",
"email": "mail@kore-nordmann.de"
},
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de"
}
],
"description": "Diff implementation",
"homepage": "https://github.com/sebastianbergmann/diff",
"keywords": [
"diff"
],
"time": "2015-12-08T07:14:41+00:00"
},
{
"name": "sebastian/exporter",
"version": "2.0.0",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/exporter.git",
"reference": "ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4",
"reference": "ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4",
"shasum": ""
},
"require": {
"php": ">=5.3.3",
"sebastian/recursion-context": "~2.0"
},
"require-dev": {
"ext-mbstring": "*",
"phpunit/phpunit": "~4.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Jeff Welch",
"email": "whatthejeff@gmail.com"
},
{
"name": "Volker Dusch",
"email": "github@wallbash.com"
},
{
"name": "Bernhard Schussek",
"email": "bschussek@2bepublished.at"
},
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de"
},
{
"name": "Adam Harvey",
"email": "aharvey@php.net"
}
],
"description": "Provides the functionality to export PHP variables for visualization",
"homepage": "http://www.github.com/sebastianbergmann/exporter",
"keywords": [
"export",
"exporter"
],
"time": "2016-11-19T08:54:04+00:00"
},
{
"name": "sebastian/recursion-context",
"version": "2.0.0",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/recursion-context.git",
"reference": "2c3ba150cbec723aa057506e73a8d33bdb286c9a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/2c3ba150cbec723aa057506e73a8d33bdb286c9a",
"reference": "2c3ba150cbec723aa057506e73a8d33bdb286c9a",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "~4.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Jeff Welch",
"email": "whatthejeff@gmail.com"
},
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de"
},
{
"name": "Adam Harvey",
"email": "aharvey@php.net"
}
],
"description": "Provides functionality to recursively process PHP variables",
"homepage": "http://www.github.com/sebastianbergmann/recursion-context",
"time": "2016-11-19T07:33:16+00:00"
}
],
"aliases": [],

11
tests/Exception/TestException.php

@ -1,6 +1,7 @@
<?php
declare(strict_types=1);
namespace JKingWeb\Arsse;
Use Phake;
class TestException extends \PHPUnit\Framework\TestCase {
@ -8,12 +9,16 @@ class TestException extends \PHPUnit\Framework\TestCase {
function setUp() {
$this->clearData(false);
$m = $this->getMockBuilder(Lang::class)->setMethods(['msg'])->getMock();
$m->expects($this->any())->method("msg")->with($this->anything(), $this->anything())->will($this->returnValue(""));
Data::$l = $m;
// create a mock Lang object so as not to create a dependency loop
Data::$l = Phake::mock(Lang::class);
Phake::when(Data::$l)->msg->thenReturn("");
}
function tearDown() {
// verify calls to the mock Lang object
Phake::verify(Data::$l, Phake::atLeast(0))->msg($this->isType("string"), $this->anything());
Phake::verifyNoOtherInteractions(Data::$l);
// clean up
$this->clearData(true);
}

14
tests/lib/Lang/Setup.php

@ -1,9 +1,10 @@
<?php
declare(strict_types=1);
namespace JKingWeb\Arsse\Test\Lang;
use org\bovigo\vfs\vfsStream;
use JKingWeb\Arsse\Lang;
use JKingWeb\Arsse\Data;
use org\bovigo\vfs\vfsStream;
use Phake;
@ -34,16 +35,19 @@ trait Setup {
chmod($this->path."ru.php", 0000);
// make the test Lang class use the vfs files
$this->l = new Lang($this->path);
// create a mock Lang object to keep exceptions from creating loops
// create a mock Lang object so as not to create a dependency loop
$this->clearData(false);
$m = $this->getMockBuilder(Lang::class)->setMethods(['msg'])->getMock();
$m->expects($this->any())->method("msg")->with($this->anything(), $this->anything())->will($this->returnValue(""));
Data::$l = $m;
Data::$l = Phake::mock(Lang::class);
Phake::when(Data::$l)->msg->thenReturn("");
// call the additional setup method if it exists
if(method_exists($this, "setUpSeries")) $this->setUpSeries();
}
function tearDown() {
// verify calls to the mock Lang object
Phake::verify(Data::$l, Phake::atLeast(0))->msg($this->isType("string"), $this->anything());
Phake::verifyNoOtherInteractions(Data::$l);
// clean up
$this->clearData(true);
// call the additional teardiwn method if it exists
if(method_exists($this, "tearDownSeries")) $this->tearDownSeries();

Loading…
Cancel
Save