Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Sample upload

  • Loading branch information...
commit 05ee7e8a920410e76923febb9d176d9006a9c2b7 1 parent e0273fc
Martin Shwalbe authored October 28, 2012
48  Module.php
... ...
@@ -0,0 +1,48 @@
  1
+<?php
  2
+namespace Album;
  3
+
  4
+use Album\Model\Album;
  5
+use Album\Model\AlbumTable;
  6
+use Zend\Db\ResultSet\ResultSet;
  7
+use Zend\Db\TableGateway\TableGateway;
  8
+
  9
+class Module
  10
+{
  11
+    public function getAutoloaderConfig()
  12
+    {
  13
+        return array(
  14
+            'Zend\Loader\ClassMapAutoloader' => array(
  15
+                __DIR__ . '/autoload_classmap.php',
  16
+            ),
  17
+            'Zend\Loader\StandardAutoloader' => array(
  18
+                'namespaces' => array(
  19
+                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
  20
+                ),
  21
+            ),
  22
+        );
  23
+    }
  24
+
  25
+    public function getServiceConfig()
  26
+    {
  27
+        return array(
  28
+            'factories' => array(
  29
+                'Album\Model\AlbumTable' =>  function($sm) {
  30
+                    $tableGateway = $sm->get('AlbumTableGateway');
  31
+                    $table = new AlbumTable($tableGateway);
  32
+                    return $table;
  33
+                },
  34
+                'AlbumTableGateway' => function ($sm) {
  35
+                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
  36
+                    $resultSetPrototype = new ResultSet();
  37
+                    $resultSetPrototype->setArrayObjectPrototype(new Album());
  38
+                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
  39
+                },
  40
+            ),
  41
+        );
  42
+    }
  43
+
  44
+    public function getConfig()
  45
+    {
  46
+        return include __DIR__ . '/config/module.config.php';
  47
+    }
  48
+}
2  autoload_classmap.php
... ...
@@ -0,0 +1,2 @@
  1
+<?php
  2
+return array();
7  config/database.local.php
... ...
@@ -0,0 +1,7 @@
  1
+<?php
  2
+return array(
  3
+    'db' => array(
  4
+        'username' => 'YOUR USERNAME HERE',
  5
+        'password' => 'YOUR PASSWORD HERE',
  6
+    ),
  7
+);
27  config/global.php.dist
... ...
@@ -0,0 +1,27 @@
  1
+<?php
  2
+/**
  3
+ * Global Configuration Override
  4
+ *
  5
+ * You can use this file for overriding configuration values from modules, etc.
  6
+ * You would place values in here that are agnostic to the environment and not
  7
+ * sensitive to security.
  8
+ *
  9
+ * @NOTE: In practice, this file will typically be INCLUDED in your source
  10
+ * control, so do not include passwords or other sensitive information in this
  11
+ * file.
  12
+ */
  13
+return array(
  14
+    'db' => array(
  15
+        'driver'         => 'Pdo',
  16
+        'dsn'            => 'mysql:dbname=album;host=localhost',
  17
+        'driver_options' => array(
  18
+            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
  19
+        ),
  20
+    ),
  21
+    'service_manager' => array(
  22
+        'factories' => array(
  23
+            'Zend\Db\Adapter\Adapter'
  24
+                    => 'Zend\Db\Adapter\AdapterServiceFactory',
  25
+        ),
  26
+    ),
  27
+);
32  config/module.config.php
... ...
@@ -0,0 +1,32 @@
  1
+<?php
  2
+return array(
  3
+    'controllers' => array(
  4
+        'invokables' => array(
  5
+            'Album\Controller\Album' => 'Album\Controller\AlbumController',
  6
+        ),
  7
+    ),
  8
+    // The following section is new and should be added to your file
  9
+    'router' => array(
  10
+        'routes' => array(
  11
+            'album' => array(
  12
+                'type'    => 'segment',
  13
+                'options' => array(
  14
+                    'route'    => '/album[/:action][/:id]',
  15
+                    'constraints' => array(
  16
+                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
  17
+                        'id'     => '[0-9]+',
  18
+                    ),
  19
+                    'defaults' => array(
  20
+                        'controller' => 'Album\Controller\Album',
  21
+                        'action'     => 'index',
  22
+                    ),
  23
+                ),
  24
+            ),
  25
+        ),
  26
+    ),
  27
+    'view_manager' => array(
  28
+        'template_path_stack' => array(
  29
+            'album' => __DIR__ . '/../view',
  30
+        ),
  31
+    ),
  32
+);
109  src/Album/Controller/AlbumController.php
... ...
@@ -0,0 +1,109 @@
  1
+<?php
  2
+namespace Album\Controller;
  3
+
  4
+use Zend\Mvc\Controller\AbstractActionController;
  5
+use Zend\View\Model\ViewModel;
  6
+use Album\Model\Album;
  7
+use Album\Form\AlbumForm;
  8
+
  9
+class AlbumController extends AbstractActionController
  10
+{
  11
+    protected $albumTable;
  12
+
  13
+    public function indexAction()
  14
+    {
  15
+        return new ViewModel(array(
  16
+            'albums' => $this->getAlbumTable()->fetchAll(),
  17
+        ));
  18
+    }
  19
+
  20
+    public function addAction()
  21
+    {
  22
+        $form = new AlbumForm();
  23
+        $form->get('submit')->setValue('Add');
  24
+
  25
+        $request = $this->getRequest();
  26
+        if ($request->isPost()) {
  27
+            $album = new Album();
  28
+            $form->setInputFilter($album->getInputFilter());
  29
+            $form->setData($request->getPost());
  30
+
  31
+            if ($form->isValid()) {
  32
+                $album->exchangeArray($form->getData());
  33
+                $this->getAlbumTable()->saveAlbum($album);
  34
+
  35
+                // Redirect to list of albums
  36
+                return $this->redirect()->toRoute('album');
  37
+            }
  38
+        }
  39
+        return array('form' => $form);
  40
+    }
  41
+
  42
+    public function editAction()
  43
+    {
  44
+        $id = (int) $this->params()->fromRoute('id', 0);
  45
+        if (!$id) {
  46
+            return $this->redirect()->toRoute('album', array(
  47
+                'action' => 'add'
  48
+            ));
  49
+        }
  50
+        $album = $this->getAlbumTable()->getAlbum($id);
  51
+
  52
+        $form  = new AlbumForm();
  53
+        $form->bind($album);
  54
+        $form->get('submit')->setAttribute('value', 'Edit');
  55
+
  56
+        $request = $this->getRequest();
  57
+        if ($request->isPost()) {
  58
+            $form->setInputFilter($album->getInputFilter());
  59
+            $form->setData($request->getPost());
  60
+
  61
+            if ($form->isValid()) {
  62
+                $this->getAlbumTable()->saveAlbum($form->getData());
  63
+
  64
+                // Redirect to list of albums
  65
+                return $this->redirect()->toRoute('album');
  66
+            }
  67
+        }
  68
+
  69
+        return array(
  70
+            'id' => $id,
  71
+            'form' => $form,
  72
+        );
  73
+    }
  74
+
  75
+    public function deleteAction()
  76
+    {
  77
+        $id = (int) $this->params()->fromRoute('id', 0);
  78
+        if (!$id) {
  79
+            return $this->redirect()->toRoute('album');
  80
+        }
  81
+
  82
+        $request = $this->getRequest();
  83
+        if ($request->isPost()) {
  84
+            $del = $request->getPost('del', 'No');
  85
+
  86
+            if ($del == 'Yes') {
  87
+                $id = (int) $request->getPost('id');
  88
+                $this->getAlbumTable()->deleteAlbum($id);
  89
+            }
  90
+
  91
+            // Redirect to list of albums
  92
+            return $this->redirect()->toRoute('album');
  93
+        }
  94
+
  95
+        return array(
  96
+            'id'    => $id,
  97
+            'album' => $this->getAlbumTable()->getAlbum($id)
  98
+        );
  99
+    }
  100
+
  101
+    public function getAlbumTable()
  102
+    {
  103
+        if (!$this->albumTable) {
  104
+            $sm = $this->getServiceLocator();
  105
+            $this->albumTable = $sm->get('Album\Model\AlbumTable');
  106
+        }
  107
+        return $this->albumTable;
  108
+    }
  109
+}
47  src/Album/Form/AlbumForm.php
... ...
@@ -0,0 +1,47 @@
  1
+<?php
  2
+// module/Album/src/Album/Form/AlbumForm.php:
  3
+namespace Album\Form;
  4
+
  5
+use Zend\Form\Form;
  6
+
  7
+class AlbumForm extends Form
  8
+{
  9
+    public function __construct($name = null)
  10
+    {
  11
+        // we want to ignore the name passed
  12
+        parent::__construct('album');
  13
+        $this->setAttribute('method', 'post');
  14
+        $this->add(array(
  15
+            'name' => 'id',
  16
+            'attributes' => array(
  17
+                'type'  => 'hidden',
  18
+            ),
  19
+        ));
  20
+        $this->add(array(
  21
+            'name' => 'artist',
  22
+            'attributes' => array(
  23
+                'type'  => 'text',
  24
+            ),
  25
+            'options' => array(
  26
+                'label' => 'Artist',
  27
+            ),
  28
+        ));
  29
+        $this->add(array(
  30
+            'name' => 'title',
  31
+            'attributes' => array(
  32
+                'type'  => 'text',
  33
+            ),
  34
+            'options' => array(
  35
+                'label' => 'Title',
  36
+            ),
  37
+        ));
  38
+        $this->add(array(
  39
+            'name' => 'submit',
  40
+            'attributes' => array(
  41
+                'type'  => 'submit',
  42
+                'value' => 'Go',
  43
+                'id' => 'submitbutton',
  44
+            ),
  45
+        ));
  46
+    }
  47
+}
92  src/Album/Model/Album.php
... ...
@@ -0,0 +1,92 @@
  1
+<?php
  2
+// module/Album/src/Album/Model/Album.php:
  3
+namespace Album\Model;
  4
+
  5
+use Zend\InputFilter\Factory as InputFactory;
  6
+use Zend\InputFilter\InputFilter;
  7
+use Zend\InputFilter\InputFilterAwareInterface;
  8
+use Zend\InputFilter\InputFilterInterface;
  9
+
  10
+class Album implements InputFilterAwareInterface
  11
+{
  12
+    public $id;
  13
+    public $artist;
  14
+    public $title;
  15
+    protected $inputFilter;
  16
+
  17
+    public function exchangeArray($data)
  18
+    {
  19
+        $this->id     = (isset($data['id']))     ? $data['id']     : null;
  20
+        $this->artist = (isset($data['artist'])) ? $data['artist'] : null;
  21
+        $this->title  = (isset($data['title']))  ? $data['title']  : null;
  22
+    }
  23
+
  24
+     // Add the following method:
  25
+    public function getArrayCopy()
  26
+    {
  27
+        return get_object_vars($this);
  28
+    }
  29
+
  30
+    public function setInputFilter(InputFilterInterface $inputFilter)
  31
+    {
  32
+        throw new \Exception("Not used");
  33
+    }
  34
+
  35
+    public function getInputFilter()
  36
+    {
  37
+        if (!$this->inputFilter) {
  38
+            $inputFilter = new InputFilter();
  39
+            $factory     = new InputFactory();
  40
+
  41
+            $inputFilter->add($factory->createInput(array(
  42
+                'name'     => 'id',
  43
+                'required' => true,
  44
+                'filters'  => array(
  45
+                    array('name' => 'Int'),
  46
+                ),
  47
+            )));
  48
+
  49
+            $inputFilter->add($factory->createInput(array(
  50
+                'name'     => 'artist',
  51
+                'required' => true,
  52
+                'filters'  => array(
  53
+                    array('name' => 'StripTags'),
  54
+                    array('name' => 'StringTrim'),
  55
+                ),
  56
+                'validators' => array(
  57
+                    array(
  58
+                        'name'    => 'StringLength',
  59
+                        'options' => array(
  60
+                            'encoding' => 'UTF-8',
  61
+                            'min'      => 1,
  62
+                            'max'      => 100,
  63
+                        ),
  64
+                    ),
  65
+                ),
  66
+            )));
  67
+
  68
+            $inputFilter->add($factory->createInput(array(
  69
+                'name'     => 'title',
  70
+                'required' => true,
  71
+                'filters'  => array(
  72
+                    array('name' => 'StripTags'),
  73
+                    array('name' => 'StringTrim'),
  74
+                ),
  75
+                'validators' => array(
  76
+                    array(
  77
+                        'name'    => 'StringLength',
  78
+                        'options' => array(
  79
+                            'encoding' => 'UTF-8',
  80
+                            'min'      => 1,
  81
+                            'max'      => 100,
  82
+                        ),
  83
+                    ),
  84
+                ),
  85
+            )));
  86
+
  87
+            $this->inputFilter = $inputFilter;
  88
+        }
  89
+
  90
+        return $this->inputFilter;
  91
+    }
  92
+}
56  src/Album/Model/AlbumTable.php
... ...
@@ -0,0 +1,56 @@
  1
+<?php
  2
+// module/Album/src/Album/Model/AlbumTable.php:
  3
+namespace Album\Model;
  4
+
  5
+use Zend\Db\TableGateway\TableGateway;
  6
+
  7
+class AlbumTable
  8
+{
  9
+    protected $tableGateway;
  10
+
  11
+    public function __construct(TableGateway $tableGateway)
  12
+    {
  13
+        $this->tableGateway = $tableGateway;
  14
+    }
  15
+
  16
+    public function fetchAll()
  17
+    {
  18
+        $resultSet = $this->tableGateway->select();
  19
+        return $resultSet;
  20
+    }
  21
+
  22
+    public function getAlbum($id)
  23
+    {
  24
+        $id  = (int) $id;
  25
+        $rowset = $this->tableGateway->select(array('id' => $id));
  26
+        $row = $rowset->current();
  27
+        if (!$row) {
  28
+            throw new \Exception("Could not find row $id");
  29
+        }
  30
+        return $row;
  31
+    }
  32
+
  33
+    public function saveAlbum(Album $album)
  34
+    {
  35
+        $data = array(
  36
+            'artist' => $album->artist,
  37
+            'title'  => $album->title,
  38
+        );
  39
+
  40
+        $id = (int)$album->id;
  41
+        if ($id == 0) {
  42
+            $this->tableGateway->insert($data);
  43
+        } else {
  44
+            if ($this->getAlbum($id)) {
  45
+                $this->tableGateway->update($data, array('id' => $id));
  46
+            } else {
  47
+                throw new \Exception('Form id does not exist');
  48
+            }
  49
+        }
  50
+    }
  51
+
  52
+    public function deleteAlbum($id)
  53
+    {
  54
+        $this->tableGateway->delete(array('id' => $id));
  55
+    }
  56
+}
76  test/AlbumTest/Controller/AlbumControllerTest.php
... ...
@@ -0,0 +1,76 @@
  1
+<?php
  2
+
  3
+namespace AlbumTest\Controller;
  4
+
  5
+use Album\Controller\AlbumController;
  6
+use Zend\Http\Request;
  7
+use Zend\Http\Response;
  8
+use Zend\Mvc\MvcEvent;
  9
+use Zend\Mvc\Router\RouteMatch;
  10
+use PHPUnit_Framework_TestCase;
  11
+use AlbumTest\Bootstrap;
  12
+
  13
+class AlbumControllerTest extends PHPUnit_Framework_TestCase
  14
+{
  15
+    protected $controller;
  16
+    protected $request;
  17
+    protected $response;
  18
+    protected $routeMatch;
  19
+    protected $event;
  20
+
  21
+    protected function setUp()
  22
+    {
  23
+        $this->controller = new AlbumController();
  24
+        $this->request    = new Request();
  25
+        $this->routeMatch = new RouteMatch(array('controller' => 'index'));
  26
+        $this->event      = new MvcEvent();
  27
+        $this->event->setRouteMatch($this->routeMatch);
  28
+        $this->controller->setEvent($this->event);
  29
+        $this->controller->setServiceLocator(Bootstrap::getServiceManager());
  30
+    }
  31
+
  32
+    public function testAddActionCanBeAccessed()
  33
+    {
  34
+        $this->routeMatch->setParam('action', 'add');
  35
+
  36
+        $result   = $this->controller->dispatch($this->request);
  37
+        $response = $this->controller->getResponse();
  38
+        $this->assertEquals(200, $response->getStatusCode());
  39
+    }
  40
+
  41
+    public function testDeleteActionCanBeAccessed()
  42
+    {
  43
+        $this->routeMatch->setParam('action', 'delete');
  44
+
  45
+        $result   = $this->controller->dispatch($this->request);
  46
+        $response = $this->controller->getResponse();
  47
+
  48
+        $this->assertEquals(200, $response->getStatusCode());
  49
+    }
  50
+
  51
+    public function testEditActionCanBeAccessed()
  52
+    {
  53
+        $this->routeMatch->setParam('action', 'edit');
  54
+
  55
+        $result   = $this->controller->dispatch($this->request);
  56
+        $response = $this->controller->getResponse();
  57
+
  58
+        $this->assertEquals(200, $response->getStatusCode());
  59
+    }
  60
+
  61
+    public function testIndexActionCanBeAccessed()
  62
+    {
  63
+        $this->routeMatch->setParam('action', 'index');
  64
+
  65
+        $result   = $this->controller->dispatch($this->request);
  66
+        $response = $this->controller->getResponse();
  67
+
  68
+        $this->assertEquals(200, $response->getStatusCode());
  69
+    }
  70
+
  71
+    public function testGetAlbumTableReturnsAnInstanceOfAlbumTable()
  72
+    {
  73
+        $this->assertInstanceOf('Album\Model\AlbumTable', $this->controller->getAlbumTable());
  74
+    }
  75
+
  76
+}
123  test/AlbumTest/Model/AlbumTableTest.php
... ...
@@ -0,0 +1,123 @@
  1
+<?php
  2
+// module/Album/test/AlbumTest/Model/AlbumTableTest.php:
  3
+namespace Album\Model;
  4
+
  5
+use Zend\Db\ResultSet\ResultSet;
  6
+use PHPUnit_Framework_TestCase;
  7
+
  8
+class AlbumTableTest extends PHPUnit_Framework_TestCase
  9
+{
  10
+    public function testFetchAllReturnsAllAlbums()
  11
+    {
  12
+        $resultSet        = new ResultSet();
  13
+        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
  14
+                                           array('select'), array(), '', false);
  15
+        $mockTableGateway->expects($this->once())
  16
+                         ->method('select')
  17
+                         ->with()
  18
+                         ->will($this->returnValue($resultSet));
  19
+
  20
+        $albumTable = new AlbumTable($mockTableGateway);
  21
+
  22
+        $this->assertSame($resultSet, $albumTable->fetchAll());
  23
+    }
  24
+    public function testCanRetrieveAnAlbumByItsId()
  25
+    {
  26
+        $album = new Album();
  27
+        $album->exchangeArray(array('id'     => 123,
  28
+                                    'artist' => 'The Military Wives',
  29
+                                    'title'  => 'In My Dreams'));
  30
+
  31
+        $resultSet = new ResultSet();
  32
+        $resultSet->setArrayObjectPrototype(new Album());
  33
+        $resultSet->initialize(array($album));
  34
+
  35
+        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select'), array(), '', false);
  36
+        $mockTableGateway->expects($this->once())
  37
+                         ->method('select')
  38
+                         ->with(array('id' => 123))
  39
+                         ->will($this->returnValue($resultSet));
  40
+
  41
+        $albumTable = new AlbumTable($mockTableGateway);
  42
+
  43
+        $this->assertSame($album, $albumTable->getAlbum(123));
  44
+    }
  45
+
  46
+    public function testCanDeleteAnAlbumByItsId()
  47
+    {
  48
+        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('delete'), array(), '', false);
  49
+        $mockTableGateway->expects($this->once())
  50
+                         ->method('delete')
  51
+                         ->with(array('id' => 123));
  52
+
  53
+        $albumTable = new AlbumTable($mockTableGateway);
  54
+        $albumTable->deleteAlbum(123);
  55
+    }
  56
+
  57
+    public function testSaveAlbumWillInsertNewAlbumsIfTheyDontAlreadyHaveAnId()
  58
+    {
  59
+        $albumData = array('artist' => 'The Military Wives', 'title' => 'In My Dreams');
  60
+        $album     = new Album();
  61
+        $album->exchangeArray($albumData);
  62
+
  63
+        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('insert'), array(), '', false);
  64
+        $mockTableGateway->expects($this->once())
  65
+                         ->method('insert')
  66
+                         ->with($albumData);
  67
+
  68
+        $albumTable = new AlbumTable($mockTableGateway);
  69
+        $albumTable->saveAlbum($album);
  70
+    }
  71
+
  72
+    public function testSaveAlbumWillUpdateExistingAlbumsIfTheyAlreadyHaveAnId()
  73
+    {
  74
+        $albumData = array('id' => 123, 'artist' => 'The Military Wives', 'title' => 'In My Dreams');
  75
+        $album     = new Album();
  76
+        $album->exchangeArray($albumData);
  77
+
  78
+        $resultSet = new ResultSet();
  79
+        $resultSet->setArrayObjectPrototype(new Album());
  80
+        $resultSet->initialize(array($album));
  81
+
  82
+        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway',
  83
+                                           array('select', 'update'), array(), '', false);
  84
+        $mockTableGateway->expects($this->once())
  85
+                         ->method('select')
  86
+                         ->with(array('id' => 123))
  87
+                         ->will($this->returnValue($resultSet));
  88
+        $mockTableGateway->expects($this->once())
  89
+                         ->method('update')
  90
+                         ->with(array('artist' => 'The Military Wives', 'title' => 'In My Dreams'),
  91
+                                array('id' => 123));
  92
+
  93
+        $albumTable = new AlbumTable($mockTableGateway);
  94
+        $albumTable->saveAlbum($album);
  95
+    }
  96
+
  97
+    public function testExceptionIsThrownWhenGettingNonexistentAlbum()
  98
+    {
  99
+        $resultSet = new ResultSet();
  100
+        $resultSet->setArrayObjectPrototype(new Album());
  101
+        $resultSet->initialize(array());
  102
+
  103
+        $mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array('select'), array(), '', false);
  104
+        $mockTableGateway->expects($this->once())
  105
+                         ->method('select')
  106
+                         ->with(array('id' => 123))
  107
+                         ->will($this->returnValue($resultSet));
  108
+
  109
+        $albumTable = new AlbumTable($mockTableGateway);
  110
+
  111
+        try
  112
+        {
  113
+            $albumTable->getAlbum(123);
  114
+        }
  115
+        catch (\Exception $e)
  116
+        {
  117
+            $this->assertSame('Could not find row 123', $e->getMessage());
  118
+            return;
  119
+        }
  120
+
  121
+        $this->fail('Expected exception was not thrown');
  122
+    }
  123
+}
47  test/AlbumTest/Model/AlbumTest.php
... ...
@@ -0,0 +1,47 @@
  1
+<?php
  2
+// module/Album/test/AlbumTest/Model/AlbumTest.php:
  3
+namespace AlbumTest\Model;
  4
+
  5
+use Album\Model\Album;
  6
+
  7
+use PHPUnit_Framework_TestCase;
  8
+
  9
+class AlbumTest extends PHPUnit_Framework_TestCase
  10
+{
  11
+    public function testAlbumInitialState()
  12
+    {
  13
+        $album = new Album();
  14
+
  15
+        $this->assertNull($album->artist, '"artist" should initially be null');
  16
+        $this->assertNull($album->id, '"id" should initially be null');
  17
+        $this->assertNull($album->title, '"title" should initially be null');
  18
+    }
  19
+
  20
+    public function testExchangeArraySetsPropertiesCorrectly()
  21
+    {
  22
+        $album = new Album();
  23
+        $data  = array('artist' => 'some artist',
  24
+                       'id'     => 123,
  25
+                       'title'  => 'some title');
  26
+
  27
+        $album->exchangeArray($data);
  28
+
  29
+        $this->assertSame($data['artist'], $album->artist, '"artist" was not set correctly');
  30
+        $this->assertSame($data['id'], $album->id, '"title" was not set correctly');
  31
+        $this->assertSame($data['title'], $album->title, '"title" was not set correctly');
  32
+    }
  33
+
  34
+    public function testExchangeArraySetsPropertiesToNullIfKeysAreNotPresent()
  35
+    {
  36
+        $album = new Album();
  37
+
  38
+        $album->exchangeArray(array('artist' => 'some artist',
  39
+                                    'id'     => 123,
  40
+                                    'title'  => 'some title'));
  41
+        $album->exchangeArray(array());
  42
+
  43
+        $this->assertNull($album->artist, '"artist" should have defaulted to null');
  44
+        $this->assertNull($album->id, '"title" should have defaulted to null');
  45
+        $this->assertNull($album->title, '"title" should have defaulted to null');
  46
+    }
  47
+}
102  test/Bootstrap.php
... ...
@@ -0,0 +1,102 @@
  1
+<?php
  2
+namespace AlbumTest;
  3
+
  4
+use Zend\Loader\AutoloaderFactory;
  5
+use Zend\Mvc\Service\ServiceManagerConfig;
  6
+use Zend\ServiceManager\ServiceManager;
  7
+use Zend\Stdlib\ArrayUtils;
  8
+use RuntimeException;
  9
+
  10
+error_reporting(E_ALL | E_STRICT);
  11
+chdir(__DIR__);
  12
+
  13
+class Bootstrap
  14
+{
  15
+    protected static $serviceManager;
  16
+
  17
+    public static function init()
  18
+    {
  19
+        // Load the user-defined test configuration file, if it exists; otherwise, load
  20
+        if (is_readable(__DIR__ . '/TestConfig.php')) {
  21
+            $testConfig = include __DIR__ . '/TestConfig.php';
  22
+        } else {
  23
+            $testConfig = include __DIR__ . '/TestConfig.php.dist';
  24
+        }
  25
+
  26
+        $zf2ModulePaths = array();
  27
+
  28
+        if(isset($testConfig['module_listener_options']['module_paths'])) {
  29
+            $modulePaths = $testConfig['module_listener_options']['module_paths'];
  30
+            foreach($modulePaths as $modulePath) {
  31
+                if (($path = static::findParentPath($modulePath)) ) {
  32
+                    $zf2ModulePaths[] = $path;
  33
+                }
  34
+            }
  35
+        }
  36
+
  37
+        $zf2ModulePaths  = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
  38
+        $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');
  39
+
  40
+        static::initAutoloader();
  41
+
  42
+        // use ModuleManager to load this module and it's dependencies
  43
+        $baseConfig = array(
  44
+            'module_listener_options' => array(
  45
+                'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
  46
+            ),
  47
+        );
  48
+
  49
+        $config = ArrayUtils::merge($baseConfig, $testConfig);
  50
+
  51
+        $serviceManager = new ServiceManager(new ServiceManagerConfig());
  52
+        $serviceManager->setService('ApplicationConfig', $config);
  53
+        $serviceManager->get('ModuleManager')->loadModules();
  54
+        static::$serviceManager = $serviceManager;
  55
+    }
  56
+
  57
+    public static function getServiceManager()
  58
+    {
  59
+        return static::$serviceManager;
  60
+    }
  61
+
  62
+    protected static function initAutoloader()
  63
+    {
  64
+        $vendorPath = static::findParentPath('vendor');
  65
+
  66
+        if (is_readable($vendorPath . '/autoload.php')) {
  67
+            $loader = include $vendorPath . '/autoload.php';
  68
+        } else {
  69
+            $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));
  70
+
  71
+            if (!$zf2Path) {
  72
+                throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
  73
+            }
  74
+
  75
+            include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
  76
+
  77
+        }
  78
+
  79
+        AutoloaderFactory::factory(array(
  80
+            'Zend\Loader\StandardAutoloader' => array(
  81
+                'autoregister_zf' => true,
  82
+                'namespaces' => array(
  83
+                    __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
  84
+                ),
  85
+            ),
  86
+        ));
  87
+    }
  88
+
  89
+    protected static function findParentPath($path)
  90
+    {
  91
+        $dir = __DIR__;
  92
+        $previousDir = '.';
  93
+        while (!is_dir($dir . '/' . $path)) {
  94
+            $dir = dirname($dir);
  95
+            if ($previousDir === $dir) return false;
  96
+            $previousDir = $dir;
  97
+        }
  98
+        return $dir . '/' . $path;
  99
+    }
  100
+}
  101
+
  102
+Bootstrap::init();
15  test/TestConfig.php.dist
... ...
@@ -0,0 +1,15 @@
  1
+<?php
  2
+return array(
  3
+    'modules' => array(
  4
+        'Album',
  5
+    ),
  6
+    'module_listener_options' => array(
  7
+        'config_glob_paths'    => array(
  8
+            '../../../config/autoload/{,*.}{global,local}.php',
  9
+        ),
  10
+        'module_paths' => array(
  11
+            'module',
  12
+            'vendor',
  13
+        ),
  14
+    ),
  15
+);
9  test/phpunit.xml
... ...
@@ -0,0 +1,9 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+
  3
+<phpunit bootstrap="Bootstrap.php">
  4
+    <testsuites>
  5
+        <testsuite name="Album Module test">
  6
+            <directory>./</directory>
  7
+        </testsuite>
  8
+    </testsuites>
  9
+</phpunit>
15  view/album/album/add.phtml
... ...
@@ -0,0 +1,15 @@
  1
+<?php
  2
+// module/Album/view/album/album/add.phtml:
  3
+
  4
+$title = 'Add new album';
  5
+$this->headTitle($title);
  6
+?>
  7
+<h1><?php echo $this->escapeHtml($title); ?></h1>
  8
+<?php
  9
+$form = $this->form;
  10
+$form->setAttribute('action', $this->url('album', array('action' => 'add')));
  11
+$form->prepare();
  12
+
  13
+echo $this->form()->openTag($form);
  14
+echo $this->formCollection($form);
  15
+echo $this->form()->closeTag();
25  view/album/album/delete.phtml
... ...
@@ -0,0 +1,25 @@
  1
+<?php
  2
+// module/Album/view/album/album/delete.phtml:
  3
+
  4
+$title = 'Delete album';
  5
+$this->headTitle($title);
  6
+?>
  7
+<h1><?php echo $this->escapeHtml($title); ?></h1>
  8
+
  9
+<p>Are you sure that you want to delete
  10
+    '<?php echo $this->escapeHtml($album->title); ?>' by
  11
+    '<?php echo $this->escapeHtml($album->artist); ?>'?
  12
+</p>
  13
+<?php
  14
+$url = $this->url('album', array(
  15
+    'action' => 'delete',
  16
+    'id'     => $this->id,
  17
+));
  18
+?>
  19
+<form action="<?php echo $url; ?>" method="post">
  20
+<div>
  21
+    <input type="hidden" name="id" value="<?php echo (int) $album->id; ?>" />
  22
+    <input type="submit" name="del" value="Yes" />
  23
+    <input type="submit" name="del" value="No" />
  24
+</div>
  25
+</form>
25  view/album/album/edit.phtml
... ...
@@ -0,0 +1,25 @@
  1
+<?php
  2
+// module/Album/view/album/album/edit.phtml:
  3
+
  4
+$title = 'Edit album';
  5
+$this->headTitle($title);
  6
+?>
  7
+<h1><?php echo $this->escapeHtml($title); ?></h1>
  8
+
  9
+<?php
  10
+$form = $this->form;
  11
+$form->setAttribute('action', $this->url(
  12
+    'album',
  13
+    array(
  14
+        'action' => 'edit',
  15
+        'id'     => $this->id,
  16
+    )
  17
+));
  18
+$form->prepare();
  19
+
  20
+echo $this->form()->openTag($form);
  21
+echo $this->formHidden($form->get('id'));
  22
+echo $this->formRow($form->get('title'));
  23
+echo $this->formRow($form->get('artist'));
  24
+echo $this->formSubmit($form->get('submit'));
  25
+echo $this->form()->closeTag();
30  view/album/album/index.phtml
... ...
@@ -0,0 +1,30 @@
  1
+<?php
  2
+// module/Album/view/album/album/index.phtml:
  3
+
  4
+$title = 'My albums';
  5
+$this->headTitle($title);
  6
+?>
  7
+<h1><?php echo $this->escapeHtml($title); ?></h1>
  8
+<p>
  9
+    <a href="<?php echo $this->url('album', array('action'=>'add'));?>">Add new album</a>
  10
+</p>
  11
+
  12
+<table class="table">
  13
+<tr>
  14
+    <th>Title</th>
  15
+    <th>Artist</th>
  16
+    <th>&nbsp;</th>
  17
+</tr>
  18
+<?php foreach ($albums as $album) : ?>
  19
+<tr>
  20
+    <td><?php echo $this->escapeHtml($album->title);?></td>
  21
+    <td><?php echo $this->escapeHtml($album->artist);?></td>
  22
+    <td>
  23
+        <a href="<?php echo $this->url('album',
  24
+            array('action'=>'edit', 'id' => $album->id));?>">Edit</a>
  25
+        <a href="<?php echo $this->url('album',
  26
+            array('action'=>'delete', 'id' => $album->id));?>">Delete</a>
  27
+    </td>
  28
+</tr>
  29
+<?php endforeach; ?>
  30
+</table>

0 notes on commit 05ee7e8

Please sign in to comment.
Something went wrong with that request. Please try again.