Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Created November 1, 2012 15:27
Show Gist options
  • Select an option

  • Save Ocramius/3994325 to your computer and use it in GitHub Desktop.

Select an option

Save Ocramius/3994325 to your computer and use it in GitHub Desktop.

Revisions

  1. Ocramius revised this gist Dec 4, 2012. 1 changed file with 171 additions and 0 deletions.
    171 changes: 171 additions & 0 deletions build.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,171 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <project name="Com2 CMS Image module" default="build">
    <target name="build" depends="install,prepare,lint,phploc,pdepend,phpmd-ci,phpcs-ci,phpcpd,phpdox,phpunit,phpcb"/>

    <target name="build-parallel" depends="install,prepare,lint,tools-parallel,phpunit,phpcb"/>

    <target name="tools-parallel" description="Run tools in parallel">
    <parallel threadCount="2">
    <sequential>
    <antcall target="pdepend"/>
    <antcall target="phpmd-ci"/>
    </sequential>
    <antcall target="phpcpd"/>
    <antcall target="phpcs-ci"/>
    <antcall target="phploc"/>
    <antcall target="phpdox"/>
    </parallel>
    </target>

    <target name="clean" description="Cleanup build artifacts">
    <delete dir="${basedir}/satis"/>
    <delete dir="${basedir}/build/api"/>
    <delete dir="${basedir}/build/code-browser"/>
    <delete dir="${basedir}/build/coverage"/>
    <delete dir="${basedir}/build/logs"/>
    <delete dir="${basedir}/build/pdepend"/>
    </target>

    <target name="get-composer" depends="clean" description="Installs composer executable in the project">
    <exec executable="bash" failonerror="true">
    <arg value="-c"/>
    <arg value="curl -s https://getcomposer.org/installer | php"/>
    </exec>
    </target>

    <target name="build-satis" depends="get-composer" description="Creates packages.json via Satis">
    <exec executable="php">
    <arg value="${basedir}/composer.phar" />
    <arg value="create-project" />
    <arg value="composer/satis" />
    </exec>
    </target>

    <target name="build-composer-packages" depends="build-satis" description="Creates packages.json via Satis">
    <exec executable="php">
    <arg value="${basedir}/satis/bin/satis" />
    <arg value="build" />
    <arg value="repositories.json" />
    <arg value="${basedir}/build" />
    </exec>
    </target>

    <target name="install" depends="get-composer" description="Installs dependencies and sets up the project">
    <exec executable="php" failonerror="true">
    <arg value="${basedir}/composer.phar" />
    <arg value="update" />
    <arg value="--prefer-dist" />
    </exec>
    </target>

    <target name="prepare" depends="clean" description="Prepare for build">
    <mkdir dir="${basedir}/build/api"/>
    <mkdir dir="${basedir}/build/code-browser"/>
    <mkdir dir="${basedir}/build/coverage"/>
    <mkdir dir="${basedir}/build/logs"/>
    <mkdir dir="${basedir}/build/pdepend"/>
    <mkdir dir="${basedir}/build/phpdox"/>
    </target>

    <target name="lint" description="Perform syntax check of sourcecode files">
    <apply executable="php" failonerror="true">
    <arg value="-l" />

    <fileset dir="${basedir}/src">
    <include name="**/*.php" />
    <modified />
    </fileset>

    <fileset dir="${basedir}/tests">
    <include name="**/*.php" />
    <modified />
    </fileset>
    </apply>
    </target>

    <target name="phploc" description="Measure project size using PHPLOC">
    <exec executable="phploc">
    <arg value="--log-csv" />
    <arg value="${basedir}/build/logs/phploc.csv" />
    <arg path="${basedir}/src" />
    </exec>
    </target>

    <target name="pdepend" description="Calculate software metrics using PHP_Depend">
    <exec executable="pdepend">
    <arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
    <arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" />
    <arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg" />
    <arg path="${basedir}/src" />
    </exec>
    </target>

    <target name="phpmd"
    description="Perform project mess detection using PHPMD and print human readable output. Intended for usage on the command line before committing.">
    <exec executable="phpmd">
    <arg path="${basedir}/src" />
    <arg value="text" />
    <arg value="${basedir}/phpmd.xml.dist" />
    </exec>
    </target>

    <target name="phpmd-ci" description="Perform project mess detection using PHPMD creating a log file for the continuous integration server">
    <exec executable="phpmd">
    <arg path="${basedir}/src" />
    <arg value="xml" />
    <arg value="${basedir}/phpmd.xml.dist" />
    <arg value="--reportfile" />
    <arg value="${basedir}/build/logs/pmd.xml" />
    </exec>
    </target>

    <target name="phpcs"
    description="Find coding standard violations using PHP_CodeSniffer and print human readable output. Intended for usage on the command line before committing.">
    <exec executable="phpcs">
    <arg value="--standard=PSR2" />
    <arg path="${basedir}/src" />
    </exec>
    </target>

    <target name="phpcs-ci" description="Find coding standard violations using PHP_CodeSniffer creating a log file for the continuous integration server">
    <exec executable="phpcs">
    <arg value="--report=checkstyle" />
    <arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
    <arg value="--standard=PSR2" />
    <arg path="${basedir}/src" />
    </exec>
    </target>

    <target name="phpcpd" description="Find duplicate code using PHPCPD">
    <exec executable="phpcpd">
    <arg value="--log-pmd" />
    <arg value="${basedir}/build/logs/pmd-cpd.xml" />
    <arg path="${basedir}/src" />
    </exec>
    </target>

    <target name="phpdox" description="Generate API documentation using phpDox">
    <exec executable="phpdox">
    <arg value="-f" />
    <arg value="${basedir}/phpdox.xml.dist" />
    </exec>
    </target>

    <target name="phpunit" description="Run unit tests with PHPUnit">
    <exec executable="phpunit" failonerror="true">
    <arg value="-c" />
    <arg value="${basedir}/phpunit.xml.dist" />
    </exec>
    </target>

    <target name="phpcb" description="Aggregate tool output with PHP_CodeBrowser">
    <exec executable="phpcb">
    <arg value="--log" />
    <arg path="${basedir}/build/logs" />
    <arg value="--source" />
    <arg path="${basedir}/src" />
    <arg value="--output" />
    <arg path="${basedir}/build/code-browser" />
    </exec>
    </target>
    </project>
  2. Ocramius created this gist Nov 1, 2012.
    52 changes: 52 additions & 0 deletions Bootstrap.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <?php
    /**
    * @author Marco Pivetta <ocramius@gmail.com>
    */

    use Zend\ServiceManager\ServiceManager;
    use Zend\Mvc\Service\ServiceManagerConfig;
    use DoctrineORMModuleTest\Framework\TestCase;
    use ContentTest\Util\ServiceManagerFactory;
    use Zend\Loader\StandardAutoloader;

    chdir(__DIR__);

    $previousDir = '.';

    while (!file_exists('config/application.config.php')) {
    $dir = dirname(getcwd());

    if ($previousDir === $dir) {
    throw new RuntimeException(
    'Unable to locate "config/application.config.php":'
    . ' is the Content module in a sub-directory of your application skeleton?'
    );
    }

    $previousDir = $dir;
    chdir($dir);
    }

    if (!((@include_once __DIR__ . '/../../../../../vendor/autoload.php') || !(@include_once __DIR__ . '/../../../../autoload.php'))) {
    throw new RuntimeException('vendor/autoload.php could not be found. Did you run `php composer.phar install`?');
    }

    if (!$config = @include __DIR__ . '/TestConfiguration.php') {
    $config = require __DIR__ . '/TestConfiguration.php.dist';
    }

    $loader = new StandardAutoloader();
    $loader->registerNamespace('ContentTest', __DIR__ . 'ContentTest');
    $loader->registerNamespace('ContentDataFixture', __DIR__ . 'ContentDataFixture');
    $loader->register();

    $serviceManager = new ServiceManager(new ServiceManagerConfig(
    isset($config['service_manager']) ? $config['service_manager'] : array()
    ));
    $serviceManager->setService('ApplicationConfig', $config);

    /** @var $moduleManager \Zend\ModuleManager\ModuleManager */
    $moduleManager = $serviceManager->get('ModuleManager');
    $moduleManager->loadModules();

    ServiceManagerFactory::setApplicationConfig($config);
    45 changes: 45 additions & 0 deletions PageRepositoryTest.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php

    namespace ContentTest\EntityRepository;

    use PHPUnit_Framework_TestCase;
    use ContentTest\Util\ServiceManagerFactory;
    use Content\EntityRepository\PageRepository;

    /**
    * Tests for PageRepository
    *
    * @author Marco Pivetta <ocramius@gmail.com>
    */
    class PageRepositoryTest extends PHPUnit_Framework_TestCase
    {
    /**
    * @var \Doctrine\Common\DataFixtures\Executor\AbstractExecutor
    */
    protected $fixtureExectutor;

    /**
    * @var PageRepository
    */
    protected $repository;

    public function setUp()
    {
    $sm = ServiceManagerFactory::getServiceManager();
    $this->repository = $sm->get('Content\EntityRepository\PageRepository');
    $this->fixtureExectutor = $sm->get('Doctrine\Common\DataFixtures\Executor\AbstractExecutor');
    $this->assertInstanceOf('Content\EntityRepository\PageRepository', $this->repository);
    }

    /**
    * @covers \Content\EntityRepository\PageRepository::getRootPages
    */
    public function testGetRootPages()
    {
    $pageWithNoLogs = new PageWithNoLogs();
    $this->fixtureExectutor->execute(array($pageWithNoLogs));
    $this->assertEmpty($this->repository->getRootPages());

    $this->assertCount(1, $this->repository->findAll());
    }
    }
    36 changes: 36 additions & 0 deletions PageWithNoLogs.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <?php
    namespace ContentDataFixture;

    use Doctrine\Common\DataFixtures\AbstractFixture;
    use Doctrine\Common\Persistence\ObjectManager;

    use Content\Entity\Page;

    /**
    * @author Marco Pivetta <ocramius@gmail.com>
    */
    class PageWithNoLogs extends AbstractFixture
    {
    /**
    * @var Page
    */
    protected $page;

    /**
    * {inheritDoc}
    */
    public function load(ObjectManager $manager)
    {
    $this->page = new Page();
    $manager->persist($this->page);
    $manager->flush();
    }

    /**
    * @return Page
    */
    public function getPage()
    {
    return $this->page;
    }
    }
    75 changes: 75 additions & 0 deletions ServiceManagerFactory.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    <?php
    /**
    * @author Marco Pivetta <ocramius@gmail.com>
    */

    namespace ContentTest\Util;

    use Zend\ServiceManager\ServiceManager;
    use Zend\ServiceManager\ServiceLocatorInterface;
    use Zend\Mvc\Service\ServiceManagerConfig;

    use Doctrine\Common\DataFixtures\Purger\ORMPurger as FixturePurger;
    use Doctrine\Common\DataFixtures\Executor\ORMExecutor as FixtureExecutor;

    use Doctrine\ORM\Tools\SchemaTool;

    /**
    * Base test case to be used when a new service manager instance is required
    */
    abstract class ServiceManagerFactory
    {
    /**
    * @var array
    */
    private static $config = array();

    /**
    * @static
    * @param array $config
    */
    public static function setApplicationConfig(array $config)
    {
    static::$config = $config;
    }

    /**
    * @static
    * @return array
    */
    public static function getApplicationConfig()
    {
    return static::$config;
    }

    /**
    * @param array|null $config
    * @return ServiceManager
    */
    public static function getServiceManager(array $config = null)
    {
    $config = $config ?: static::getApplicationConfig();
    $serviceManager = new ServiceManager(new ServiceManagerConfig(
    isset($config['service_manager']) ? $config['service_manager'] : array()
    ));
    $serviceManager->setService('ApplicationConfig', $config);
    /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */
    $moduleManager = $serviceManager->get('ModuleManager');
    $moduleManager->loadModules();

    // @todo move to own factory class/add to merged configuration? Create a test module?
    $serviceManager->setFactory(
    'Doctrine\Common\DataFixtures\Executor\AbstractExecutor',
    function(ServiceLocatorInterface $sl)
    {
    /* @var $em \Doctrine\ORM\EntityManager */
    $em = $sl->get('Doctrine\ORM\EntityManager');
    $schemaTool = new SchemaTool($em);
    $schemaTool->createSchema($em->getMetadataFactory()->getAllMetadata());
    return new FixtureExecutor($em, new FixturePurger($em));
    }
    );

    return $serviceManager;
    }
    }