如何在php包和phpunit中使用名称空间?
编辑以添加注释中要求的MainClass代码 我正在努力学习如何制作php软件包以及如何使用phpunit,同时,我可能在这里使用了所有错误的术语,并且做了所有错误的事情 当我使用composer导入包时,一切都按预期进行。我决定添加一些单元测试,因为我终于开始了解它们如何对我有用,但是我遇到了麻烦,我怀疑这与名称空间有关,但我不确定 我将测试放在它们自己的目录中,并在测试类的顶部使用use语句来导入主src类。例如,在一个测试类中,我有以下内容:如何在php包和phpunit中使用名称空间?,php,namespaces,phpunit,package,Php,Namespaces,Phpunit,Package,编辑以添加注释中要求的MainClass代码 我正在努力学习如何制作php软件包以及如何使用phpunit,同时,我可能在这里使用了所有错误的术语,并且做了所有错误的事情 当我使用composer导入包时,一切都按预期进行。我决定添加一些单元测试,因为我终于开始了解它们如何对我有用,但是我遇到了麻烦,我怀疑这与名称空间有关,但我不确定 我将测试放在它们自己的目录中,并在测试类的顶部使用use语句来导入主src类。例如,在一个测试类中,我有以下内容: use myname\Package\Main
use myname\Package\MainClass;
use myname\Package\Resources\Resource;
use myname\Package\Resources\ExtendedResource;
class ResourceTest extends PHPUnit_Framework_TestCase {
public function testArtistIsResource() {
$resource = '\\myname\\Package\\Resources\\Resource';
$main = new MainClass('Artist');
$artist = $main->find(1383508);
$this->assertTrue($artist instanceof $resource);
}
}
这个测试通过了。我在测试目录外运行相同的代码,并在浏览器中显示值以查看发生了什么。我正在运行laravel中的包,并在主页上运行比较代码。例如,以下代码的输出直接在浏览器中打印
$main = new MainClass('Artist');
$artist = $main->find(1383508);
echo get_class($main);
echo get_class($artist);
这表明$main是myname\Package\main类,$artist是myname\Package\Resources\ExtendedResource。如果我将上面的第二条echo语句更改为:
echo get_class($artist->get());
然后,结果是stdClass会回显到屏幕上,这是正确的。但是,在测试类中,$artist->get()返回ResourceTest的一个实例,我不明白为什么它不是另一个stdClass
我错过了什么
我的目录结构如下:
myname
|- Package
|- composer.json
|- src
|- MainClass.php
|- Resources
|- Resource.php
|- ExtendsResource.php
|- tests
|- Resources
|- ResourceTest.php
下面是composer.json的自动加载部分的内容
"autoload": {
"psr-4": {
"myname\\Package\\": "src/"
}
}
我尝试在其中添加另一个名称空间-“myname\Package\tests\:“tests/”——但似乎没有帮助
MainClass.php的命名空间为myname\Package
Resources.php和ExtendsResources.php的名称空间是myname\Package\Resources
下面是MainClass.php的代码
<?php
namespace myname\Package;
class MainClass {
private $resource;
public function __construct($resource = null) {
Container::setup();
if ($resource) {
$this->resource = Container::get($resource);
}
return $this->resource;
}
public function find($id) {
if (isset ($this->resource)) {
$this->resource->find($id);
return $this->resource;
}
else { throw new \Exception('Resource is not set'); }
}
public function setResource($resource) {
$this->resource = Container::get($resource);
return $this->resource;
}
}
你能添加MainClass
的代码吗?@StoryTeller code addedSorry,再看看这个问题,myname\Package\Resources\ExtendedResource
会很有帮助的,tooThanks,@StoryTeller我现在已经添加了这个代码,现在我看到了get()
返回由实现抓取接口的对象生成的$this->response
。。。我建议您在代码中查找原因,从grab()
方法开始。PHPUnit不应该影响它试图测试的代码,其他一切都是不合理的。
<?php
namespace myname\Package\Resources;
use myname\Package\Contracts\ConfigInterface;
use myname\Package\Contracts\GrabberInterface;
use myname\Package\Contracts\ResourceInterface;
use myname\Package\Http\Grabber;
use myname\Package\Http\Poster;
abstract class Resource {
protected $config = null;
protected $url = null;
protected $resource = null;
protected $response = null;
protected $grabber = null;
protected $perPage = null;
protected $page = null;
protected $params = null;
protected $token = null;
protected $identifier = null;
protected $update = array();
protected $appendTokenTo = array(
'myname\Package\Resources\Artist',
'myname\Package\Resources\Listing',
'myname\Package\Resources\Release',
'myname\Package\Resources\Search'
);
public function __construct($config, $grabber) {
if ($config instanceof ConfigInterface) {
$this->config = $config;
} else { throw new \Exception('The supplied $config is not an instance of ConfigInterface'); }
if ($grabber instanceof GrabberInterface) {
$this->grabber = $grabber;
} else { throw new \Exception('The supplied $grabber is not an instance of GrabberInterface'); }
$this->url .= $this->config->getApiUrl() . $this->resource;
$this->token = $this->config->getApiToken();
return $this;
}
public function addParam($param, $value) {
$value= $this->formatParamValue($value);
$this->params .= "&". "$param=$value";
return $this;
}
protected function addToken(){
$this->addParam('token', $this->token);
//$this->params .= "&". "token=$this->token";
return $this;
}
public function addUpdate($update, $value) {
$this->update[$update] = $value;
return $this;
}
protected function checkIfTokenIsRequired() {
if (in_array(get_class($this), $this->appendTokenTo)) {
$this->addToken();
}
}
public function find($identifier) {
if (empty ($this->identifier)) {
$this->identifier = $identifier;
$this->url .= "/$identifier";
}
return $this;
}
protected function formatParamValue($value) {
$value = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
$value = str_replace(' ', '+', $value);
return $value;
}
public function get() {
return json_decode($this->getResponse());
}
protected function getResponse() {
if (!isset($this->response)) {
$this->checkIfTokenIsRequired();
$this->_prepare();
}
return $this->response;
}
public function json() {
return $this->getResponse();
}
public function page($pageNumber) {
$this->page = $pageNumber;
return $this;
}
public function perPage($resultsPerPage) {
$this->perPage = $resultsPerPage;
return $this;
}
protected function _prepare() {
$params = 0;
if (isset($this->page) OR isset($this->perPage) OR isset($this->params)){
$this->url .= '?';
}
if (isset($this->params)) {
$this->url .= "$this->params";
$params++;
}
if (isset($this->perPage)) {
if ($params > 0) {
$this->url .= '&';
}
$this->url .= "per_page=$this->perPage";
$params++;
}
if (isset($this->page)) {
if ($params > 0) {
$this->url .= '&';
}
$this->url .= "page=$this->page";
$params++;
}
$this->grabber->setUrl($this->url);
$this->response = $this->grabber->grab();
}
public function setGrabber($grabber) {
if ($grabber instanceof GrabberInterface) {
$this->grabber = $grabber;
} else { throw new \Exception($grabber . " is not an instance of GrabberInterface"); }
}
public function setUrl($url) {
$this->url = $url;
}
public function update() {
//$this->update['token'] = $this->token;
$this->grabber->setMethod('POST');
$this->grabber->setUpdates(json_encode($this->update));
$this->_prepare();
return $this;
}
}
<?php
namespace myname\Package\Resources;
class ExtendedResource extends Resource {
protected $resource = 'artists';
/**
* Returns the releases associated with an artist
*
* @return $this
*/
public function releases() {
$this->url .= '/releases';
return $this;
}
}