namespaces - How do you use namespacing with php packages and phpunit? -


edited add code mainclass requested in comments.

i'm trying learn how make php packages , how use phpunit @ same time, may using wrong terminology here , doing wrong..

everything works expected when import package composer. decided add unit tests started see how useful me having trouble , suspect has namespaces i'm not sure.

i have put tests in own directory , used use statements @ top of test classes importing main src classes. so, example, in test class have following:

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);     }  } 

this test passes. running same code outside of test directory , displaying values in browser see happens. running package in laravel , running comparison code on homepage. example output of following code printed directly in browser

$main = new mainclass('artist'); $artist = $main->find(1383508);  echo get_class($main);  echo get_class($artist); 

this shows $main myname\package\mainclass , $artist myname\package\resources\extendedresource. if change second echo statement above this:

echo get_class($artist->get()); 

then result stdclass echoed screen, correct. in test class $artist->get() returns instance of resourcetest , don't understand why not stdclass.

what missing?

my directory structure follows:

myname |- package    |- composer.json    |- src       |- mainclass.php       |- resources           |- resource.php           |- extendsresource.php    |- tests       |- resources          |- resourcetest.php 

below content of autoload part of composer.json

"autoload": {     "psr-4": {         "myname\\package\\": "src/"      } } 

i tried adding namespace in there - "myname\package\tests\": "tests/" - didn't seem help.

the namespace mainclass.php myname\package.

the namespace resources.php , extendsresources.php myname\package\resources.

here code 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 not set'); }      }       public function setresource($resource) {          $this->resource = container::get($resource);         return $this->resource;     }  } 

below code myname\package\resources\reource

<?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 not instance of configinterface'); }           if ($grabber instanceof grabberinterface) {              $this->grabber = $grabber;         } else { throw new \exception('the supplied $grabber not 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 . " not 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;      }   } 

below code extendedresource

<?php  namespace myname\package\resources;   class extendedresource extends resource {      protected $resource = 'artists';      /**      * returns releases associated artist      *      * @return $this      */     public function releases() {          $this->url .= '/releases';         return $this;     }  } 


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -