Skip to content
This repository has been archived by the owner on Nov 13, 2017. It is now read-only.

Commit

Permalink
added basic peer methods and state check/change methods to Subscriber…
Browse files Browse the repository at this point in the history
…/Peer
  • Loading branch information
havvg committed May 1, 2010
1 parent c3cfdf2 commit 1599651
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 0 deletions.
65 changes: 65 additions & 0 deletions data/fixtures/sfNewsletterPlugin-SubscriptionTest.yml
@@ -0,0 +1,65 @@
ContentType:
ContentType_1:
name: "plain text"
mime_type: "text/plain"

ContentType_2:
name: "HTML text"
mime_type: "text/html"

Newsletter:
Newsletter_1:
name: "first newsletter"
subject: "testing one"
content: "This is a plain text newsletter."
content_type_id: ContentType_1
created_at: ~
scheduled_at: NULL
sent_at: NULL

Newsletter_2:
name: "second newsletter"
subject: "testing two"
content: "This is <strong>not</strong> a plain text newsletter."
content_type_id: ContentType_2
created_at: ~
scheduled_at: NULL
sent_at: NULL

Subscriber:
Subscriber_1:
# entered, but not activated
name: "first subscriber"
email: "subscriber-one@example.net"
unsubscribe_hash: "92hjk248ds23"
activate_hash: "892kd7823"
is_active: false
created_at: ~
activated_at: NULL
Subscriber_2:
# entered, but not activated
name: "second subscriber"
email: "subscriber-two@example.com"
unsubscribe_hash: "asd87324"
activate_hash: "5uhj32l4"
is_active: false
created_at: ~
activated_at: NULL
Subscriber_3:
# activated and unsubscribed
name: "third subscriber"
email: "subscriber-three@example.net"
unsubscribe_hash: "723jksdo2"
activate_hash: "892kd7823"
is_active: false
created_at: '2009-10-04 21:38:23'
activated_at: '2009-10-05 22:04:32'
Subscriber_4:
# activated and subscribed
name: "fourth subscriber"
email: "subscriber-four@example.com"
unsubscribe_hash: "9123hsdaa"
activate_hash: "7k2j3"
is_active: true
created_at: '2009-10-03 21:38:23'
activated_at: '2009-10-03 22:04:32'
33 changes: 33 additions & 0 deletions lib/model/plugin/PluginSubscriber.php
Expand Up @@ -2,4 +2,37 @@

class PluginSubscriber extends BaseSubscriber
{
/**
* Activate the subscription.
*
* @return Subscriber (this)
*/
public function activate()
{
$this->setActivatedAt(new DateTime())->setIsActive(true)->save();

return $this;
}

/**
* Unsubscribe from Newsletters.
*
* @return Subscriber (this)
*/
public function unsubscribe()
{
$this->setIsActive(false)->save();

return $this;
}

/**
* Check whether this Subscriber is subscribed.
*
* @return bool
*/
public function isSubscribed()
{
return (bool) $this->getIsActive();
}
}
84 changes: 84 additions & 0 deletions lib/model/plugin/PluginSubscriberPeer.php
Expand Up @@ -2,4 +2,88 @@

class PluginSubscriberPeer extends BaseSubscriberPeer
{
/**
* Returns all active Subscribers.
*
* @param Criteria $criteria
* @param PropelPDO $con
*
* @return array of Subscriber
*/
public static function retrieveSubscribed(Criteria $criteria = null, PropelPDO $con = null)
{
if ($con === null)
{
$con = Propel::getConnection(SubscriberPeer::DATABASE_NAME, Propel::CONNECTION_READ);
}

if (empty($criteria))
{
$criteria = new Criteria();
}
else
{
$criteria = clone $criteria;
}

$criteria->add(self::IS_ACTIVE, true, Criteria::EQUAL);

return self::doSelect($criteria, $con);
}

/**
* Returns all Subscribers that are required to activate themselves.
*
* @param Criteria $criteria
* @param PropelPDO $con
*
* @return array of Subscriber
*/
public static function retrievePendingActivation(Criteria $criteria = null, PropelPDO $con = null)
{
if ($con === null)
{
$con = Propel::getConnection(SubscriberPeer::DATABASE_NAME, Propel::CONNECTION_READ);
}

if (empty($criteria))
{
$criteria = new Criteria();
}
else
{
$criteria = clone $criteria;
}

$criteria->add(self::IS_ACTIVE, false, Criteria::EQUAL);
$criteria->add(self::ACTIVATED_AT, null, Criteria::EQUAL);

return self::doSelect($criteria, $con);
}

/**
* Retrieve a single Subscriber by the given E-Mail.
*
* @param string $email
* @param PropelPDO $con
*
* @return Subscriber
*/
public static function retrieveByEmail($email, PropelPDO $con = null)
{
if (!is_string($email))
{
throw new InvalidArgumentException('The given email is invalid.');
}

if ($con === null)
{
$con = Propel::getConnection(SubscriberPeer::DATABASE_NAME, Propel::CONNECTION_READ);
}

$criteria = new Criteria();
$criteria->add(self::EMAIL, $email, Criteria::EQUAL);

return self::doSelectOne($criteria, $con);
}
}
48 changes: 48 additions & 0 deletions test/unit/sfNewsletterPlugin-SubscribersTest.php
@@ -0,0 +1,48 @@
<?php
require_once(dirname(__FILE__) . '/../bootstrap/unit.php');

# load fixtures of this plugin
$propelData->loadData(sfConfig::get('sf_plugins_dir') . '/sfNewsletterPlugin/data/fixtures');

$limeTest = new lime_test(12, new lime_output_color());

$subscriber = SubscriberPeer::retrieveSubscribed();
$limeTest->is(count($subscriber), 1, 'Count of active subscriptions.');

$pendingActivation = SubscriberPeer::retrievePendingActivation();
$limeTest->is(count($pendingActivation), 2, 'Count of subscribers that have to activate themselves.');

$criteria = new Criteria();
$criteria->add(SubscriberPeer::EMAIL, '%example.net', Criteria::LIKE);

$pendingActivation = SubscriberPeer::retrievePendingActivation($criteria);
$limeTest->is(count($pendingActivation), 1, 'Count pending activation from example.net.');

$criteria = new Criteria();
$criteria->add(SubscriberPeer::EMAIL, '%example%', Criteria::LIKE);

$pendingActivation = SubscriberPeer::retrievePendingActivation($criteria);
$limeTest->is(count($pendingActivation), 2, 'Count pending activation from example (any TLD).');

$subscriber = SubscriberPeer::retrieveByEmail('subscriber-three@example.net');
$limeTest->isa_ok($subscriber, 'Subscriber', 'Retrieved correct class.');
$limeTest->is($subscriber->getName(), 'third subscriber', 'Retrieved by email.');

try
{
SubscriberPeer::retrieveByEmail(1);
$limeTest->error('Invalid email provided.');
}
catch (InvalidArgumentException $e)
{
$limeTest->pass('InvalidArgumentException caught on invalid email.');
}

$subscriber = SubscriberPeer::retrieveByEmail('subscriber-one@example.net');
$limeTest->isa_ok($subscriber, 'Subscriber', 'Found Subscriber.');
$limeTest->is($subscriber->getName(), 'first subscriber', 'Found correct Subscriber.');
$limeTest->isnt($subscriber->isSubscribed(), true, 'Is not subscribed.');
$subscriber->activate();
$limeTest->ok($subscriber->isSubscribed(), 'Is now subscribed.');
$subscriber->unsubscribe();
$limeTest->isnt($subscriber->isSubscribed(), true, 'Is now unsubscribed.');

0 comments on commit 1599651

Please sign in to comment.